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);
}
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);
}
Aggregations