Search in sources :

Example 16 with DocIterator

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

the class SolrPluginUtils method optimizePreFetchDocs.

/**
   * Pre-fetch documents into the index searcher's document cache.
   *
   * This is an entirely optional step which you might want to perform for
   * the following reasons:
   *
   * <ul>
   *     <li>Locates the document-retrieval costs in one spot, which helps
   *     detailed performance measurement</li>
   *
   *     <li>Determines a priori what fields will be needed to be fetched by
   *     various subtasks, like response writing and highlighting.  This
   *     minimizes the chance that many needed fields will be loaded lazily.
   *     (it is more efficient to load all the field we require normally).</li>
   * </ul>
   *
   * If lazy field loading is disabled, this method does nothing.
   */
public static void optimizePreFetchDocs(ResponseBuilder rb, DocList docs, Query query, SolrQueryRequest req, SolrQueryResponse res) throws IOException {
    SolrIndexSearcher searcher = req.getSearcher();
    if (!searcher.getDocFetcher().isLazyFieldLoadingEnabled()) {
        // nothing to do
        return;
    }
    ReturnFields returnFields = res.getReturnFields();
    if (returnFields.getLuceneFieldNames() != null) {
        Set<String> fieldFilter = returnFields.getLuceneFieldNames();
        if (rb.doHighlights) {
            // copy return fields list
            fieldFilter = new HashSet<>(fieldFilter);
            // add highlight fields
            SolrHighlighter highlighter = HighlightComponent.getHighlighter(req.getCore());
            for (String field : highlighter.getHighlightFields(query, req, null)) fieldFilter.add(field);
            // fetch unique key if one exists.
            SchemaField keyField = searcher.getSchema().getUniqueKeyField();
            if (null != keyField)
                fieldFilter.add(keyField.getName());
        }
        // get documents
        DocIterator iter = docs.iterator();
        for (int i = 0; i < docs.size(); i++) {
            searcher.doc(iter.nextDoc(), fieldFilter);
        }
    }
}
Also used : SolrHighlighter(org.apache.solr.highlight.SolrHighlighter) SchemaField(org.apache.solr.schema.SchemaField) DocIterator(org.apache.solr.search.DocIterator) ReturnFields(org.apache.solr.search.ReturnFields) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Example 17 with DocIterator

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

the class TermVectorReusingLeafReader method doHighlighting.

/**
   * Generates a list of Highlighted query fragments for each item in a list
   * of documents, or returns null if highlighting is disabled.
   *
   * @param docs query results
   * @param query the query
   * @param req the current request
   * @param defaultFields default list of fields to summarize
   *
   * @return NamedList containing a NamedList for each document, which in 
   * turns contains sets (field, summary) pairs.
   */
@Override
@SuppressWarnings("unchecked")
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields) throws IOException {
    SolrParams params = req.getParams();
    if (// also returns early if no unique key field
    !isHighlightingEnabled(params))
        return null;
    boolean rewrite = query != null && !(Boolean.valueOf(params.get(HighlightParams.USE_PHRASE_HIGHLIGHTER, "true")) && Boolean.valueOf(params.get(HighlightParams.HIGHLIGHT_MULTI_TERM, "true")));
    if (rewrite) {
        query = query.rewrite(req.getSearcher().getIndexReader());
    }
    SolrIndexSearcher searcher = req.getSearcher();
    IndexSchema schema = searcher.getSchema();
    // fetch unique key if one exists.
    SchemaField keyField = schema.getUniqueKeyField();
    if (keyField == null) {
        //exit early; we need a unique key field to populate the response
        return null;
    }
    String[] fieldNames = getHighlightFields(query, req, defaultFields);
    Set<String> preFetchFieldNames = getDocPrefetchFieldNames(fieldNames, req);
    if (preFetchFieldNames != null) {
        preFetchFieldNames.add(keyField.getName());
    }
    // Lazy container for fvh and fieldQuery
    FvhContainer fvhContainer = new FvhContainer(null, null);
    // SOLR-5855
    IndexReader reader = new TermVectorReusingLeafReader(req.getSearcher().getSlowAtomicReader());
    // Highlight each document
    NamedList fragments = new SimpleOrderedMap();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        int docId = iterator.nextDoc();
        Document doc = searcher.doc(docId, preFetchFieldNames);
        @SuppressWarnings("rawtypes") NamedList docHighlights = new SimpleOrderedMap();
        // Highlight per-field
        for (String fieldName : fieldNames) {
            SchemaField schemaField = schema.getFieldOrNull(fieldName);
            // object type allows flexibility for subclassers
            Object fieldHighlights;
            fieldHighlights = doHighlightingOfField(doc, docId, schemaField, fvhContainer, query, reader, req, params);
            if (fieldHighlights == null) {
                fieldHighlights = alternateField(doc, docId, fieldName, fvhContainer, query, reader, req);
            }
            if (fieldHighlights != null) {
                docHighlights.add(fieldName, fieldHighlights);
            }
        }
        // for each field
        fragments.add(schema.printableUniqueKey(doc), docHighlights);
    }
    // for each doc
    return fragments;
}
Also used : DocIterator(org.apache.solr.search.DocIterator) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Document(org.apache.lucene.document.Document) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SchemaField(org.apache.solr.schema.SchemaField) IndexReader(org.apache.lucene.index.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) IndexSchema(org.apache.solr.schema.IndexSchema)

