Search in sources :

Example 51 with Explanation

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

the class RandomScoreFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) {
    AtomicFieldData leafData = uidFieldData.load(ctx);
    final SortedBinaryDocValues uidByteData = leafData.getBytesValues();
    if (uidByteData == null)
        throw new NullPointerException("failed to get uid byte data");
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) {
            uidByteData.setDocument(docId);
            int hash = StringHelper.murmurhash3_x86_32(uidByteData.valueAt(0), saltedSeed);
            // only use the lower 24 bits to construct a float from 0.0-1.0
            return (hash & 0x00FFFFFF) / (float) (1 << 24);
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) {
            return Explanation.match(CombineFunction.toFloat(score(docId, subQueryScore.getValue())), "random score function (seed: " + originalSeed + ")");
        }
    };
}
Also used : Explanation(org.apache.lucene.search.Explanation) AtomicFieldData(org.elasticsearch.index.fielddata.AtomicFieldData) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Example 52 with Explanation

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

the class FieldValueFactorFunction method getLeafScoreFunction.

@Override
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) {
    final SortedNumericDoubleValues values;
    if (indexFieldData == null) {
        values = FieldData.emptySortedNumericDoubles(ctx.reader().maxDoc());
    } else {
        values = this.indexFieldData.load(ctx).getDoubleValues();
    }
    return new LeafScoreFunction() {

        @Override
        public double score(int docId, float subQueryScore) {
            values.setDocument(docId);
            final int numValues = values.count();
            double value;
            if (numValues > 0) {
                value = values.valueAt(0);
            } else if (missing != null) {
                value = missing;
            } else {
                throw new ElasticsearchException("Missing value for field [" + field + "]");
            }
            double val = value * boostFactor;
            double result = modifier.apply(val);
            if (Double.isNaN(result) || Double.isInfinite(result)) {
                throw new ElasticsearchException("Result of field modification [" + modifier.toString() + "(" + val + ")] must be a number");
            }
            return result;
        }

        @Override
        public Explanation explainScore(int docId, Explanation subQueryScore) {
            String modifierStr = modifier != null ? modifier.toString() : "";
            String defaultStr = missing != null ? "?:" + missing : "";
            double score = score(docId, subQueryScore.getValue());
            return Explanation.match(CombineFunction.toFloat(score), String.format(Locale.ROOT, "field value function: %s(doc['%s'].value%s * factor=%s)", modifierStr, field, defaultStr, boostFactor));
        }
    };
}
Also used : Explanation(org.apache.lucene.search.Explanation) ElasticsearchException(org.elasticsearch.ElasticsearchException) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 53 with Explanation

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

the class FunctionScoreTests method getFunctionScoreExplanation.

public Explanation getFunctionScoreExplanation(IndexSearcher searcher, ScoreFunction scoreFunction) throws IOException {
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(new TermQuery(TERM), scoreFunction, 0.0f, CombineFunction.AVG, 100);
    Weight weight = searcher.createNormalizedWeight(functionScoreQuery, true);
    Explanation explanation = weight.explain(searcher.getIndexReader().leaves().get(0), 0);
    return explanation.getDetails()[1];
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) Weight(org.apache.lucene.search.Weight)

Example 54 with Explanation

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

the class FunctionScoreTests method checkFiltersFunctionScoreExplanation.

public void checkFiltersFunctionScoreExplanation(Explanation randomExplanation, String functionExpl, int whichFunction) {
    assertThat(randomExplanation.getDescription(), equalTo("min of:"));
    Explanation explanation = randomExplanation.getDetails()[0];
    assertThat(explanation.getDescription(), equalTo("function score, score mode [avg]"));
    Explanation functionExplanation = randomExplanation.getDetails()[0].getDetails()[whichFunction];
    assertThat(functionExplanation.getDescription(), equalTo("function score, product of:"));
    assertThat(functionExplanation.getDetails()[0].getDescription(), equalTo("match filter: " + FIELD + ":" + TERM.text()));
    assertThat(functionExplanation.getDetails()[1].getDescription(), equalTo(functionExpl));
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Example 55 with Explanation

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

the class FunctionScoreTests method testExplainFunctionScoreQuery.

public void testExplainFunctionScoreQuery() throws IOException {
    Explanation functionExplanation = getFunctionScoreExplanation(searcher, RANDOM_SCORE_FUNCTION);
    checkFunctionScoreExplanation(functionExplanation, "random score function (seed: 0)");
    assertThat(functionExplanation.getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, FIELD_VALUE_FACTOR_FUNCTION);
    checkFunctionScoreExplanation(functionExplanation, "field value function: ln(doc['test'].value?:1.0 * factor=1.0)");
    assertThat(functionExplanation.getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, GAUSS_DECAY_FUNCTION);
    checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = exp(-0.5*pow(MAX[Math.max(Math.abs" + "(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)],2.0)/0.21714724095162594)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, EXP_DECAY_FUNCTION);
    checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = exp(- MAX[Math.max(Math.abs(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)] * 2.3025850929940455)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, LIN_DECAY_FUNCTION);
    checkFunctionScoreExplanation(functionExplanation, "Function for field test:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("0.1 = max(0.0, ((1.1111111111111112" + " - MAX[Math.max(Math.abs(1.0(=doc value) - 0.0(=origin))) - 0.0(=offset), 0)])/1.1111111111111112)\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFunctionScoreExplanation(searcher, WEIGHT_FACTOR_FUNCTION);
    checkFunctionScoreExplanation(functionExplanation, "product of:");
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].toString(), equalTo("1.0 = constant score 1.0 - no function provided\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[1].toString(), equalTo("4.0 = weight\n"));
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails().length, equalTo(0));
    assertThat(functionExplanation.getDetails()[0].getDetails()[1].getDetails().length, equalTo(0));
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Aggregations

Explanation (org.apache.lucene.search.Explanation)77 TermQuery (org.apache.lucene.search.TermQuery)16 ArrayList (java.util.ArrayList)13 Query (org.apache.lucene.search.Query)13 IndexSearcher (org.apache.lucene.search.IndexSearcher)12 Term (org.apache.lucene.index.Term)11 IOException (java.io.IOException)9 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 TopDocs (org.apache.lucene.search.TopDocs)7 IndexReader (org.apache.lucene.index.IndexReader)6 IndexWriter (org.apache.lucene.index.IndexWriter)6 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)6 Collectors (java.util.stream.Collectors)5 Collections (java.util.Collections)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 FunctionValues (org.apache.lucene.queries.function.FunctionValues)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)4