/** * Helper method for inspecting QueryCommand and creating the appropriate * {@link TopDocsCollector} * * @param len the number of docs to return * @param cmd The Command whose properties should determine the type of * TopDocsCollector to use. */ private TopDocsCollector buildTopDocsCollector(int len, QueryCommand cmd) throws IOException { Query q = cmd.getQuery(); if(q instanceof RankQuery) { RankQuery rq = (RankQuery)q; return rq.getTopDocsCollector(len, cmd, this); } if (null == cmd.getSort()) { assert null == cmd.getCursorMark() : "have cursor but no sort"; return TopScoreDocCollector.create(len, true); } else { // we have a sort final boolean needScores = (cmd.getFlags() & GET_SCORES) != 0; final Sort weightedSort = weightSort(cmd.getSort()); final CursorMark cursor = cmd.getCursorMark(); // :TODO: make fillFields it's own QueryCommand flag? ... // ... see comments in populateNextCursorMarkFromTopDocs for cache issues (SOLR-5595) final boolean fillFields = (null != cursor); final FieldDoc searchAfter = (null != cursor ? cursor.getSearchAfterFieldDoc() : null); return TopFieldCollector.create(weightedSort, len, searchAfter, fillFields, needScores, needScores, true); } }
/** * Helper method for inspecting QueryCommand and creating the appropriate * {@link TopDocsCollector} * * @param len the number of docs to return * @param cmd The Command whose properties should determine the type of * TopDocsCollector to use. */ private TopDocsCollector buildTopDocsCollector(int len, QueryCommand cmd) throws IOException { if (null == cmd.getSort()) { assert null == cmd.getCursorMark() : "have cursor but no sort"; return TopScoreDocCollector.create(len, true); } else { // we have a sort final boolean needScores = (cmd.getFlags() & GET_SCORES) != 0; final Sort weightedSort = weightSort(cmd.getSort()); final CursorMark cursor = cmd.getCursorMark(); // :TODO: make fillFields it's own QueryCommand flag? ... // ... see comments in populateNextCursorMarkFromTopDocs for cache issues (SOLR-5595) final boolean fillFields = (null != cursor); final FieldDoc searchAfter = (null != cursor ? cursor.getSearchAfterFieldDoc() : null); return TopFieldCollector.create(weightedSort, len, searchAfter, fillFields, needScores, needScores, true); } }
private static void query(String indexDir, Query q) throws IOException, ParseException { int hitsPerPage = 10; IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir))); IndexSearcher indexSearcher = new IndexSearcher(reader); TopDocsCollector collector = TopScoreDocCollector.create(hitsPerPage, false); indexSearcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; LOGGER.info("Found " + hits.length + " hits."); for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = indexSearcher.doc(docId); // LOGGER.info((i + 1) + ". " + d.get("title")); } // searcher can only be closed when there // is no need to access the documents any more. // indexSearcher.close(); }
private static void query(IndexSearcher indexSearcher, Query q) throws IOException, ParseException { int hitsPerPage = 10; TopDocsCollector collector = TopScoreDocCollector.create(hitsPerPage, false); indexSearcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; LOGGER.info("Found " + hits.length + " hits."); for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = indexSearcher.doc(docId); LOGGER.info((i + 1) + ". " + d.get("title")); } }
@Override public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException { Query rawParentFilter; if (parentObjectMapper == null) { rawParentFilter = Queries.newNonNestedFilter(); } else { rawParentFilter = parentObjectMapper.nestedTypeFilter(); } BitSetProducer parentFilter = context.bitsetFilterCache().getBitSetProducer(rawParentFilter); Query childFilter = childObjectMapper.nestedTypeFilter(); Query q = Queries.filtered(query(), new NestedChildrenQuery(parentFilter, childFilter, hitContext)); if (size() == 0) { return new TopDocs(context.searcher().count(q), Lucene.EMPTY_SCORE_DOCS, 0); } else { int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc()); TopDocsCollector topDocsCollector; if (sort() != null) { try { topDocsCollector = TopFieldCollector.create(sort().sort, topN, true, trackScores(), trackScores()); } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); } } else { topDocsCollector = TopScoreDocCollector.create(topN); } try { context.searcher().search(q, topDocsCollector); } finally { clearReleasables(Lifetime.COLLECTION); } return topDocsCollector.topDocs(from(), size()); } }
public EarlyTerminationCollector(TopDocsCollector collector, Sort sort, int numDocsToCollectPerSortedSegment) { if (numDocsToCollectPerSortedSegment <= 0) { throw new IllegalStateException( "numDocsToCollectPerSortedSegment must always be > 0, got " + numDocsToCollectPerSortedSegment); } this.collector = collector; this.sort = sort; this.numDocsToCollectPerSortedSegment = numDocsToCollectPerSortedSegment; }
public LindenDocsCollector(Collector collector) { if (!(collector instanceof TopDocsCollector) && !(collector instanceof EarlyTerminationCollector)) { throw new RuntimeException("Unsupported collector class in LindenDocsCollector: " + collector.getClass().getName()); } hitCollector = collector; wrappedCollector = collector; }
@Override public TopDocs reduce(Collection<AccessibleTimeLimitingCollector> collectors) throws IOException { TopDocs[] docs = collectors.stream() .map(AccessibleTimeLimitingCollector::getWrappedCollector) .map(TopDocsCollector::topDocs) .collect(Collectors.toList()) .toArray(new TopDocs[collectors.size()]); return TopDocs.merge(perPage, docs); }
public int getTotalHits() { if (totalHits == null) { try { TopDocsCollector documentCollector = TopFieldCollector.create( this.sort, 1, null, false, false, false, false); searcher.search(this.q, documentCollector); this.totalHits = documentCollector.getTotalHits(); } catch (IOException ex) { throw new RuntimeException(ex); } } return this.totalHits; }
private TopDocs queryDocuments(int pageSize, FieldDoc memento) throws IOException { TopDocsCollector documentCollector = TopFieldCollector.create( this.sort, pageSize, memento, true, false, false, false); searcher.search(this.q, documentCollector); this.totalHits = documentCollector.getTotalHits(); return documentCollector.topDocs(0, pageSize); }
TopDocsCollector newCollector(Sort sort, boolean needScores) throws IOException { int groupDocsToCollect = getMax(groupOffset, docsPerGroup, maxDoc); if (sort == null || sort == Sort.RELEVANCE) { return TopScoreDocCollector.create(groupDocsToCollect, true); } else { return TopFieldCollector.create(searcher.weightSort(sort), groupDocsToCollect, false, needScores, needScores, true); } }
/** * {@inheritDoc} */ @Override protected void finish() throws IOException { TopDocsCollector topDocsCollector = (TopDocsCollector) collector.getDelegate(); TopDocs topDocs = topDocsCollector.topDocs(); GroupDocs<String> groupDocs = new GroupDocs<>(Float.NaN, topDocs.getMaxScore(), topDocs.totalHits, topDocs.scoreDocs, query.toString(), null); if (main) { mainResult = getDocList(groupDocs); } else { NamedList rsp = commonResponse(); addDocList(rsp, groupDocs); } }
public TopDocsCollector getTopDocsCollector(int len, SolrIndexSearcher.QueryCommand cmd, IndexSearcher searcher) throws IOException { if(this.boostedPriority == null) { SolrRequestInfo info = SolrRequestInfo.getRequestInfo(); if(info != null) { Map context = info.getReq().getContext(); this.boostedPriority = (Map<BytesRef, Integer>)context.get(QueryElevationComponent.BOOSTED_PRIORITY); } } return new ReRankCollector(reRankDocs, length, reRankQuery, reRankWeight, cmd, searcher, boostedPriority); }
public TopDocsCollector getTopDocsCollector(int len, SolrIndexSearcher.QueryCommand cmd, IndexSearcher searcher) throws IOException { if(this.boostedPriority == null) { SolrRequestInfo info = SolrRequestInfo.getRequestInfo(); if(info != null) { Map context = info.getReq().getContext(); this.boostedPriority = (Map<BytesRef, Integer>)context.get(QueryElevationComponent.BOOSTED_PRIORITY); } } return new ReRankCollector(reRankDocs, length, reRankQuery, reRankWeight, cmd, searcher, boostedPriority, scale); }
protected DocList sortDocSet(DocSet set, Sort sort, int nDocs) throws IOException { if (nDocs == 0) { // SOLR-2923 return new DocSlice(0, 0, new int[0], null, 0, 0f); } // bit of a hack to tell if a set is sorted - do it better in the future. boolean inOrder = set instanceof BitDocSet || set instanceof SortedIntDocSet; TopDocsCollector topCollector = TopFieldCollector.create(weightSort(sort), nDocs, false, false, false, inOrder); DocIterator iter = set.iterator(); int base=0; int end=0; int readerIndex = 0; while (iter.hasNext()) { int doc = iter.nextDoc(); while (doc>=end) { AtomicReaderContext leaf = leafContexts.get(readerIndex++); base = leaf.docBase; end = base + leaf.reader().maxDoc(); topCollector.setNextReader(leaf); // we should never need to set the scorer given the settings for the collector } topCollector.collect(doc-base); } TopDocs topDocs = topCollector.topDocs(0, nDocs); int nDocsReturned = topDocs.scoreDocs.length; int[] ids = new int[nDocsReturned]; for (int i=0; i<nDocsReturned; i++) { ScoreDoc scoreDoc = topDocs.scoreDocs[i]; ids[i] = scoreDoc.doc; } return new DocSlice(0,nDocsReturned,ids,null,topDocs.totalHits,0.0f); }
protected DocList sortDocSet(DocSet set, Sort sort, int nDocs) throws IOException { if(nDocs == 0) { // SOLR-2923 return new DocSlice(0, 0, new int[0], null, 0, 0f); } // bit of a hack to tell if a set is sorted - do it better in the future. boolean inOrder = set instanceof BitDocSet || set instanceof SortedIntDocSet; TopDocsCollector topCollector = TopFieldCollector.create(weightSort(sort), nDocs, false, false, false, inOrder); DocIterator iter = set.iterator(); int base = 0; int end = 0; int readerIndex = 0; while(iter.hasNext()) { int doc = iter.nextDoc(); while(doc >= end) { AtomicReaderContext leaf = leafContexts.get(readerIndex++); base = leaf.docBase; end = base + leaf.reader().maxDoc(); topCollector.setNextReader(leaf); // we should never need to set the scorer given the settings for the collector } topCollector.collect(doc - base); } TopDocs topDocs = topCollector.topDocs(0, nDocs); int nDocsReturned = topDocs.scoreDocs.length; int[] ids = new int[nDocsReturned]; for(int i = 0; i < nDocsReturned; i++) { ScoreDoc scoreDoc = topDocs.scoreDocs[i]; ids[i] = scoreDoc.doc; } return new DocSlice(0, nDocsReturned, ids, null, topDocs.totalHits, 0.0f); }
/** * {@inheritDoc} */ @Override protected void finish() throws IOException { TopDocsCollector topDocsCollector = (TopDocsCollector) collector.getDelegate(); TopDocs topDocs = topDocsCollector.topDocs(); GroupDocs<String> groupDocs = new GroupDocs<String>(Float.NaN, topDocs.getMaxScore(), topDocs.totalHits, topDocs.scoreDocs, query.toString(), null); if (main) { mainResult = getDocList(groupDocs); } else { NamedList rsp = commonResponse(); addDocList(rsp, groupDocs); } }
TopDocsAndLeafCollector(TopDocsCollector<?> topLevelCollector) { this.topLevelCollector = topLevelCollector; }
@Override protected TopDocsCollector<ScoreDocKey> createTopDocsCollector(int size) { return new ValuesDiversifiedTopDocsCollector(size, maxDocsPerValue); }
protected TopDocsCollector<? extends ScoreDoc> createTopDocsCollector(int size) throws IOException { return TopScoreDocCollector.create(size); }
public TopDocs topDocs() { if (hitCollector instanceof TopDocsCollector) { return ((TopDocsCollector) hitCollector).topDocs(); } return ((EarlyTerminationCollector) hitCollector).topDocs(); }
protected void sortDocSet(QueryResult qr, QueryCommand cmd) throws IOException { DocSet set = qr.getDocListAndSet().docSet; int nDocs = cmd.getSupersetMaxDoc(); if (nDocs == 0) { // SOLR-2923 qr.getDocListAndSet().docList = new DocSlice(0, 0, new int[0], null, set.size(), 0f); qr.setNextCursorMark(cmd.getCursorMark()); return; } // bit of a hack to tell if a set is sorted - do it better in the future. boolean inOrder = set instanceof BitDocSet || set instanceof SortedIntDocSet; TopDocsCollector topCollector = buildTopDocsCollector(nDocs, cmd); DocIterator iter = set.iterator(); int base=0; int end=0; int readerIndex = 0; while (iter.hasNext()) { int doc = iter.nextDoc(); while (doc>=end) { AtomicReaderContext leaf = leafContexts.get(readerIndex++); base = leaf.docBase; end = base + leaf.reader().maxDoc(); topCollector.setNextReader(leaf); // we should never need to set the scorer given the settings for the collector } topCollector.collect(doc-base); } TopDocs topDocs = topCollector.topDocs(0, nDocs); int nDocsReturned = topDocs.scoreDocs.length; int[] ids = new int[nDocsReturned]; for (int i=0; i<nDocsReturned; i++) { ScoreDoc scoreDoc = topDocs.scoreDocs[i]; ids[i] = scoreDoc.doc; } qr.getDocListAndSet().docList = new DocSlice(0,nDocsReturned,ids,null,topDocs.totalHits,0.0f); populateNextCursorMarkFromTopDocs(qr, cmd, topDocs); }
public TopDocsCollector getTopDocsCollector(int len, SolrIndexSearcher.QueryCommand cmd, IndexSearcher searcher) { if(collector == 0) return new TestCollector(null); else return new TestCollector1(null); }
public TopDocSubCollector(TopDocsCollector<?> docCollector, SuperCollectorType parent) throws IOException { super(docCollector, parent); }