Search in sources :

Example 1 with MultiCollectorManager

use of org.apache.lucene.search.MultiCollectorManager in project lucene-solr by apache.

the class DrillSideways method search.

/** Runs a search, using a {@link CollectorManager} to gather and merge search results */
public <R> ConcurrentDrillSidewaysResult<R> search(final DrillDownQuery query, final CollectorManager<?, R> hitCollectorManager) throws IOException {
    final Map<String, Integer> drillDownDims = query.getDims();
    final List<CallableCollector> callableCollectors = new ArrayList<>(drillDownDims.size() + 1);
    // Add the main DrillDownQuery
    callableCollectors.add(new CallableCollector(-1, searcher, query, new MultiCollectorManager(new FacetsCollectorManager(), hitCollectorManager)));
    int i = 0;
    final Query[] filters = query.getDrillDownQueries();
    for (String dim : drillDownDims.keySet()) callableCollectors.add(new CallableCollector(i++, searcher, getDrillDownQuery(query, filters, dim), new FacetsCollectorManager()));
    final FacetsCollector mainFacetsCollector;
    final FacetsCollector[] facetsCollectors = new FacetsCollector[drillDownDims.size()];
    final R collectorResult;
    try {
        // Run the query pool
        final List<Future<CallableResult>> futures = executor.invokeAll(callableCollectors);
        // Extract the results
        final Object[] mainResults = (Object[]) futures.get(0).get().result;
        mainFacetsCollector = (FacetsCollector) mainResults[0];
        collectorResult = (R) mainResults[1];
        for (i = 1; i < futures.size(); i++) {
            final CallableResult result = futures.get(i).get();
            facetsCollectors[result.pos] = (FacetsCollector) result.result;
        }
        // Fill the null results with the mainFacetsCollector
        for (i = 0; i < facetsCollectors.length; i++) if (facetsCollectors[i] == null)
            facetsCollectors[i] = mainFacetsCollector;
    } catch (InterruptedException e) {
        throw new ThreadInterruptedException(e);
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
    // build the facets and return the result
    return new ConcurrentDrillSidewaysResult<>(buildFacetsResult(mainFacetsCollector, facetsCollectors, drillDownDims.keySet().toArray(new String[drillDownDims.size()])), null, collectorResult);
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ArrayList(java.util.ArrayList) ThreadInterruptedException(org.apache.lucene.util.ThreadInterruptedException) ThreadInterruptedException(org.apache.lucene.util.ThreadInterruptedException) MultiCollectorManager(org.apache.lucene.search.MultiCollectorManager) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MultiCollectorManager (org.apache.lucene.search.MultiCollectorManager)1 Query (org.apache.lucene.search.Query)1 ThreadInterruptedException (org.apache.lucene.util.ThreadInterruptedException)1