Search in sources :

Example 6 with Explanation

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

the class SimpleAllTests method assertExplanationScore.

private void assertExplanationScore(IndexSearcher searcher, Query query, ScoreDoc scoreDoc) throws IOException {
    final Explanation expl = searcher.explain(query, scoreDoc.doc);
    assertEquals(scoreDoc.score, expl.getValue(), 0.00001f);
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Example 7 with Explanation

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

the class ExplainActionIT method testStreamExplain.

public void testStreamExplain() throws Exception {
    Explanation exp = Explanation.match(2f, "some explanation");
    // write
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
    Lucene.writeExplanation(out, exp);
    // read
    ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer);
    Explanation result = Lucene.readExplanation(esBuffer);
    assertThat(exp.toString(), equalTo(result.toString()));
    exp = Explanation.match(2.0f, "some explanation", Explanation.match(2.0f, "another explanation"));
    // write complex
    outBuffer = new ByteArrayOutputStream();
    out = new OutputStreamStreamOutput(outBuffer);
    Lucene.writeExplanation(out, exp);
    // read complex
    esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    esBuffer = new InputStreamStreamInput(esInBuffer);
    result = Lucene.readExplanation(esBuffer);
    assertThat(exp.toString(), equalTo(result.toString()));
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Explanation(org.apache.lucene.search.Explanation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 8 with Explanation

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

the class FunctionScoreTests method testExplainFiltersFunctionScoreQuery.

public void testExplainFiltersFunctionScoreQuery() throws IOException {
    Explanation functionExplanation = getFiltersFunctionScoreExplanation(searcher, RANDOM_SCORE_FUNCTION);
    checkFiltersFunctionScoreExplanation(functionExplanation, "random score function (seed: 0)", 0);
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails()[1].getDetails().length, equalTo(0));
    functionExplanation = getFiltersFunctionScoreExplanation(searcher, FIELD_VALUE_FACTOR_FUNCTION);
    checkFiltersFunctionScoreExplanation(functionExplanation, "field value function: ln(doc['test'].value?:1.0 * factor=1.0)", 0);
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails()[1].getDetails().length, equalTo(0));
    functionExplanation = getFiltersFunctionScoreExplanation(searcher, GAUSS_DECAY_FUNCTION);
    checkFiltersFunctionScoreExplanation(functionExplanation, "Function for field test:", 0);
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails()[1].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()[1].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFiltersFunctionScoreExplanation(searcher, EXP_DECAY_FUNCTION);
    checkFiltersFunctionScoreExplanation(functionExplanation, "Function for field test:", 0);
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails()[1].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()[1].getDetails()[0].getDetails().length, equalTo(0));
    functionExplanation = getFiltersFunctionScoreExplanation(searcher, LIN_DECAY_FUNCTION);
    checkFiltersFunctionScoreExplanation(functionExplanation, "Function for field test:", 0);
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails()[1].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()[1].getDetails()[0].getDetails().length, equalTo(0));
    // now test all together
    functionExplanation = getFiltersFunctionScoreExplanation(searcher, RANDOM_SCORE_FUNCTION, FIELD_VALUE_FACTOR_FUNCTION, GAUSS_DECAY_FUNCTION, EXP_DECAY_FUNCTION, LIN_DECAY_FUNCTION);
    checkFiltersFunctionScoreExplanation(functionExplanation, "random score function (seed: 0)", 0);
    assertThat(functionExplanation.getDetails()[0].getDetails()[0].getDetails()[1].getDetails().length, equalTo(0));
    checkFiltersFunctionScoreExplanation(functionExplanation, "field value function: ln(doc['test'].value?:1.0 * factor=1.0)", 1);
    assertThat(functionExplanation.getDetails()[0].getDetails()[1].getDetails()[1].getDetails().length, equalTo(0));
    checkFiltersFunctionScoreExplanation(functionExplanation, "Function for field test:", 2);
    assertThat(functionExplanation.getDetails()[0].getDetails()[2].getDetails()[1].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()[2].getDetails()[1].getDetails()[0].getDetails().length, equalTo(0));
    checkFiltersFunctionScoreExplanation(functionExplanation, "Function for field test:", 3);
    assertThat(functionExplanation.getDetails()[0].getDetails()[3].getDetails()[1].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()[3].getDetails()[1].getDetails()[0].getDetails().length, equalTo(0));
    checkFiltersFunctionScoreExplanation(functionExplanation, "Function for field test:", 4);
    assertThat(functionExplanation.getDetails()[0].getDetails()[4].getDetails()[1].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()[4].getDetails()[1].getDetails()[0].getDetails().length, equalTo(0));
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Example 9 with Explanation

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