Example 18 with DocIterator

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

the class BlockJoin method toParents.

/** childInput may also contain parents (i.e. a parent or below will all roll up to that parent) */
public static DocSet toParents(DocSet childInput, BitDocSet parentList, QueryContext qcontext) throws IOException {
    FixedBitSet parentBits = parentList.getBits();
    DocSetCollector collector = new DocSetCollector(qcontext.searcher().maxDoc());
    DocIterator iter = childInput.iterator();
    int currentParent = -1;
    while (iter.hasNext()) {
        // TODO: skipping
        int childDoc = iter.nextDoc();
        if (childDoc <= currentParent) {
            // we already visited this parent
            continue;
        }
        currentParent = parentBits.nextSetBit(childDoc);
        if (currentParent != DocIdSetIterator.NO_MORE_DOCS) {
            // only collect the parent the first time we skip to it
            collector.collect(currentParent);
        }
    }
    return collector.getDocSet();
}
Also used : DocIterator(org.apache.solr.search.DocIterator) FixedBitSet(org.apache.lucene.util.FixedBitSet) DocSetCollector(org.apache.solr.search.DocSetCollector)

Example 19 with DocIterator

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

the class BlockJoin method toChildren.

/** acceptDocs will normally be used to avoid deleted documents from being generated as part of the answer DocSet (just use *:*)
   *  although it can be used to further constrain the generated documents.
   */
public static DocSet toChildren(DocSet parentInput, BitDocSet parentList, DocSet acceptDocs, QueryContext qcontext) throws IOException {
    FixedBitSet parentBits = parentList.getBits();
    DocSetCollector collector = new DocSetCollector(qcontext.searcher().maxDoc());
    DocIterator iter = parentInput.iterator();
    while (iter.hasNext()) {
        int parentDoc = iter.nextDoc();
        if (!parentList.exists(parentDoc) || parentDoc == 0) {
            // not a parent, or parent has no children
            continue;
        }
        int prevParent = parentBits.prevSetBit(parentDoc - 1);
        for (int childDoc = prevParent + 1; childDoc < parentDoc; childDoc++) {
            // only select live docs
            if (acceptDocs != null && !acceptDocs.exists(childDoc))
                continue;
            collector.collect(childDoc);
        }
    }
    return collector.getDocSet();
}
Also used : DocIterator(org.apache.solr.search.DocIterator) FixedBitSet(org.apache.lucene.util.FixedBitSet) DocSetCollector(org.apache.solr.search.DocSetCollector)

Example 20 with DocIterator

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

the class SortSlotAcc method collect.

public int collect(DocSet docs, int slot) throws IOException {
    int count = 0;
    SolrIndexSearcher searcher = fcontext.searcher;
    final List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
    final Iterator<LeafReaderContext> ctxIt = leaves.iterator();
    LeafReaderContext ctx = null;
    int segBase = 0;
    int segMax;
    int adjustedMax = 0;
    for (DocIterator docsIt = docs.iterator(); docsIt.hasNext(); ) {
        final int doc = docsIt.nextDoc();
        if (doc >= adjustedMax) {
            do {
                ctx = ctxIt.next();
                if (ctx == null) {
                    // should be impossible
                    throw new RuntimeException("INTERNAL FACET ERROR");
                }
                segBase = ctx.docBase;
                segMax = ctx.reader().maxDoc();
                adjustedMax = segBase + segMax;
            } while (doc >= adjustedMax);
            assert doc >= ctx.docBase;
            setNextReader(ctx);
        }
        count++;
        // per-seg collectors
        collect(doc - segBase, slot);
    }
    return count;
}
Also used : DocIterator(org.apache.solr.search.DocIterator) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Aggregations

DocIterator (org.apache.solr.search.DocIterator)27 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)10 SchemaField (org.apache.solr.schema.SchemaField)9 DocList (org.apache.solr.search.DocList)8 NamedList (org.apache.solr.common.util.NamedList)7 FieldType (org.apache.solr.schema.FieldType)7 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)7 Document (org.apache.lucene.document.Document)6 IOException (java.io.IOException)5 SolrParams (org.apache.solr.common.params.SolrParams)5 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)5 ArrayList (java.util.ArrayList)4 IndexableField (org.apache.lucene.index.IndexableField)4 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)4 FixedBitSet (org.apache.lucene.util.FixedBitSet)4 NumericDocValues (org.apache.lucene.index.NumericDocValues)3 Query (org.apache.lucene.search.Query)3 Sort (org.apache.lucene.search.Sort)3 SolrException (org.apache.solr.common.SolrException)3 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)3