Example usage for org.apache.lucene.search Weight explain

List of usage examples for org.apache.lucene.search Weight explain

Introduction

In this page you can find the example usage for org.apache.lucene.search Weight explain.

Prototype

public abstract Explanation explain(LeafReaderContext context, int doc) throws IOException;

Source Link

Document

An explanation of the score computation for the named document.

Usage

From source file:com.sindicetech.siren.search.node.TestNodePhraseQuery.java

License:Open Source License

@Test
public void testExplain() throws IOException {
    this.addDocuments("\"Renaud Delbru\" . ", "\"Renaud Delbru\" . \"Renaud Delbru\" . ");

    final Query query = npq("renaud", "delbru").getLuceneProxyQuery();

    final Weight w = query.createWeight(searcher);

    // Explain entity 0 : 1 match
    Explanation explanation = w.explain((AtomicReaderContext) reader.getContext(), 0);
    assertNotNull("explanation is null and it shouldn't be", explanation);

    //    final Similarity sim = searcher.getSimilarity();
    // TODO: The similarity is randomly set
    //     System.out.println("Explanation: " + explanation.toString());
    //    //All this Explain does is return the term frequency
    //    assertEquals("term frq is not 1",
    //      sim.tf(1), explanation.getDetails()[1].getDetails()[0].getValue(), 0.01);

    // Explain entity 1 : 2 match
    explanation = w.explain((AtomicReaderContext) reader.getContext(), 1);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    // TODO: The similarity is randomly set
    //     System.out.println("Explanation: " + explanation.toString());
    //    //All this Explain does is return the term frequency
    //    assertEquals("term frq is not 2",
    //      sim.tf(2), explanation.getDetails()[1].getDetails()[0].getValue(), 0f);

    // Explain non existing entity
    explanation = w.explain((AtomicReaderContext) reader.getContext(), 2);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    //    System.out.println("Explanation: " + explanation.toString());
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 0", 0f, explanation.getValue(), 0f);
}

From source file:com.sindicetech.siren.search.node.TestNodeTermQuery.java

License:Open Source License

@Test
public void testExplain() throws IOException {
    this.addDocumentNoNorms(
            "<http://renaud.delbru.fr/rdf/foaf#me> <http://xmlns.com/foaf/0.1/name> \"Renaud Delbru\" . ");

    final Query query = ntq("renaud").getLuceneProxyQuery();
    final Weight w = searcher.createNormalizedWeight(query);
    assertTrue(searcher.getTopReaderContext() instanceof AtomicReaderContext);
    final AtomicReaderContext context = (AtomicReaderContext) searcher.getTopReaderContext();

    // Explain entity 0
    Explanation explanation = w.explain(context, 0);
    assertNotNull("explanation is null and it shouldn't be", explanation);

    // TODO: the similarity is random
    // All this Explain does is return the term frequency
    //    final float termFreq = explanation.getDetails()[0].getDetails()[0].getValue();
    //    assertEquals("term frq is not 2", 2f, termFreq, 0f);

    // Explain non existing entity
    explanation = w.explain(context, 1);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 0", 0f, explanation.getValue(), 0f);
}

From source file:org.apache.solr.ltr.LTRRescorer.java

License:Apache License

@Override
public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID)
        throws IOException {

    final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext().leaves();
    final int n = ReaderUtil.subIndex(docID, leafContexts);
    final LeafReaderContext context = leafContexts.get(n);
    final int deBasedDoc = docID - context.docBase;
    final Weight modelWeight = searcher.createNormalizedWeight(scoringQuery, true);
    return modelWeight.explain(context, deBasedDoc);
}

From source file:org.elasticsearch.index.query.functionscore.FunctionScoreTests.java

License:Apache License

public Explanation getFunctionScoreExplanation(IndexSearcher searcher, ScoreFunction scoreFunction)
        throws IOException {
    FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(new TermQuery(TERM), scoreFunction, 0.0f);
    functionScoreQuery.setCombineFunction(CombineFunction.AVG);
    Weight weight = searcher.createNormalizedWeight(functionScoreQuery, true);
    Explanation explanation = weight.explain(searcher.getIndexReader().leaves().get(0), 0);
    return explanation.getDetails()[1];
}

From source file:org.elasticsearch.index.query.functionscore.FunctionScoreTests.java

License:Apache License

