Search in sources :

Example 11 with Explanation

use of org.apache.lucene.search.Explanation in project elasticsearch by elastic.

the class ScriptScoreFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) throws IOException {
    final LeafSearchScript leafScript = script.getLeafSearchScript(ctx);
    final CannedScorer scorer = new CannedScorer();
    leafScript.setScorer(scorer);
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) {
            leafScript.setDocument(docId);
            scorer.docid = docId;
            scorer.score = subQueryScore;
            double result = leafScript.runAsDouble();
            if (Double.isNaN(result)) {
                throw new GeneralScriptException("script_score returned NaN");
            }
            return result;
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) throws IOException {
            Explanation exp;
            if (leafScript instanceof ExplainableSearchScript) {
                leafScript.setDocument(docId);
                scorer.docid = docId;
                scorer.score = subQueryScore.getValue();
                exp = ((ExplainableSearchScript) leafScript).explain(subQueryScore);
            } else {
                double score = score(docId, subQueryScore.getValue());
                String explanation = "script score function, computed with script:\"" + sScript + "\"";
                if (sScript.getParams() != null) {
                    explanation += " and parameters: \n" + sScript.getParams().toString();
                }
                Explanation scoreExp = Explanation.match(subQueryScore.getValue(), "_score: ", subQueryScore);
                return Explanation.match(CombineFunction.toFloat(score), explanation, scoreExp);
            }
            return exp;
        }
    };
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) Explanation(org.apache.lucene.search.Explanation) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript)

Example 12 with Explanation

use of org.apache.lucene.search.Explanation in project elasticsearch by elastic.

the class Lucene method writeExplanation.

public static void writeExplanation(StreamOutput out, Explanation explanation) throws IOException {
    out.writeBoolean(explanation.isMatch());
    out.writeString(explanation.getDescription());
    Explanation[] subExplanations = explanation.getDetails();
    out.writeVInt(subExplanations.length);
    for (Explanation subExp : subExplanations) {
        writeExplanation(out, subExp);
    }
    if (explanation.isMatch()) {
        out.writeFloat(explanation.getValue());
    }
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Example 13 with Explanation

use of org.apache.lucene.search.Explanation in project elasticsearch by elastic.

the class ExplainFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.explain() == false) {
        return;
    }
    try {
        final int topLevelDocId = hitContext.hit().docId();
        Explanation explanation = context.searcher().explain(context.query(), topLevelDocId);
        for (RescoreSearchContext rescore : context.rescore()) {
            explanation = rescore.rescorer().explain(topLevelDocId, context, rescore, explanation);
        }
        // we use the top level doc id, since we work with the top level searcher
        hitContext.hit().explanation(explanation);
    } catch (IOException e) {
        throw new FetchPhaseExecutionException(context, "Failed to explain doc [" + hitContext.hit().getType() + "#" + hitContext.hit().getId() + "]", e);
    } finally {
        context.clearReleasables(SearchContext.Lifetime.COLLECTION);
    }
}
Also used : RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) FetchPhaseExecutionException(org.elasticsearch.search.fetch.FetchPhaseExecutionException) Explanation(org.apache.lucene.search.Explanation) IOException(java.io.IOException)

Example 14 with Explanation

use of org.apache.lucene.search.Explanation 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)

Example 15 with Explanation

use of org.apache.lucene.search.Explanation in project elasticsearch by elastic.

the class RestExplainAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ExplainRequest explainRequest = new ExplainRequest(request.param("index"), request.param("type"), request.param("id"));
    explainRequest.parent(request.param("parent"));
    explainRequest.routing(request.param("routing"));
    explainRequest.preference(request.param("preference"));
    String queryString = request.param("q");
    request.withContentOrSourceParamParserOrNull(parser -> {
        if (parser != null) {
            explainRequest.query(RestActions.getQueryContent(parser));
        } else if (queryString != null) {
            QueryBuilder query = RestActions.urlParamsToQueryBuilder(request);
            explainRequest.query(query);
        }
    });
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("The parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields");
    }
    String sField = request.param("stored_fields");
    if (sField != null) {
        String[] sFields = Strings.splitStringByCommaToArray(sField);
        if (sFields != null) {
            explainRequest.storedFields(sFields);
        }
    }
    explainRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> client.explain(explainRequest, new RestBuilderListener<ExplainResponse>(channel) {

        @Override
        public RestResponse buildResponse(ExplainResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field(Fields._INDEX, response.getIndex()).field(Fields._TYPE, response.getType()).field(Fields._ID, response.getId()).field(Fields.MATCHED, response.isMatch());
            if (response.hasExplanation()) {
                builder.startObject(Fields.EXPLANATION);
                buildExplanation(builder, response.getExplanation());
                builder.endObject();
            }
            GetResult getResult = response.getGetResult();
            if (getResult != null) {
                builder.startObject(Fields.GET);
                response.getGetResult().toXContentEmbedded(builder, request);
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(response.isExists() ? OK : NOT_FOUND, builder);
        }

        private void buildExplanation(XContentBuilder builder, Explanation explanation) throws IOException {
            builder.field(Fields.VALUE, explanation.getValue());
            builder.field(Fields.DESCRIPTION, explanation.getDescription());
            Explanation[] innerExps = explanation.getDetails();
            if (innerExps != null) {
                builder.startArray(Fields.DETAILS);
                for (Explanation exp : innerExps) {
                    builder.startObject();
                    buildExplanation(builder, exp);
                    builder.endObject();
                }
                builder.endArray();
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Explanation(org.apache.lucene.search.Explanation) GET(org.elasticsearch.rest.RestRequest.Method.GET) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) RestResponse(org.elasticsearch.rest.RestResponse) GetResult(org.elasticsearch.index.get.GetResult) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) GetResult(org.elasticsearch.index.get.GetResult) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Explanation(org.apache.lucene.search.Explanation) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) IOException(java.io.IOException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

Explanation (org.apache.lucene.search.Explanation)66 TermQuery (org.apache.lucene.search.TermQuery)15 Query (org.apache.lucene.search.Query)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)13 ArrayList (java.util.ArrayList)12 Term (org.apache.lucene.index.Term)10 IOException (java.io.IOException)8 BooleanQuery (org.apache.lucene.search.BooleanQuery)8 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 Directory (org.apache.lucene.store.Directory)8 Document (org.apache.lucene.document.Document)7 IndexReader (org.apache.lucene.index.IndexReader)7 TopDocs (org.apache.lucene.search.TopDocs)7 IndexWriter (org.apache.lucene.index.IndexWriter)6 DirectoryReader (org.apache.lucene.index.DirectoryReader)5 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)5 Collectors (java.util.stream.Collectors)4 IntStream (java.util.stream.IntStream)4 Store (org.apache.lucene.document.Field.Store)4 FunctionValues (org.apache.lucene.queries.function.FunctionValues)4