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: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();/*from w w w . ja v a2 s . co m*/ dir.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 w ww .ja v a2 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();/* w w w. j a v a 2 s .co 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();/*from w w w.ja v a 2 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); /*//from www . j ava2 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(); }
From source file:aos.lucene.tools.BerkeleyDbJESearcher.java
License:Apache License
public static void main(String[] args) throws IOException, DatabaseException { if (args.length != 1) { System.err.println("Usage: BerkeleyDbSearcher <index dir>"); System.exit(-1);/*from www .j a v a2 s . c o m*/ } File indexFile = new File(args[0]); EnvironmentConfig envConfig = new EnvironmentConfig(); DatabaseConfig dbConfig = new DatabaseConfig(); envConfig.setTransactional(true); envConfig.setAllowCreate(true); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); Environment env = new Environment(indexFile, envConfig); Database index = env.openDatabase(null, "__index__", dbConfig); Database blocks = env.openDatabase(null, "__blocks__", dbConfig); JEDirectory directory = new JEDirectory(null, index, blocks); IndexSearcher searcher = new IndexSearcher(directory, true); TopDocs hits = searcher.search(new TermQuery(new Term("contents", "fox")), 10); LOGGER.info(hits.totalHits + " documents found"); searcher.close(); index.close(); blocks.close(); env.close(); }
From source file:aos.lucene.tools.BerkeleyDbSearcher.java
License:Apache License
public static void main(String[] args) throws IOException, DatabaseException { if (args.length != 1) { System.err.println("Usage: BerkeleyDbSearcher <index dir>"); System.exit(-1);// ww w. j a v a2s.c o m } File indexFile = new File(args[0]); EnvironmentConfig envConfig = new EnvironmentConfig(); DatabaseConfig dbConfig = new DatabaseConfig(); envConfig.setTransactional(true); envConfig.setInitializeCache(true); envConfig.setInitializeLocking(true); envConfig.setInitializeLogging(true); envConfig.setAllowCreate(true); envConfig.setThreaded(true); dbConfig.setAllowCreate(true); dbConfig.setType(DatabaseType.BTREE); Environment env = new Environment(indexFile, envConfig); Database index = env.openDatabase(null, "__index__", null, dbConfig); Database blocks = env.openDatabase(null, "__blocks__", null, dbConfig); DbDirectory directory = new DbDirectory(null, index, blocks, 0); IndexSearcher searcher = new IndexSearcher(directory, true); TopDocs hits = searcher.search(new TermQuery(new Term("contents", "fox")), 10); LOGGER.info(hits.totalHits + " documents found"); searcher.close(); index.close(); blocks.close(); env.close(); }
From source file:aos.lucene.tools.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 = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); int numDocs = reader.maxDoc(); MoreLikeThis mlt = new MoreLikeThis(reader); mlt.setFieldNames(new String[] { "title", "author" }); mlt.setMinTermFreq(1);//from w w w . j a v a2 s .com mlt.setMinDocFreq(1); for (int docID = 0; docID < numDocs; docID++) { LOGGER.info(); Document doc = reader.document(docID); LOGGER.info(doc.get("title")); Query query = mlt.like(docID); LOGGER.info(" query=" + query); TopDocs similarDocs = searcher.search(query, 10); if (similarDocs.totalHits == 0) LOGGER.info(" None like this"); for (int i = 0; i < similarDocs.scoreDocs.length; i++) { if (similarDocs.scoreDocs[i].doc != docID) { doc = reader.document(similarDocs.scoreDocs[i].doc); LOGGER.info(" -> " + doc.getField("title").stringValue()); } } } reader.close(); directory.close(); }
From source file:aos.lucene.tools.FastVectorHighlighterSample.java
License:Apache License
static void searchIndex(String filename) throws Exception { QueryParser parser = new QueryParser(Version.LUCENE_46, F, analyzer); Query query = parser.parse(QUERY); FastVectorHighlighter highlighter = getHighlighter(); // #C FieldQuery fieldQuery = highlighter.getFieldQuery(query); // #D IndexSearcher searcher = new IndexSearcher(dir); TopDocs docs = searcher.search(query, 10); FileWriter writer = new FileWriter(filename); writer.write("<html>"); writer.write("<body>"); writer.write("<p>QUERY : " + QUERY + "</p>"); for (ScoreDoc scoreDoc : docs.scoreDocs) { String snippet = highlighter.getBestFragment( // #E fieldQuery, searcher.getIndexReader(), // #E scoreDoc.doc, F, 100); // #E if (snippet != null) { writer.write(scoreDoc.doc + " : " + snippet + "<br/>"); }/*from w w w . ja v a 2 s .c o m*/ } writer.write("</body></html>"); writer.close(); }
From source file:aos.lucene.tools.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);/*w w w. j a v a 2 s . c o m*/ String fragment = highlighter.getBestFragment(stream, title); LOGGER.info(fragment); } }