List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher
public IndexSearcher(IndexReaderContext context)
From source file:aos.lucene.search.msc.NearRealTimeTest.java
License:Apache License
public void testNearRealTime() throws Exception { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_46), 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);/* w w w.j a va 2 s.c om*/ } IndexReader reader = writer.getReader(); IndexSearcher searcher = new IndexSearcher(reader); Query query = new TermQuery(new Term("text", "aaa")); TopDocs docs = searcher.search(query, 1); assertEquals(10, docs.totalHits); writer.deleteDocuments(new Term("id", "7")); Document doc = new Document(); doc.add(new Field("id", "11", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field("text", "bbb", Field.Store.NO, Field.Index.ANALYZED)); writer.addDocument(doc); IndexReader newReader = reader.reopen(); assertFalse(reader == newReader); reader.close(); searcher = new IndexSearcher(newReader); TopDocs hits = searcher.search(query, 10); assertEquals(9, hits.totalHits); query = new TermQuery(new Term("text", "bbb")); hits = searcher.search(query, 1); assertEquals(1, hits.totalHits); newReader.close(); writer.close(); }
From source file:aos.lucene.search.msc.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 w ww . j a v a 2 s.co m*/ for(int i=0;i<matches.totalHits;i++) { LOGGER.info("match " + i + ": " + searcher.doc(matches.scoreDocs[i].doc).get("author")); } */ assertEquals(1, matches.totalHits); searcher.close(); dir.close(); }
From source file:aos.lucene.search.msc.NumericRangeQueryTest.java
License:Apache License
public void testExclusive() 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, false, false); TopDocs matches = searcher.search(query, 10); assertEquals(0, matches.totalHits);//from w w w .jav a2 s .c o m searcher.close(); dir.close(); }
From source file:aos.lucene.search.msc.PhraseQueryTest.java
License:Apache License
protected void setUp() throws IOException { dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(Version.LUCENE_46), IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("field", "the quick brown fox jumped over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from www .j av a 2s .c o m writer.close(); searcher = new IndexSearcher(dir); }
From source file:aos.lucene.search.msc.PrefixQueryTest.java
License:Apache License
public void testPrefix() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Term term = new Term("category", //#A "/technology/computers/programming"); //#A PrefixQuery query = new PrefixQuery(term); //#A TopDocs matches = searcher.search(query, 10); //#A int programmingAndBelow = matches.totalHits; matches = searcher.search(new TermQuery(term), 10); //#B int justProgramming = matches.totalHits; assertTrue(programmingAndBelow > justProgramming); searcher.close();/* w w w. jav a 2s .co m*/ dir.close(); }
From source file:aos.lucene.search.msc.QueryParserTest.java
License:Apache License
protected void setUp() throws Exception { analyzer = new WhitespaceAnalyzer(Version.LUCENE_46); dir = TestUtil.getBookIndexDirectory(); searcher = new IndexSearcher(dir); }
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 w w w.j a v a 2 s. c om TopDocs matches = searcher.search(query, 10); assertEquals(1, matches.totalHits); assertEquals(1F, matches.scoreDocs[0].score, 0.0); searcher.close(); }
From source file:aos.lucene.search.msc.ScoreTest.java
License:Apache License
public void testWildcard() throws Exception { indexSingleFieldDocs(new Field[] { new Field("contents", "wild", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "child", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "mild", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "mildew", Field.Store.YES, Field.Index.ANALYZED) }); IndexSearcher searcher = new IndexSearcher(directory); Query query = new WildcardQuery(new Term("contents", "?ild*")); //#A TopDocs matches = searcher.search(query, 10); assertEquals("child no match", 3, matches.totalHits); assertEquals("score the same", matches.scoreDocs[0].score, matches.scoreDocs[1].score, 0.0); assertEquals("score the same", matches.scoreDocs[1].score, matches.scoreDocs[2].score, 0.0); searcher.close();//from www. j a v a 2s . c o m }
From source file:aos.lucene.search.msc.ScoreTest.java
License:Apache License
public void testFuzzy() throws Exception { indexSingleFieldDocs(new Field[] { new Field("contents", "fuzzy", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "wuzzy", Field.Store.YES, Field.Index.ANALYZED) }); IndexSearcher searcher = new IndexSearcher(directory); Query query = new FuzzyQuery(new Term("contents", "wuzza")); TopDocs matches = searcher.search(query, 10); assertEquals("both close enough", 2, matches.totalHits); assertTrue("wuzzy closer than fuzzy", matches.scoreDocs[0].score != matches.scoreDocs[1].score); Document doc = searcher.doc(matches.scoreDocs[0].doc); assertEquals("wuzza bear", "wuzzy", doc.get("contents")); searcher.close();//w ww . java2 s.co m }
From source file:aos.lucene.search.msc.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); /*/* w w w. j a v a 2 s . c o m*/ for(int i=0;i<matches.totalHits;i++) { LOGGER.info("match " + i + ": " + searcher.doc(matches.scoreDocs[i].doc).get("title2")); } */ assertEquals(3, matches.totalHits); searcher.close(); dir.close(); }