the class FunctionScoreTests method testMinScoreExplain.

public void testMinScoreExplain() throws IOException {
    Query query = new MatchAllDocsQuery();
    Explanation queryExpl = searcher.explain(query, 0);
    FunctionScoreQuery fsq = new FunctionScoreQuery(query, null, 0f, null, Float.POSITIVE_INFINITY);
    Explanation fsqExpl = searcher.explain(fsq, 0);
    assertTrue(fsqExpl.isMatch());
    assertEquals(queryExpl.getValue(), fsqExpl.getValue(), 0f);
    assertEquals(queryExpl.getDescription(), fsqExpl.getDescription());
    fsq = new FunctionScoreQuery(query, null, 10f, null, Float.POSITIVE_INFINITY);
    fsqExpl = searcher.explain(fsq, 0);
    assertFalse(fsqExpl.isMatch());
    assertEquals("Score value is too low, expected at least 10.0 but got 1.0", fsqExpl.getDescription());
    FiltersFunctionScoreQuery ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, 0f, CombineFunction.MULTIPLY);
    Explanation ffsqExpl = searcher.explain(ffsq, 0);
    assertTrue(ffsqExpl.isMatch());
    assertEquals(queryExpl.getValue(), ffsqExpl.getValue(), 0f);
    assertEquals(queryExpl.getDescription(), ffsqExpl.getDetails()[0].getDescription());
    ffsq = new FiltersFunctionScoreQuery(query, ScoreMode.SUM, new FilterFunction[0], Float.POSITIVE_INFINITY, 10f, CombineFunction.MULTIPLY);
    ffsqExpl = searcher.explain(ffsq, 0);
    assertFalse(ffsqExpl.isMatch());
    assertEquals("Score value is too low, expected at least 10.0 but got 1.0", ffsqExpl.getDescription());
}
Also used : FilterFunction(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) RandomApproximationQuery(org.apache.lucene.search.RandomApproximationQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 10 with Explanation

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

the class FunctionScoreTests method testExplanationAndScoreEqualsEvenIfNoFunctionMatches.

public void testExplanationAndScoreEqualsEvenIfNoFunctionMatches() throws IOException {
    IndexSearcher localSearcher = newSearcher(reader);
    ScoreMode scoreMode = randomFrom(new ScoreMode[] { ScoreMode.SUM, ScoreMode.AVG, ScoreMode.FIRST, ScoreMode.MIN, ScoreMode.MAX, ScoreMode.MULTIPLY });
    CombineFunction combineFunction = randomFrom(new CombineFunction[] { CombineFunction.SUM, CombineFunction.AVG, CombineFunction.MIN, CombineFunction.MAX, CombineFunction.MULTIPLY, CombineFunction.REPLACE });
    // check for document that has no macthing function
    FiltersFunctionScoreQuery query = new FiltersFunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), scoreMode, new FilterFunction[] { new FilterFunction(new TermQuery(new Term("_uid", "2")), new WeightFactorFunction(10)) }, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, combineFunction);
    TopDocs searchResult = localSearcher.search(query, 1);
    Explanation explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
    assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
    // check for document that has a matching function
    query = new FiltersFunctionScoreQuery(new TermQuery(new Term(FIELD, "out")), scoreMode, new FilterFunction[] { new FilterFunction(new TermQuery(new Term("_uid", "1")), new WeightFactorFunction(10)) }, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, combineFunction);
    searchResult = localSearcher.search(query, 1);
    explanation = localSearcher.explain(query, searchResult.scoreDocs[0].doc);
    assertThat(searchResult.scoreDocs[0].score, equalTo(explanation.getValue()));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) TopDocs(org.apache.lucene.search.TopDocs) ScoreMode(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.ScoreMode) TermQuery(org.apache.lucene.search.TermQuery) WeightFactorFunction(org.elasticsearch.common.lucene.search.function.WeightFactorFunction) FilterFunction(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery.FilterFunction) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term)

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