List of usage examples for org.apache.lucene.search IndexSearcher search
public <C extends Collector, T> T search(Query query, CollectorManager<C, T> collectorManager) throws IOException
From source file:com.leavesfly.lia.searching.BasicSearchingTest.java
License:Apache License
public void testKeyword() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Term t = new Term("isbn", "9781935182023"); Query query = new TermQuery(t); TopDocs docs = searcher.search(query, 10); assertEquals("JUnit in Action, Second Edition", 1, docs.totalHits); searcher.close();// ww w. j av a2s .co m dir.close(); }
From source file:com.leavesfly.lia.searching.BasicSearchingTest.java
License:Apache License
public void testQueryParser() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); QueryParser parser = new QueryParser(Version.LUCENE_30, //A "contents", //A new SimpleAnalyzer()); //A Query query = parser.parse("+JUNIT +ANT -MOCK"); //B TopDocs docs = searcher.search(query, 10); assertEquals(1, docs.totalHits);/*from ww w .ja v a 2 s.c o m*/ Document d = searcher.doc(docs.scoreDocs[0].doc); assertEquals("Ant in Action", d.get("title")); query = parser.parse("mock OR junit"); //B docs = searcher.search(query, 10); assertEquals("Ant in Action, " + "JUnit in Action, Second Edition", 2, docs.totalHits); searcher.close(); dir.close(); }
From source file:com.leavesfly.lia.searching.BooleanQueryTest.java
License:Apache License
public void testOr() throws Exception { TermQuery methodologyBooks = new TermQuery( // #1 new Term("category", // #1 "/technology/computers/programming/methodology")); // #1 TermQuery easternPhilosophyBooks = new TermQuery( // #2 new Term("category", // #2 "/philosophy/eastern")); // #2 BooleanQuery enlightenmentBooks = new BooleanQuery(); // #3 enlightenmentBooks.add(methodologyBooks, // #3 BooleanClause.Occur.SHOULD); // #3 enlightenmentBooks.add(easternPhilosophyBooks, // #3 BooleanClause.Occur.SHOULD); // #3 Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); TopDocs matches = searcher.search(enlightenmentBooks, 10); System.out.println("or = " + enlightenmentBooks); assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Extreme Programming Explained")); assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Tao Te Ching \u9053\u5FB7\u7D93")); searcher.close();//from w ww . jav a2s. co m dir.close(); }
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);/*from w w w. jav a 2s .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.NearRealTimeTest.java
License:Apache License
public void testNearRealTime() throws Exception { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED); for (int i = 0; i < 10; i++) { Document doc = new Document(); doc.add(new Field("id", "" + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field("text", "aaa", Field.Store.NO, Field.Index.ANALYZED)); writer.addDocument(doc);// ww w .j av a 2s. c o m } IndexReader reader = writer.getReader(); // #1 IndexSearcher searcher = new IndexSearcher(reader); // #A Query query = new TermQuery(new Term("text", "aaa")); TopDocs docs = searcher.search(query, 1); assertEquals(10, docs.totalHits); // #B writer.deleteDocuments(new Term("id", "7")); // #2 Document doc = new Document(); // #3 doc.add(new Field("id", // #3 "11", // #3 Field.Store.NO, // #3 Field.Index.NOT_ANALYZED_NO_NORMS)); // #3 doc.add(new Field("text", // #3 "bbb", // #3 Field.Store.NO, // #3 Field.Index.ANALYZED)); // #3 writer.addDocument(doc); // #3 IndexReader newReader = reader.reopen(); // #4 assertFalse(reader == newReader); // #5 reader.close(); // #6 searcher = new IndexSearcher(newReader); TopDocs hits = searcher.search(query, 10); // #7 assertEquals(9, hits.totalHits); // #7 query = new TermQuery(new Term("text", "bbb")); // #8 hits = searcher.search(query, 1); // #8 assertEquals(1, hits.totalHits); // #8 newReader.close(); writer.close(); }
From source file:com.leavesfly.lia.searching.NumericRangeQueryTest.java
License:Apache License
public void testInclusive() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); // pub date of TTC was September 2006 NumericRangeQuery query = NumericRangeQuery.newIntRange("pubmonth", 200605, 200609, true, true); TopDocs matches = searcher.search(query, 10); /*/*from ww w . ja v a 2 s. c om*/ for(int i=0;i<matches.totalHits;i++) { System.out.println("match " + i + ": " + searcher.doc(matches.scoreDocs[i].doc).get("author")); } */ assertEquals(1, matches.totalHits); searcher.close(); dir.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);// ww w. j ava 2 s . c o m assertEquals(1F, matches.scoreDocs[0].score, 0.0); searcher.close(); }
From source file:com.leavesfly.lia.searching.TermRangeQueryTest.java
License:Apache License
public void testTermRangeQuery() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); TermRangeQuery query = new TermRangeQuery("title2", "d", "j", true, true); TopDocs matches = searcher.search(query, 100); /*/*from w w w .ja v a 2s . c o m*/ * for(int i=0;i<matches.totalHits;i++) { System.out.println("match " + * i + ": " + searcher.doc(matches.scoreDocs[i].doc).get("title2")); } */ assertEquals(3, matches.totalHits); searcher.close(); dir.close(); }
From source file:com.leavesfly.lia.tool.BooksMoreLikeThis.java
License:Apache License
public static void main(String[] args) throws Throwable { String indexDir = System.getProperty("index.dir"); FSDirectory directory = FSDirectory.open(new File(indexDir)); IndexReader reader = IndexReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); int numDocs = reader.maxDoc(); MoreLikeThis mlt = new MoreLikeThis(reader); // #A mlt.setFieldNames(new String[] { "title", "author" }); mlt.setMinTermFreq(1); // #B mlt.setMinDocFreq(1);//from w w w . java 2s . co m for (int docID = 0; docID < numDocs; docID++) { // #C System.out.println(); Document doc = reader.document(docID); System.out.println(doc.get("title")); Query query = mlt.like(docID); // #D System.out.println(" query=" + query); TopDocs similarDocs = searcher.search(query, 10); if (similarDocs.totalHits == 0) System.out.println(" None like this"); for (int i = 0; i < similarDocs.scoreDocs.length; i++) { if (similarDocs.scoreDocs[i].doc != docID) { // #E doc = reader.document(similarDocs.scoreDocs[i].doc); System.out.println(" -> " + doc.getField("title").stringValue()); } } } searcher.close(); reader.close(); directory.close(); }
From source file:com.leavesfly.lia.tool.HighlightTest.java
License:Apache License
public void testHits() throws Exception { IndexSearcher searcher = new IndexSearcher(TestUtil.getBookIndexDirectory()); TermQuery query = new TermQuery(new Term("title", "action")); TopDocs hits = searcher.search(query, 10); QueryScorer scorer = new QueryScorer(query, "title"); Highlighter highlighter = new Highlighter(scorer); highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer)); Analyzer analyzer = new SimpleAnalyzer(); for (ScoreDoc sd : hits.scoreDocs) { Document doc = searcher.doc(sd.doc); String title = doc.get("title"); TokenStream stream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), sd.doc, "title", doc, analyzer);/*from w ww . j a va 2 s .c o m*/ String fragment = highlighter.getBestFragment(stream, title); System.out.println(fragment); } }