protected Explanation getExplanation(IndexSearcher searcher,
        FiltersFunctionScoreQuery filtersFunctionScoreQuery) throws IOException {
    Weight weight = searcher.createNormalizedWeight(filtersFunctionScoreQuery, true);
    return weight.explain(searcher.getIndexReader().leaves().get(0), 0);
}

From source file:org.sindice.siren.search.TestSirenPhraseQuery.java

License:Apache License

@Test
public void testExplain() throws IOException {
    _helper.addDocument("\"Renaud Delbru\" . ");
    _helper.addDocument("\"Renaud Delbru\" . \"Renaud Delbru\" . ");

    final Term t1 = new Term(QueryTestingHelper.DEFAULT_FIELD, "renaud");
    final Term t2 = new Term(QueryTestingHelper.DEFAULT_FIELD, "delbru");
    final SirenPhraseQuery query = new SirenPhraseQuery();
    query.add(t1);/*  ww  w .jav  a  2  s  . c  om*/
    query.add(t2);
    final Weight w = query.weight(_helper.getSearcher());
    final IndexReader reader = _helper.getIndexReader();

    // Explain entity 0 : 1 match
    Explanation explanation = w.explain(reader, 0);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    // System.out.println("Explanation: " + explanation.toString());
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 1", _helper.getSearcher().getSimilarity().tf(1),
            explanation.getDetails()[1].getDetails()[0].getValue(), 0.01);

    // Explain entity 1 : 2 match
    explanation = w.explain(reader, 1);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    // System.out.println("Explanation: " + explanation.toString());
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 2", _helper.getSearcher().getSimilarity().tf(2),
            explanation.getDetails()[1].getDetails()[0].getValue(), 0f);

    // Explain non existing entity
    explanation = w.explain(reader, 2);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    //System.out.println("Explanation: " + explanation.toString());
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 0", 0f, explanation.getValue(), 0f);
}

From source file:org.sindice.siren.search.TestSirenTermQuery.java

License:Apache License

/**
 * Check if explanation is correct/*from   ww  w  .  j a  v a  2  s . com*/
 */
@Test
public void testWeight() throws Exception {
    _helper.addDocument("\"Renaud Delbru\" . ");
    _helper.addDocument("\"Renaud\" . ");

    final SirenTermQuery query = new SirenTermQuery(new Term("content", "renaud"));
    final Weight w = query.weight(_helper.getSearcher());
    assertNotNull(w);
    final Explanation explain = w.explain(_helper.getIndexReader(), 0);
    assertNotNull(explain);
    assertTrue(explain.isMatch());
    assertEquals(0.37158427f, explain.getValue(), 0f);
    //System.out.println(explain.toString());
}

From source file:org.sindice.siren.search.TestSirenTermQuery.java

License:Apache License

@Test
public void testExplain() throws IOException {
    _helper.addDocument("\"Renaud\" . ");

    final Term t = new Term(QueryTestingHelper.DEFAULT_FIELD, "renaud");
    final SirenTermQuery query = new SirenTermQuery(t);
    final Weight w = query.weight(_helper.getSearcher());
    final IndexReader reader = _helper.getIndexReader();

    // Explain entity 0
    Explanation explanation = w.explain(reader, 0);
    assertNotNull("explanation is null and it shouldn't be", explanation);
    System.out.println("Explanation: " + explanation.toString());
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 1", 1f, explanation.getDetails()[0].getValue(), 0f);

    // Explain non existing entity
    explanation = w.explain(reader, 1);//  www.  j a va 2s.c  o  m
    assertNotNull("explanation is null and it shouldn't be", explanation);
    //System.out.println("Explanation: " + explanation.toString());
    //All this Explain does is return the term frequency
    assertEquals("term frq is not 0", 0f, explanation.getValue(), 0f);
}

From source file:uk.co.flax.luwak.util.ForceNoBulkScoringQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {

    final Weight innerWeight = inner.createWeight(searcher, needsScores);

    return new Weight(ForceNoBulkScoringQuery.this) {
        @Override/*from  w w  w. j a v a2  s . co m*/
        public void extractTerms(Set<Term> set) {
            innerWeight.extractTerms(set);
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int i) throws IOException {
            return innerWeight.explain(leafReaderContext, i);
        }

        @Override
        public float getValueForNormalization() throws IOException {
            return innerWeight.getValueForNormalization();
        }

        @Override
        public void normalize(float v, float v1) {
            innerWeight.normalize(v, v1);
        }

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            return innerWeight.scorer(leafReaderContext);
        }
    };
}