List of usage examples for org.apache.lucene.search IndexSearcher explain
protected Explanation explain(Weight weight, int doc) throws IOException
doc
scored against weight
. From source file:aos.lucene.search.msc.Explainer.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: Explainer <index dir> <query>"); System.exit(1);//from w w w . java2 s. c o m } String indexDir = args[0]; String queryExpression = args[1]; Directory directory = FSDirectory.open(new File(indexDir)); QueryParser parser = new QueryParser(Version.LUCENE_46, "contents", new SimpleAnalyzer()); Query query = parser.parse(queryExpression); LOGGER.info("Query: " + queryExpression); IndexSearcher searcher = new IndexSearcher(directory); TopDocs topDocs = searcher.search(query, 10); for (ScoreDoc match : topDocs.scoreDocs) { Explanation explanation = searcher.explain(query, match.doc); //#A LOGGER.info("----------"); Document doc = searcher.doc(match.doc); LOGGER.info(doc.get("title")); LOGGER.info(explanation.toString()); //#B } searcher.close(); directory.close(); }
From source file:aos.lucene.search.msc.ScoreTest.java
License:Apache License
public void testSimple() throws Exception { indexSingleFieldDocs(new Field[] { new Field("contents", "x", Field.Store.YES, Field.Index.ANALYZED) }); IndexSearcher searcher = new IndexSearcher(directory); searcher.setSimilarity(new SimpleSimilarity()); Query query = new TermQuery(new Term("contents", "x")); Explanation explanation = searcher.explain(query, 0); LOGGER.info(explanation);/*from ww w . j av a2s . c o m*/ TopDocs matches = searcher.search(query, 10); assertEquals(1, matches.totalHits); assertEquals(1F, matches.scoreDocs[0].score, 0.0); searcher.close(); }
From source file:at.ac.univie.mminf.luceneSKOS.util.TestUtil.java
License:Apache License
public static void explainQuery(IndexSearcher searcher, Query query) throws IOException { TopDocs topDocs = searcher.search(query, 10); for (ScoreDoc match : topDocs.scoreDocs) { Explanation explanation = searcher.explain(query, match.doc); System.out.println("---------------"); System.out.println(explanation.toString()); }/*w ww . j a v a 2 s . c o m*/ }
From source file:cn.b2b.index.product.index.ProductQueryOptimizer.java
@Override public Writable explain(IndexSearcher searcher, Writable param) throws Exception { ExplainParam queryParam = (ExplainParam) param; Query oriquery = queryParam.getQuery(); BooleanQuery original = QueryFilters.filter(oriquery, Constants.SLOP_SCORE_CVM_CLASS); BooleanQuery query = new BooleanQuery(); BooleanQuery filterQuery = null;//from ww w .j av a 2s. co m BooleanClause[] clauses = original.getClauses(); for (int i = 0; i < clauses.length; i++) { BooleanClause c = clauses[i]; if (c.isRequired() // required && c.getQuery().getBoost() == 0.0f // boost is zero && c.getQuery() instanceof TermQuery // TermQuery && (searcher.docFreq(((TermQuery) c.getQuery()).getTerm()) / (float) searcher.maxDoc()) >= threshold) { // check threshold if (filterQuery == null) filterQuery = new BooleanQuery(); // lucene1.4.3 -> lucene2.0.0 // filterQuery.add(c.getQuery(), true, false); // filter it filterQuery.add(c.getQuery(), BooleanClause.Occur.MUST); // filter // it } else { query.add(c); // query it } } Filter filter = null; if (filterQuery != null) { synchronized (cache) { // check cache filter = (Filter) cache.get(filterQuery); } if (filter == null) { // miss filter = new QueryFilter(filterQuery); // construct new entry synchronized (cache) { cache.put(filterQuery, filter); // cache it } } } ScoreParam scoreParam = new ScoreParam(); // store segment query word query.setQueryStr(original.getQueryStr()); float[] ranks = (float[]) MemoryFieldCache.get("rank"); Explanation explain = searcher.explain(query, queryParam.getDoc()); return new UTF8("RANK:" + ranks[queryParam.getDoc()] + "\t" + explain.toString()); }
From source file:com.github.le11.nls.lucene.UIMATypeBasedSimilarityTest.java
License:Apache License
@Test public void baseSimilarityTest() { try {//from www. j av a 2 s . c om IndexSearcher searcher = new IndexSearcher(dir, true); Similarity payloadSimilarity = new UIMATypeBasedSimilarity(); searcher.setSimilarity(payloadSimilarity); // BooleanQuery booleanQuery = new BooleanQuery(); // booleanQuery.add(new PayloadTermQuery(new Term("title", "London"), new MaxPayloadFunction()), BooleanClause.Occur.SHOULD); // booleanQuery.add(new PayloadTermQuery(new Term("title", "English"), new MaxPayloadFunction()), BooleanClause.Occur.SHOULD); // SpanQuery[] clauses = new SpanQuery[]{new PayloadTermQuery(new Term("title","London"),new MaxPayloadFunction()), // new PayloadTermQuery(new Term("title","English"),new MaxPayloadFunction())}; // int slop = 3; // boolean inOrder = true; // Query query = new PayloadNearQuery(clauses, slop, inOrder); Query directQuery = new TermQuery(new Term("title", "London")); TopDocs topDocs = searcher.search(directQuery, 10); System.out.println("Number of matching docs: " + topDocs.totalHits); ScoreDoc doc1 = topDocs.scoreDocs[0]; System.out.println("Doc: " + doc1.toString()); System.out.println("Explain: " + searcher.explain(directQuery, doc1.doc)); Query payloadQuery = new PayloadTermQuery(new Term("title", "London"), new MaxPayloadFunction()); topDocs = searcher.search(payloadQuery, 10); System.out.println("Number of matching docs: " + topDocs.totalHits); ScoreDoc doc2 = topDocs.scoreDocs[0]; System.out.println("Doc: " + doc2.toString()); System.out.println("Explain: " + searcher.explain(payloadQuery, doc2.doc)); assertTrue(doc1.score < doc2.score); } catch (Exception e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } }
From source file:com.leavesfly.lia.searching.Explainer.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: Explainer <index dir> <query>"); System.exit(1);/* w ww . j a v a2s.co m*/ } String indexDir = args[0]; String queryExpression = args[1]; Directory directory = FSDirectory.open(new File(indexDir)); QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new SimpleAnalyzer()); Query query = parser.parse(queryExpression); System.out.println("Query: " + queryExpression); IndexSearcher searcher = new IndexSearcher(directory); TopDocs topDocs = searcher.search(query, 10); for (ScoreDoc match : topDocs.scoreDocs) { Explanation explanation = searcher.explain(query, match.doc); //#A System.out.println("----------"); Document doc = searcher.doc(match.doc); System.out.println(doc.get("title")); System.out.println(explanation.toString()); //#B } searcher.close(); directory.close(); }
From source file:com.leavesfly.lia.searching.ScoreTest.java
License:Apache License
public void testSimple() throws Exception { indexSingleFieldDocs(new Field[] { new Field("contents", "x", Field.Store.YES, Field.Index.ANALYZED) }); IndexSearcher searcher = new IndexSearcher(directory); searcher.setSimilarity(new SimpleSimilarity()); Query query = new TermQuery(new Term("contents", "x")); Explanation explanation = searcher.explain(query, 0); System.out.println(explanation); TopDocs matches = searcher.search(query, 10); assertEquals(1, matches.totalHits);//from www. java 2 s. c om assertEquals(1F, matches.scoreDocs[0].score, 0.0); searcher.close(); }
From source file:com.mathworks.xzheng.searching.Explainer.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: Explainer <index dir> <query>"); System.exit(1);/*from w ww . j a va 2 s . com*/ } String indexDir = args[0]; String queryExpression = args[1]; Directory directory = FSDirectory.open(new File(indexDir)); QueryParser parser = new QueryParser(Version.LUCENE_46, "contents", new SimpleAnalyzer(Version.LUCENE_46)); Query query = parser.parse(queryExpression); System.out.println("Query: " + queryExpression); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory)); TopDocs topDocs = searcher.search(query, 10); for (ScoreDoc match : topDocs.scoreDocs) { Explanation explanation = searcher.explain(query, match.doc); //#A System.out.println("----------"); Document doc = searcher.doc(match.doc); System.out.println(doc.get("title")); System.out.println(explanation.toString()); //#B } directory.close(); }
From source file:com.mathworks.xzheng.searching.ScoreTest.java
License:Apache License
public void testSimple() throws Exception { indexSingleFieldDocs(new Field[] { new Field("contents", "x", Field.Store.YES, Field.Index.ANALYZED) }); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory)); searcher.setSimilarity(new SimpleSimilarity()); Query query = new TermQuery(new Term("contents", "x")); Explanation explanation = searcher.explain(query, 0); System.out.println(explanation); TopDocs matches = searcher.search(query, 10); assertEquals(1, matches.totalHits);//from www. ja va2 s.c o m assertEquals(1F, matches.scoreDocs[0].score, 0.0); }
From source file:com.mycompany.lucenedemo.SearchFiles.java
/** * This demonstrates a typical paging search scenario, where the search engine presents * pages of size n to the user. The user can then go to the next page if interested in * the next hits.// w w w.j av a 2 s .c o m * * When the query is executed for the first time, then only enough results are collected * to fill 5 result pages. If the user wants to page beyond this limit, then the query * is executed another time and all hits are collected. * */ public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage, boolean raw, boolean interactive) throws IOException { // Collect enough docs to show 5 pages TopDocs results = searcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = Math.toIntExact(results.totalHits); System.out.println(numTotalHits + " total matching documents"); int start = 0; int end = Math.min(numTotalHits, hitsPerPage); while (true) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits + " total matching documents collected."); System.out.println("Collect more (y/n) ?"); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'n') { break; } hits = searcher.search(query, numTotalHits).scoreDocs; } end = Math.min(hits.length, start + hitsPerPage); for (int i = start; i < end; i++) { Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println((i + 1) + ". " + path); String title = doc.get("title"); if (title != null) { System.out.println(" Title: " + doc.get("title")); } } else { System.out.println((i + 1) + ". " + "No path for this document"); } if (raw) { // output raw format System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score); System.out.println(searcher.explain(query, hits[i].doc)); } } if (!interactive || end == 0) { break; } if (numTotalHits >= end) { boolean quit = false; while (true) { System.out.print("Press "); if (start - hitsPerPage >= 0) { System.out.print("(p)revious page, "); } if (start + hitsPerPage < numTotalHits) { System.out.print("(n)ext page, "); } System.out.println("(q)uit or enter number to jump to a page."); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'q') { quit = true; break; } if (line.charAt(0) == 'p') { start = Math.max(0, start - hitsPerPage); break; } else if (line.charAt(0) == 'n') { if (start + hitsPerPage < numTotalHits) { start += hitsPerPage; } break; } else { int page = Integer.parseInt(line); if ((page - 1) * hitsPerPage < numTotalHits) { start = (page - 1) * hitsPerPage; break; } else { System.out.println("No such page"); } } } if (quit) break; end = Math.min(numTotalHits, start + hitsPerPage); } } }