Search in sources :

Example 26 with Explanation

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

the class ShapeAreaValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues shapeValues = shapeValueSource.getValues(context, readerContext);
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) throws IOException {
            Shape shape = (Shape) shapeValues.objectVal(doc);
            if (shape == null || shape.isEmpty())
                //or NaN?
                return 0;
            //   assuming ctx.isGeo()
            return shape.getArea(geoArea ? ctx : null) * multiplier;
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return shapeValues.exists(doc);
        }

        @Override
        public Explanation explain(int doc) throws IOException {
            Explanation exp = super.explain(doc);
            List<Explanation> details = new ArrayList<>(Arrays.asList(exp.getDetails()));
            details.add(shapeValues.explain(doc));
            return Explanation.match(exp.getValue(), exp.getDescription(), details);
        }
    };
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Explanation(org.apache.lucene.search.Explanation) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) ArrayList(java.util.ArrayList) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 27 with Explanation

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

the class SolrPluginUtils method explanationToNamedList.

public static NamedList<Object> explanationToNamedList(Explanation e) {
    NamedList<Object> out = new SimpleOrderedMap<>();
    out.add("match", e.isMatch());
    out.add("value", e.getValue());
    out.add("description", e.getDescription());
    Explanation[] details = e.getDetails();
    // short circut out
    if (0 == details.length)
        return out;
    List<NamedList<Object>> kids = new ArrayList<>(details.length);
    for (Explanation d : details) {
        kids.add(explanationToNamedList(d));
    }
    out.add("details", kids);
    return out;
}
Also used : Explanation(org.apache.lucene.search.Explanation) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 28 with Explanation

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

the class SolrPluginUtils method getExplanations.

/**
   * Generates an NamedList of Explanations for each item in a list of docs.
   *
   * @param query The Query you want explanations in the context of
   * @param docs The Documents you want explained relative that query
   */
public static NamedList<Explanation> getExplanations(Query query, DocList docs, SolrIndexSearcher searcher, IndexSchema schema) throws IOException {
    NamedList<Explanation> explainList = new SimpleOrderedMap<>();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        int id = iterator.nextDoc();
        Document doc = searcher.doc(id);
        String strid = schema.printableUniqueKey(doc);
        explainList.add(strid, searcher.explain(query, id));
    }
    return explainList;
}
Also used : DocIterator(org.apache.solr.search.DocIterator) Explanation(org.apache.lucene.search.Explanation) Document(org.apache.lucene.document.Document) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 29 with Explanation

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

the class TestClassicSimilarity method testNormEncodingBackwardCompatibility.

public void testNormEncodingBackwardCompatibility() throws IOException {
    Similarity similarity = new ClassicSimilarity();
    for (int indexCreatedVersionMajor : new int[] { Version.LUCENE_6_0_0.major, Version.LATEST.major }) {
        for (int length : new int[] { 1, 4, 16 }) {
            // these length values are encoded accurately on both cases
            Directory dir = newDirectory();
            // set the version on the directory
            new SegmentInfos(indexCreatedVersionMajor).commit(dir);
            IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(similarity));
            Document doc = new Document();
            String value = IntStream.range(0, length).mapToObj(i -> "b").collect(Collectors.joining(" "));
            doc.add(new TextField("foo", value, Store.NO));
            w.addDocument(doc);
            IndexReader reader = DirectoryReader.open(w);
            IndexSearcher searcher = newSearcher(reader);
            searcher.setSimilarity(similarity);
            Explanation expl = searcher.explain(new TermQuery(new Term("foo", "b")), 0);
            Explanation fieldNorm = findExplanation(expl, "fieldNorm");
            assertNotNull(fieldNorm);
            assertEquals(fieldNorm.toString(), 1 / Math.sqrt(length), fieldNorm.getValue(), 0f);
            w.close();
            reader.close();
            dir.close();
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) StringField(org.apache.lucene.document.StringField) Term(org.apache.lucene.index.Term) TestUtil(org.apache.lucene.util.TestUtil) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) Store(org.apache.lucene.document.Field.Store) TopDocs(org.apache.lucene.search.TopDocs) Explanation(org.apache.lucene.search.Explanation) Occur(org.apache.lucene.search.BooleanClause.Occur) DirectoryReader(org.apache.lucene.index.DirectoryReader) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Version(org.apache.lucene.util.Version) SegmentInfos(org.apache.lucene.index.SegmentInfos) FieldInvertState(org.apache.lucene.index.FieldInvertState) IndexWriter(org.apache.lucene.index.IndexWriter) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) TextField(org.apache.lucene.document.TextField) IDFStats(org.apache.lucene.search.similarities.TFIDFSimilarity.IDFStats) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) SegmentInfos(org.apache.lucene.index.SegmentInfos) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) Directory(org.apache.lucene.store.Directory)

Example 30 with Explanation

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

the class ExpressionRescorer method explain.

@Override
public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID) throws IOException {
    Explanation superExpl = super.explain(searcher, firstPassExplanation, docID);
    List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
    int subReader = ReaderUtil.subIndex(docID, leaves);
    LeafReaderContext readerContext = leaves.get(subReader);
    int docIDInSegment = docID - readerContext.docBase;
    return expression.getDoubleValuesSource(bindings).explain(readerContext, docIDInSegment, superExpl);
}
Also used : Explanation(org.apache.lucene.search.Explanation) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

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