Java Code Examples for org.apache.lucene.search.IndexSearcher#explain()
The following examples show how to use
org.apache.lucene.search.IndexSearcher#explain() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestFunctionScoreQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testTruncateNegativeScores() throws IOException { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); Document doc = new Document(); doc.add(new NumericDocValuesField("foo", -2)); w.addDocument(doc); IndexReader reader = DirectoryReader.open(w); w.close(); IndexSearcher searcher = newSearcher(reader); Query q = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.fromLongField("foo")); QueryUtils.check(random(), q, searcher); Explanation expl = searcher.explain(q, 0); assertEquals(0, expl.getValue().doubleValue(), 0f); assertTrue(expl.toString(), expl.getDetails()[0].getDescription().contains("truncated score")); reader.close(); dir.close(); }
Example 2
Source File: TestFunctionScoreQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testNaN() throws IOException { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); Document doc = new Document(); doc.add(new NumericDocValuesField("foo", Double.doubleToLongBits(Double.NaN))); w.addDocument(doc); IndexReader reader = DirectoryReader.open(w); w.close(); IndexSearcher searcher = newSearcher(reader); Query q = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.fromDoubleField("foo")); QueryUtils.check(random(), q, searcher); Explanation expl = searcher.explain(q, 0); assertEquals(0, expl.getValue().doubleValue(), 0f); assertTrue(expl.toString(), expl.getDetails()[0].getDescription().contains("NaN is an illegal score")); reader.close(); dir.close(); }
Example 3
Source File: SweetSpotSimilarityTest.java From lucene-solr with Apache License 2.0 | 6 votes |
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 ByteBuffersDirectory(); 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().floatValue(); }
Example 4
Source File: TestSweetSpotSimilarityFactory.java From lucene-solr with Apache License 2.0 | 6 votes |
private static float computeNorm(Similarity sim, int length) throws IOException { String value = IntStream.range(0, length).mapToObj(i -> "a").collect(Collectors.joining(" ")); Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); w.addDocument(Collections.singleton(newTextField("foo", 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("foo", "a")), 0); reader.close(); dir.close(); Explanation norm = findExplanation(expl, "fieldNorm"); assertNotNull(norm); return norm.getValue().floatValue(); }
Example 5
Source File: IndexManager.java From spacewalk with GNU General Public License v2.0 | 6 votes |
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher, Query q, Set<Term> queryTerms) throws IOException { log.debug("Parsed Query is " + q.toString()); log.debug("Looking at index: " + indexName); for (int i = 0; i < hits.length(); i++) { if ((i < 10)) { Document doc = hits.doc(i); Float score = hits.score(i); Explanation ex = searcher.explain(q, hits.id(i)); log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score + ">: " + doc); log.debug("Explanation: " + ex); MatchingField match = new MatchingField(q.toString(), doc, queryTerms); String fieldName = match.getFieldName(); String fieldValue = match.getFieldValue(); log.debug("Guessing that matched fieldName is " + fieldName + " = " + fieldValue); } } }
Example 6
Source File: IndexManager.java From uyuni with GNU General Public License v2.0 | 5 votes |
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher, Query q, Set<Term> queryTerms) throws IOException { log.debug("Parsed Query is " + q.toString()); log.debug("Looking at index: " + indexName); for (int i = 0; i < hits.length(); i++) { if ((i < 10)) { Document doc = hits.doc(i); Float score = hits.score(i); Explanation ex = searcher.explain(q, hits.id(i)); log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score + ">: " + doc); log.debug("Explanation: " + ex); MatchingField match = new MatchingField(q.toString(), doc, queryTerms); String fieldName = match.getFieldName(); String fieldValue = match.getFieldValue(); log.debug("Guessing that matched fieldName is " + fieldName + " = " + fieldValue); } } }
Example 7
Source File: ExplainCommand.java From clue with Apache License 2.0 | 5 votes |
@Override public void execute(Namespace args, PrintStream out) throws Exception { List<String> qlist = args.getList("query"); String qstring = CmdlineHelper.toString(qlist); List<Integer> docidList = args.getList("docs"); IndexReader r = ctx.getIndexReader(); IndexSearcher searcher = new IndexSearcher(r); Query q = null; try{ q = CmdlineHelper.toQuery(qlist, ctx.getQueryBuilder()); } catch(Exception e){ out.println("cannot parse query: "+e.getMessage()); return; } out.println("parsed query: "+q); for (Integer docid : docidList) { Explanation expl = searcher.explain(q, docid); out.println(expl); } out.flush(); }
Example 8
Source File: TestSimilarity2.java From lucene-solr with Apache License 2.0 | 4 votes |
/** 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()); } iw.close(); ir.close(); dir.close(); }