use of ddf.catalog.util.impl.RelevanceResultComparator in project ddf by codice.
the class QueryRunnable method getResultComparator.
protected Comparator<Result> getResultComparator(Query query) {
Comparator<Result> sortComparator = new RelevanceResultComparator(SortOrder.DESCENDING);
SortBy sortBy = query.getSortBy();
if (sortBy != null && sortBy.getPropertyName() != null) {
PropertyName sortingProp = sortBy.getPropertyName();
String sortType = sortingProp.getPropertyName();
SortOrder sortOrder = (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
// Temporal searches are currently sorted by the effective time
if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
sortComparator = new TemporalResultComparator(sortOrder);
} else if (Metacard.CREATED.equals(sortType) || Metacard.MODIFIED.equals(sortType)) {
sortComparator = new TemporalResultComparator(sortOrder, sortType);
} else if (Result.DISTANCE.equals(sortType)) {
sortComparator = new DistanceResultComparator(sortOrder);
} else if (Result.RELEVANCE.equals(sortType)) {
sortComparator = new RelevanceResultComparator(sortOrder);
}
}
return sortComparator;
}
use of ddf.catalog.util.impl.RelevanceResultComparator in project ddf by codice.
the class SortedQueryMonitor method run.
@Override
public void run() {
SortBy sortBy = query.getSortBy();
// Prepare the Comparators that we will use
Comparator<Result> coreComparator = CachingFederationStrategy.DEFAULT_COMPARATOR;
if (sortBy != null && sortBy.getPropertyName() != null) {
PropertyName sortingProp = sortBy.getPropertyName();
String sortType = sortingProp.getPropertyName();
SortOrder sortOrder = (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
LOGGER.debug("Sorting type: {}", sortType);
LOGGER.debug("Sorting order: {}", sortBy.getSortOrder());
// Temporal searches are currently sorted by the effective time
if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
coreComparator = new TemporalResultComparator(sortOrder);
} else if (Result.DISTANCE.equals(sortType)) {
coreComparator = new DistanceResultComparator(sortOrder);
} else if (Result.RELEVANCE.equals(sortType)) {
coreComparator = new RelevanceResultComparator(sortOrder);
}
}
List<Result> resultList = new ArrayList<>();
long totalHits = 0;
Set<ProcessingDetails> processingDetails = returnResults.getProcessingDetails();
Map<String, Serializable> returnProperties = returnResults.getProperties();
HashMap<String, Long> hitsPerSource = new HashMap<>();
for (int i = futures.size(); i > 0; i--) {
String sourceId = "Unknown Source";
QueryRequest queryRequest = null;
SourceResponse sourceResponse = null;
try {
Future<SourceResponse> future;
if (query.getTimeoutMillis() < 1) {
future = completionService.take();
} else {
future = completionService.poll(getTimeRemaining(deadline), TimeUnit.MILLISECONDS);
if (future == null) {
timeoutRemainingSources(processingDetails);
break;
}
}
queryRequest = futures.remove(future);
sourceId = getSourceIdFromRequest(queryRequest);
sourceResponse = future.get();
if (sourceResponse == null) {
LOGGER.debug("Source {} returned null response", sourceId);
executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, new NullPointerException(), processingDetails);
} else {
sourceResponse = executePostFederationQueryPlugins(sourceResponse, queryRequest);
resultList.addAll(sourceResponse.getResults());
long hits = sourceResponse.getHits();
totalHits += hits;
hitsPerSource.merge(sourceId, hits, (l1, l2) -> l1 + l2);
Map<String, Serializable> properties = sourceResponse.getProperties();
returnProperties.putAll(properties);
}
} catch (InterruptedException e) {
if (queryRequest != null) {
// First, add interrupted processing detail for this source
LOGGER.debug("Search interrupted for {}", sourceId);
executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, e, processingDetails);
}
// Then add the interrupted exception for the remaining sources
interruptRemainingSources(processingDetails, e);
break;
} catch (ExecutionException e) {
LOGGER.info("Couldn't get results from completed federated query. {}, {}", sourceId, Exceptions.getFullMessage(e), e);
executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, e, processingDetails);
}
}
returnProperties.put("hitsPerSource", hitsPerSource);
LOGGER.debug("All sources finished returning results: {}", resultList.size());
returnResults.setHits(totalHits);
if (CachingFederationStrategy.INDEX_QUERY_MODE.equals(request.getPropertyValue(CachingFederationStrategy.QUERY_MODE))) {
QueryResponse result = cachingFederationStrategy.queryCache(request);
returnResults.addResults(result.getResults(), true);
} else {
returnResults.addResults(sortedResults(resultList, coreComparator), true);
}
}
Aggregations