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);
}
}
}
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;
}
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();
}
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();
}
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;
}
Aggregations