Search in sources :

Example 16 with Explanation

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

the class SimpleNestedIT method testExplain.

public void testExplain() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").endObject().endObject().endObject().endObject()));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", "value1").startArray("nested1").startObject().field("n_field1", "n_value1").endObject().startObject().field("n_field1", "n_value1").endObject().endArray().endObject()).setRefreshPolicy(IMMEDIATE).execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1"), ScoreMode.Total)).setExplain(true).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    Explanation explanation = searchResponse.getHits().getHits()[0].getExplanation();
    assertThat(explanation.getValue(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertThat(explanation.toString(), startsWith("0.36464313 = Score based on 2 child docs in range from 0 to 1"));
}
Also used : Explanation(org.apache.lucene.search.Explanation) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 17 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class TestSimilarity2 method testNoFieldSkew.

/** make sure scores are not skewed by docs not containing the field */
public void testNoFieldSkew() throws Exception {
    Directory dir = newDirectory();
    // an evil merge policy could reorder our docs for no reason
    IndexWriterConfig iwConfig = newIndexWriterConfig().setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig);
    Document doc = new Document();
    doc.add(newTextField("foo", "bar baz somethingelse", Field.Store.NO));
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    IndexSearcher is = newSearcher(ir);
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    queryBuilder.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
    queryBuilder.add(new TermQuery(new Term("foo", "baz")), BooleanClause.Occur.SHOULD);
    Query query = queryBuilder.build();
    // collect scores
    List<Explanation> scores = new ArrayList<>();
    for (Similarity sim : sims) {
        is.setSimilarity(sim);
        scores.add(is.explain(query, 0));
    }
    ir.close();
    // add some additional docs without the field
    int numExtraDocs = TestUtil.nextInt(random(), 1, 1000);
    for (int i = 0; i < numExtraDocs; i++) {
        iw.addDocument(new Document());
    }
    // check scores are the same
    ir = iw.getReader();
    is = newSearcher(ir);
    for (int i = 0; i < sims.size(); i++) {
        is.setSimilarity(sims.get(i));
        Explanation expected = scores.get(i);
        Explanation actual = is.explain(query, 0);
        assertEquals(sims.get(i).toString() + ": actual=" + actual + ",expected=" + expected, expected.getValue(), actual.getValue(), 0F);
    }
    iw.close();
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Explanation(org.apache.lucene.search.Explanation) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 18 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class SweetSpotSimilarityTest method computeNorm.

private static float computeNorm(Similarity sim, String field, int length) throws IOException {
    String value = IntStream.range(0, length).mapToObj(i -> "a").collect(Collectors.joining(" "));
    Directory dir = new RAMDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim));
    w.addDocument(Collections.singleton(newTextField(field, value, Store.NO)));
    DirectoryReader reader = DirectoryReader.open(w);
    w.close();
    IndexSearcher searcher = new IndexSearcher(reader);
    searcher.setSimilarity(sim);
    Explanation expl = searcher.explain(new TermQuery(new Term(field, "a")), 0);
    reader.close();
    dir.close();
    Explanation norm = findExplanation(expl, "fieldNorm");
    assertNotNull(norm);
    return norm.getValue();
}
Also used : IntStream(java.util.stream.IntStream) Explanation(org.apache.lucene.search.Explanation) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) RAMDirectory(org.apache.lucene.store.RAMDirectory) IOException(java.io.IOException) PerFieldSimilarityWrapper(org.apache.lucene.search.similarities.PerFieldSimilarityWrapper) Collectors(java.util.stream.Collectors) IndexWriter(org.apache.lucene.index.IndexWriter) TermQuery(org.apache.lucene.search.TermQuery) Similarity(org.apache.lucene.search.similarities.Similarity) Directory(org.apache.lucene.store.Directory) Store(org.apache.lucene.document.Field.Store) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) Collections(java.util.Collections) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 19 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class ValueSource method asDoubleValuesSource.

/**
   * Expose this ValueSource as a DoubleValuesSource
   */
public DoubleValuesSource asDoubleValuesSource() {
    return new DoubleValuesSource() {

        @Override
        public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
            Map context = new HashMap<>();
            FakeScorer scorer = new FakeScorer();
            context.put("scorer", scorer);
            FunctionValues fv = ValueSource.this.getValues(context, ctx);
            return new DoubleValues() {

                @Override
                public double doubleValue() throws IOException {
                    return fv.doubleVal(scorer.current);
                }

                @Override
                public boolean advanceExact(int doc) throws IOException {
                    scorer.current = doc;
                    if (scores != null && scores.advanceExact(doc)) {
                        scorer.score = (float) scores.doubleValue();
                    } else
                        scorer.score = 0;
                    return fv.exists(doc);
                }
            };
        }

        @Override
        public boolean needsScores() {
            // be on the safe side
            return true;
        }

        @Override
        public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
            Map context = new HashMap<>();
            FakeScorer scorer = new FakeScorer();
            scorer.score = scoreExplanation.getValue();
            context.put("scorer", scorer);
            FunctionValues fv = ValueSource.this.getValues(context, ctx);
            return fv.explain(docId);
        }
    };
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Explanation(org.apache.lucene.search.Explanation) DoubleValues(org.apache.lucene.search.DoubleValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class LinearModel method explain.

@Override
public Explanation explain(LeafReaderContext context, int doc, float finalScore, List<Explanation> featureExplanations) {
    final List<Explanation> details = new ArrayList<>();
    int index = 0;
    for (final Explanation featureExplain : featureExplanations) {
        final List<Explanation> featureDetails = new ArrayList<>();
        featureDetails.add(Explanation.match(featureToWeight[index], "weight on feature"));
        featureDetails.add(featureExplain);
        details.add(Explanation.match(featureExplain.getValue() * featureToWeight[index], "prod of:", featureDetails));
        index++;
    }
    return Explanation.match(finalScore, toString() + " model applied to features, sum of:", details);
}
Also used : Explanation(org.apache.lucene.search.Explanation) ArrayList(java.util.ArrayList)

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