Search in sources :

Example 1 with SubSearchContext

use of org.elasticsearch.search.internal.SubSearchContext in project elasticsearch by elastic.

the class TopHitsAggregatorFactory method createInternal.

@Override
public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
    SubSearchContext subSearchContext = new SubSearchContext(context);
    subSearchContext.parsedQuery(context.parsedQuery());
    subSearchContext.explain(explain);
    subSearchContext.version(version);
    subSearchContext.trackScores(trackScores);
    subSearchContext.from(from);
    subSearchContext.size(size);
    if (sort.isPresent()) {
        subSearchContext.sort(sort.get());
    }
    if (storedFieldsContext != null) {
        subSearchContext.storedFieldsContext(storedFieldsContext);
    }
    if (docValueFields != null) {
        subSearchContext.docValueFieldsContext(new DocValueFieldsContext(docValueFields));
    }
    for (ScriptFieldsContext.ScriptField field : scriptFields) {
        subSearchContext.scriptFields().add(field);
    }
    if (fetchSourceContext != null) {
        subSearchContext.fetchSourceContext(fetchSourceContext);
    }
    if (highlightBuilder != null) {
        subSearchContext.highlight(highlightBuilder.build(context.getQueryShardContext()));
    }
    return new TopHitsAggregator(context.fetchPhase(), subSearchContext, name, context, parent, pipelineAggregators, metaData);
}
Also used : ScriptFieldsContext(org.elasticsearch.search.fetch.subphase.ScriptFieldsContext) DocValueFieldsContext(org.elasticsearch.search.fetch.subphase.DocValueFieldsContext) SubSearchContext(org.elasticsearch.search.internal.SubSearchContext)

Example 2 with SubSearchContext

use of org.elasticsearch.search.internal.SubSearchContext in project elasticsearch by elastic.

the class PercolatorHighlightSubFetchPhase method createSubSearchContext.

private SubSearchContext createSubSearchContext(SearchContext context, LeafReaderContext leafReaderContext, BytesReference source) {
    SubSearchContext subSearchContext = new SubSearchContext(context);
    subSearchContext.highlight(new SearchContextHighlight(context.highlight().fields()));
    // Enforce highlighting by source, because MemoryIndex doesn't support stored fields.
    subSearchContext.highlight().globalForceSource(true);
    subSearchContext.lookup().source().setSegmentAndDocument(leafReaderContext, 0);
    subSearchContext.lookup().source().setSource(source);
    return subSearchContext;
}
Also used : SearchContextHighlight(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight) SubSearchContext(org.elasticsearch.search.internal.SubSearchContext)

Example 3 with SubSearchContext

use of org.elasticsearch.search.internal.SubSearchContext in project elasticsearch by elastic.

the class PercolatorHighlightSubFetchPhase method hitsExecute.

@Override
public void hitsExecute(SearchContext context, SearchHit[] hits) {
    if (hitsExecutionNeeded(context) == false) {
        return;
    }
    PercolateQuery percolateQuery = locatePercolatorQuery(context.query());
    if (percolateQuery == null) {
        // shouldn't happen as we checked for the existence of a percolator query in hitsExecutionNeeded(...)
        throw new IllegalStateException("couldn't locate percolator query");
    }
    List<LeafReaderContext> ctxs = context.searcher().getIndexReader().leaves();
    IndexSearcher percolatorIndexSearcher = percolateQuery.getPercolatorIndexSearcher();
    PercolateQuery.QueryStore queryStore = percolateQuery.getQueryStore();
    LeafReaderContext percolatorLeafReaderContext = percolatorIndexSearcher.getIndexReader().leaves().get(0);
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    SubSearchContext subSearchContext = createSubSearchContext(context, percolatorLeafReaderContext, percolateQuery.getDocumentSource());
    for (SearchHit hit : hits) {
        final Query query;
        try {
            LeafReaderContext ctx = ctxs.get(ReaderUtil.subIndex(hit.docId(), ctxs));
            int segmentDocId = hit.docId() - ctx.docBase;
            query = queryStore.getQueries(ctx).apply(segmentDocId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (query != null) {
            subSearchContext.parsedQuery(new ParsedQuery(query));
            hitContext.reset(new SearchHit(0, "unknown", new Text(percolateQuery.getDocumentType()), Collections.emptyMap()), percolatorLeafReaderContext, 0, percolatorIndexSearcher);
            hitContext.cache().clear();
            super.hitExecute(subSearchContext, hitContext);
            hit.getHighlightFields().putAll(hitContext.hit().getHighlightFields());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SearchHit(org.elasticsearch.search.SearchHit) Query(org.apache.lucene.search.Query) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) Text(org.elasticsearch.common.text.Text) IOException(java.io.IOException) SubSearchContext(org.elasticsearch.search.internal.SubSearchContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase)

Aggregations

SubSearchContext (org.elasticsearch.search.internal.SubSearchContext)3 IOException (java.io.IOException)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 FunctionScoreQuery (org.elasticsearch.common.lucene.search.function.FunctionScoreQuery)1 Text (org.elasticsearch.common.text.Text)1 ParsedQuery (org.elasticsearch.index.query.ParsedQuery)1 SearchHit (org.elasticsearch.search.SearchHit)1 FetchSubPhase (org.elasticsearch.search.fetch.FetchSubPhase)1 DocValueFieldsContext (org.elasticsearch.search.fetch.subphase.DocValueFieldsContext)1 ScriptFieldsContext (org.elasticsearch.search.fetch.subphase.ScriptFieldsContext)1 SearchContextHighlight (org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight)1