Search in sources :

Example 1 with Rescorer

use of org.elasticsearch.search.rescore.Rescorer in project elasticsearch by elastic.

the class TransportExplainAction method shardOperation.

@Override
protected ExplainResponse shardOperation(ExplainRequest request, ShardId shardId) throws IOException {
    ShardSearchLocalRequest shardSearchLocalRequest = new ShardSearchLocalRequest(shardId, new String[] { request.type() }, request.nowInMillis, request.filteringAlias());
    SearchContext context = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT, null);
    Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
    Engine.GetResult result = null;
    try {
        result = context.indexShard().get(new Engine.Get(false, uidTerm));
        if (!result.exists()) {
            return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), false);
        }
        context.parsedQuery(context.getQueryShardContext().toQuery(request.query()));
        context.preProcess(true);
        int topLevelDocId = result.docIdAndVersion().docId + result.docIdAndVersion().context.docBase;
        Explanation explanation = context.searcher().explain(context.query(), topLevelDocId);
        for (RescoreSearchContext ctx : context.rescore()) {
            Rescorer rescorer = ctx.rescorer();
            explanation = rescorer.explain(topLevelDocId, context, ctx, explanation);
        }
        if (request.storedFields() != null || (request.fetchSourceContext() != null && request.fetchSourceContext().fetchSource())) {
            // Advantage is that we're not opening a second searcher to retrieve the _source. Also
            // because we are working in the same searcher in engineGetResult we can be sure that a
            // doc isn't deleted between the initial get and this call.
            GetResult getResult = context.indexShard().getService().get(result, request.id(), request.type(), request.storedFields(), request.fetchSourceContext());
            return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), true, explanation, getResult);
        } else {
            return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), true, explanation);
        }
    } catch (IOException e) {
        throw new ElasticsearchException("Could not explain", e);
    } finally {
        Releasables.close(result, context);
    }
}
Also used : ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) GetResult(org.elasticsearch.index.get.GetResult) Explanation(org.apache.lucene.search.Explanation) SearchContext(org.elasticsearch.search.internal.SearchContext) RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) Rescorer(org.elasticsearch.search.rescore.Rescorer) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) Engine(org.elasticsearch.index.engine.Engine)

Aggregations

IOException (java.io.IOException)1 Term (org.apache.lucene.index.Term)1 Explanation (org.apache.lucene.search.Explanation)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Engine (org.elasticsearch.index.engine.Engine)1 GetResult (org.elasticsearch.index.get.GetResult)1 SearchContext (org.elasticsearch.search.internal.SearchContext)1 ShardSearchLocalRequest (org.elasticsearch.search.internal.ShardSearchLocalRequest)1 RescoreSearchContext (org.elasticsearch.search.rescore.RescoreSearchContext)1 Rescorer (org.elasticsearch.search.rescore.Rescorer)1