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