Example usage for org.apache.lucene.search CachingCollector replay

List of usage examples for org.apache.lucene.search CachingCollector replay

Introduction

In this page you can find the example usage for org.apache.lucene.search CachingCollector replay.

Prototype

public abstract void replay(Collector other) throws IOException;

Source Link

Document

Replays the cached doc IDs (and scores) to the given Collector.

Usage

From source file:org.apache.solr.search.Grouping.java

License:Apache License

public void execute() throws IOException {
    if (commands.isEmpty()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Specify at least one field, function or query to group by.");
    }/*  ww w.  j a  va 2 s . c  o  m*/

    DocListAndSet out = new DocListAndSet();
    qr.setDocListAndSet(out);

    SolrIndexSearcher.ProcessedFilter pf = searcher.getProcessedFilter(cmd.getFilter(), cmd.getFilterList());
    final Filter luceneFilter = pf.filter;
    maxDoc = searcher.maxDoc();

    needScores = (cmd.getFlags() & SolrIndexSearcher.GET_SCORES) != 0;
    boolean cacheScores = false;
    // NOTE: Change this when groupSort can be specified per group
    if (!needScores && !commands.isEmpty()) {
        if (commands.get(0).groupSort == null) {
            cacheScores = true;
        } else {
            for (SortField field : commands.get(0).groupSort.getSort()) {
                if (field.getType() == SortField.Type.SCORE) {
                    cacheScores = true;
                    break;
                }
            }
        }
    } else if (needScores) {
        cacheScores = needScores;
    }
    getDocSet = (cmd.getFlags() & SolrIndexSearcher.GET_DOCSET) != 0;
    getDocList = (cmd.getFlags() & SolrIndexSearcher.GET_DOCLIST) != 0;
    query = QueryUtils.makeQueryable(cmd.getQuery());

    for (Command cmd : commands) {
        cmd.prepare();
    }

    AbstractAllGroupHeadsCollector<?> allGroupHeadsCollector = null;
    List<Collector> collectors = new ArrayList<Collector>(commands.size());
    for (Command cmd : commands) {
        Collector collector = cmd.createFirstPassCollector();
        if (collector != null) {
            collectors.add(collector);
        }
        if (getGroupedDocSet && allGroupHeadsCollector == null) {
            collectors.add(allGroupHeadsCollector = cmd.createAllGroupCollector());
        }
    }

    Collector allCollectors = MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()]));
    DocSetCollector setCollector = null;
    if (getDocSet && allGroupHeadsCollector == null) {
        setCollector = new DocSetDelegateCollector(maxDoc >> 6, maxDoc, allCollectors);
        allCollectors = setCollector;
    }

    CachingCollector cachedCollector = null;
    if (cacheSecondPassSearch && allCollectors != null) {
        int maxDocsToCache = (int) Math.round(maxDoc * (maxDocsPercentageToCache / 100.0d));
        // Only makes sense to cache if we cache more than zero.
        // Maybe we should have a minimum and a maximum, that defines the window we would like caching for.
        if (maxDocsToCache > 0) {
            allCollectors = cachedCollector = CachingCollector.create(allCollectors, cacheScores,
                    maxDocsToCache);
        }
    }

    if (pf.postFilter != null) {
        pf.postFilter.setLastDelegate(allCollectors);
        allCollectors = pf.postFilter;
    }

    if (allCollectors != null) {
        searchWithTimeLimiter(luceneFilter, allCollectors);
    }

    if (getGroupedDocSet && allGroupHeadsCollector != null) {
        FixedBitSet fixedBitSet = allGroupHeadsCollector.retrieveGroupHeads(maxDoc);
        long[] bits = fixedBitSet.getBits();
        OpenBitSet openBitSet = new OpenBitSet(bits, bits.length);
        qr.setDocSet(new BitDocSet(openBitSet));
    } else if (getDocSet) {
        qr.setDocSet(setCollector.getDocSet());
    }

    collectors.clear();
    for (Command cmd : commands) {
        Collector collector = cmd.createSecondPassCollector();
        if (collector != null)
            collectors.add(collector);
    }

    if (!collectors.isEmpty()) {
        Collector secondPhaseCollectors = MultiCollector
                .wrap(collectors.toArray(new Collector[collectors.size()]));
        if (collectors.size() > 0) {
            if (cachedCollector != null) {
                if (cachedCollector.isCached()) {
                    cachedCollector.replay(secondPhaseCollectors);
                } else {
                    signalCacheWarning = true;
                    logger.warn(String.format(Locale.ROOT,
                            "The grouping cache is active, but not used because it exceeded the max cache limit of %d percent",
                            maxDocsPercentageToCache));
                    logger.warn("Please increase cache size or disable group caching.");
                    searchWithTimeLimiter(luceneFilter, secondPhaseCollectors);
                }
            } else {
                if (pf.postFilter != null) {
                    pf.postFilter.setLastDelegate(secondPhaseCollectors);
                    secondPhaseCollectors = pf.postFilter;
                }
                searchWithTimeLimiter(luceneFilter, secondPhaseCollectors);
            }
        }
    }

    for (Command cmd : commands) {
        cmd.finish();
    }

    qr.groupedResults = grouped;

    if (getDocList) {
        int sz = idSet.size();
        int[] ids = new int[sz];
        int idx = 0;
        for (int val : idSet) {
            ids[idx++] = val;
        }
        qr.setDocList(new DocSlice(0, sz, ids, null, maxMatches, maxScore));
    }
}