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.mathworks.xzheng.extsearch.collector.CollectorTest.java
License:Apache License
public void testCollecting() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); TermQuery query = new TermQuery(new Term("contents", "junit")); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); BookLinkCollector collector = new BookLinkCollector(); searcher.search(query, collector); Map<String, String> linkMap = collector.getLinks(); assertEquals("ant in action", linkMap.get("http://www.manning.com/loughran")); TopDocs hits = searcher.search(query, 10); TestUtil.dumpHits(searcher, hits);//from w w w . j a v a2 s . c o m dir.close(); }
From source file:com.mathworks.xzheng.extsearch.payloads.PayloadsTest.java
License:Apache License
public void testPayloadTermQuery() throws Throwable { addDoc("Hurricane warning", "Bulletin: A hurricane warning was issued at " + "6 AM for the outer great banks"); addDoc("Warning label maker", "The warning label maker is a delightful toy for " + "your precocious seven year old's warning needs"); addDoc("Tornado warning", "Bulletin: There is a tornado warning for " + "Worcester county until 6 PM today"); IndexReader r = writer.getReader();/*from w w w. ja va 2s . c o m*/ writer.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(new BoostingSimilarity()); Term warning = new Term("contents", "warning"); Query query1 = new TermQuery(warning); System.out.println("\nTermQuery results:"); TopDocs hits = searcher.search(query1, 10); TestUtil.dumpHits(searcher, hits); assertEquals("Warning label maker", // #B searcher.doc(hits.scoreDocs[0].doc).get("title")); // #B Query query2 = new PayloadTermQuery(warning, new AveragePayloadFunction()); System.out.println("\nPayloadTermQuery results:"); hits = searcher.search(query2, 10); TestUtil.dumpHits(searcher, hits); assertEquals("Warning label maker", // #C searcher.doc(hits.scoreDocs[2].doc).get("title")); // #C r.close(); }
From source file:com.mathworks.xzheng.meetlucene.Fragments.java
License:Apache License
public void simpleSearch() throws IOException { Directory dir = FSDirectory.open(new File("/tmp/index")); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); Query q = new TermQuery(new Term("contents", "lucene")); TopDocs hits = searcher.search(q, 10); }
From source file:com.mathworks.xzheng.meetlucene.Searcher.java
License:Apache License
public static void search(String indexDir, String q) throws IOException, ParseException { Directory dir = FSDirectory.open(new File(indexDir)); //3 IndexSearcher is = new IndexSearcher(DirectoryReader.open(dir)); //3 QueryParser parser = new QueryParser(Version.LUCENE_46, // 4 "contents", //4 new StandardAnalyzer( //4 Version.LUCENE_46)); //4 Query query = parser.parse(q); //4 long start = System.currentTimeMillis(); TopDocs hits = is.search(query, 10); //5 long end = System.currentTimeMillis(); System.err.println("Found " + hits.totalHits + //6 " document(s) (in " + (end - start) + // 6 " milliseconds) that matched query '" + // 6 q + "':"); // 6 for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); //7 System.out.println(doc.get("fullpath")); //8 }// w ww. j a v a 2 s . com is.getIndexReader().close(); //9 }
From source file:com.mathworks.xzheng.searching.BasicSearchingTest.java
License:Apache License
public void testTerm() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); //A IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); //B Term t = new Term("subject", "ant"); Query query = new TermQuery(t); TopDocs docs = searcher.search(query, 10); assertEquals("Ant in Action", //C 1, docs.totalHits); //C t = new Term("subject", "junit"); docs = searcher.search(new TermQuery(t), 10); assertEquals("Ant in Action, " + //D "JUnit in Action, Second Edition", //D 2, docs.totalHits); //D dir.close();/*from w w w. jav a 2 s . com*/ }
From source file:com.mathworks.xzheng.searching.BasicSearchingTest.java
License:Apache License
public void testKeyword() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(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); dir.close();/*from w ww . j a v a 2 s . c o m*/ }
From source file:com.mathworks.xzheng.searching.BasicSearchingTest.java
License:Apache License
public void testQueryParser() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); QueryParser parser = new QueryParser(Version.LUCENE_46, //A "contents", //A new SimpleAnalyzer(Version.LUCENE_46)); //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 . com*/ 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); dir.close(); }
From source file:com.mathworks.xzheng.searching.BooleanQueryTest.java
License:Apache License
public void testAnd() throws Exception { TermQuery searchingBooks = new TermQuery(new Term("subject", "search")); //#1 Query books2010 = //#2 NumericRangeQuery.newIntRange("pubmonth", 201001, //#2 201012, //#2 true, true); //#2 BooleanQuery searchingBooks2010 = new BooleanQuery(); //#3 searchingBooks2010.add(searchingBooks, BooleanClause.Occur.MUST); //#3 searchingBooks2010.add(books2010, BooleanClause.Occur.MUST); //#3 Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); TopDocs matches = searcher.search(searchingBooks2010, 10); assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Lucene in Action, Second Edition")); dir.close();/*from w ww .j a v a 2 s. com*/ }
From source file:com.mathworks.xzheng.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(DirectoryReader.open(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")); dir.close();//from www . j a v a2 s. co m }
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 v a 2s .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(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(); }