Search in sources :

Example 1 with SearchResult

use of org.onebusaway.transit_data_federation.model.SearchResult in project onebusaway-application-modules by camsys.

the class StopSearchServiceImpl method search.

private SearchResult<AgencyAndId> search(QueryParser parser, String value, int maxResultCount, double minScoreToKeep) throws IOException, ParseException {
    if (_searcher == null)
        return new SearchResult<AgencyAndId>();
    TopDocCollector collector = new TopDocCollector(maxResultCount);
    Query query = parser.parse(value);
    _searcher.search(query, collector);
    TopDocs top = collector.topDocs();
    Map<AgencyAndId, Float> topScores = new HashMap<AgencyAndId, Float>();
    for (ScoreDoc sd : top.scoreDocs) {
        Document document = _searcher.doc(sd.doc);
        if (sd.score < minScoreToKeep)
            continue;
        String agencyId = document.get(StopSearchIndexConstants.FIELD_AGENCY_ID);
        String stopId = document.get(StopSearchIndexConstants.FIELD_STOP_ID);
        AgencyAndId id = new AgencyAndId(agencyId, stopId);
        Float existingScore = topScores.get(id);
        if (existingScore == null || existingScore < sd.score)
            topScores.put(id, sd.score);
    }
    List<AgencyAndId> ids = new ArrayList<AgencyAndId>(top.totalHits);
    double[] scores = new double[top.totalHits];
    int index = 0;
    for (AgencyAndId id : topScores.keySet()) {
        ids.add(id);
        scores[index] = topScores.get(id);
        index++;
    }
    return new SearchResult<AgencyAndId>(ids, scores);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Query(org.apache.lucene.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResult(org.onebusaway.transit_data_federation.model.SearchResult) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) TopDocCollector(org.apache.lucene.search.TopDocCollector)

Example 2 with SearchResult

use of org.onebusaway.transit_data_federation.model.SearchResult in project onebusaway-application-modules by camsys.

the class RouteCollectionSearchServiceImpl method search.

private SearchResult<AgencyAndId> search(QueryParser parser, String value, int maxResultCount, double minScoreToKeep) throws IOException, ParseException {
    if (_searcher == null)
        return new SearchResult<AgencyAndId>();
    TopDocCollector collector = new TopDocCollector(maxResultCount);
    Query query = parser.parse(value);
    _searcher.search(query, collector);
    TopDocs top = collector.topDocs();
    Map<AgencyAndId, Float> topScores = new HashMap<AgencyAndId, Float>();
    String lowerCaseQueryValue = value.toLowerCase();
    for (ScoreDoc sd : top.scoreDocs) {
        Document document = _searcher.doc(sd.doc);
        String routeShortName = document.get(RouteCollectionSearchIndexConstants.FIELD_ROUTE_SHORT_NAME);
        Set<String> tokens = new HashSet<String>();
        if (routeShortName != null) {
            for (String token : routeShortName.toLowerCase().split("\\b")) {
                if (!token.isEmpty())
                    tokens.add(token);
            }
        }
        // Result must have a minimum score to qualify
        if (sd.score < minScoreToKeep && !tokens.contains(lowerCaseQueryValue))
            continue;
        // Keep the best score for a particular id
        String agencyId = document.get(RouteCollectionSearchIndexConstants.FIELD_ROUTE_COLLECTION_AGENCY_ID);
        String id = document.get(RouteCollectionSearchIndexConstants.FIELD_ROUTE_COLLECTION_ID);
        AgencyAndId routeId = new AgencyAndId(agencyId, id);
        Float score = topScores.get(routeId);
        if (score == null || score < sd.score)
            topScores.put(routeId, sd.score);
    }
    List<AgencyAndId> ids = new ArrayList<AgencyAndId>(topScores.size());
    double[] scores = new double[topScores.size()];
    int index = 0;
    for (AgencyAndId id : topScores.keySet()) {
        ids.add(id);
        scores[index] = topScores.get(id);
        index++;
    }
    return new SearchResult<AgencyAndId>(ids, scores);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Query(org.apache.lucene.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResult(org.onebusaway.transit_data_federation.model.SearchResult) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) TopDocCollector(org.apache.lucene.search.TopDocCollector) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Document (org.apache.lucene.document.Document)2 Query (org.apache.lucene.search.Query)2 ScoreDoc (org.apache.lucene.search.ScoreDoc)2 TopDocCollector (org.apache.lucene.search.TopDocCollector)2 TopDocs (org.apache.lucene.search.TopDocs)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 SearchResult (org.onebusaway.transit_data_federation.model.SearchResult)2 HashSet (java.util.HashSet)1