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.ivannotes.searchbee.SearchBee.java
License:Apache License
/** * ?/*from www .ja v a 2s. c om*/ * * @param keyWords key words * @param page ? * @param pageSize ? * @return {@link PaginationResult} * @see PaginationResult * @throws IOException */ public final PaginationResult doPaginationQuery(String keyWords, int page, int pageSize, QueryBuilder queryBuilder) throws IOException { PaginationResult result = new PaginationResult(); result.setCurrentPage(page); result.setPageSize(pageSize); IndexSearcher idxSearch = getIndexSearcher(); Query query = queryBuilder.buildQuery(keyWords); TopDocs topDocs = idxSearch.search(query, MAX_RESULT); int totalHits = topDocs.totalHits; result.setTotalHits(totalHits); int start = page * pageSize; int end = start + pageSize; List<Document> docs = new ArrayList<Document>(); ScoreDoc[] scoreDocs = topDocs.scoreDocs; result.setDocs(docs); if (null == scoreDocs || scoreDocs.length == 0) { return result; } for (int i = start; i < totalHits; i++) { if (i > (end - 1)) { break; } docs.add(idxSearch.doc(scoreDocs[i].doc)); } if (logger.isDebugEnabled()) { logger.debug(String.format("search result: totalHits-%d curPage-%d pageSize-%d", result.getTotalHits(), result.getCurrentPage(), result.getPageSize())); } return result; }
From source file:com.javapr.plaintextindex.search.Suchen.java
License:Apache License
public void sucheString(String line) throws IOException, ParseException { frame = new JFrame(); frame.setTitle("Lucene Suche"); frame.setSize(400, 180);// w w w. j a v a 2 s . c om frame.setLocation(400, 400); frame.setVisible(true); panel = new JPanel(new BorderLayout(5, 5)); list = new JList(); panel1 = new JPanel(new GridLayout(1, 1)); panelLabel = new JPanel(new GridLayout(2, 1)); pane = new JScrollPane(panel1); labelAnzData = new JLabel(); alert = new JLabel(); suche = new JLabel(); dir = new JLabel(); panel.add(panelLabel, BorderLayout.WEST); panel.add(pane, BorderLayout.CENTER); frame.add(panel); String field = "contents"; int hitsPerPage = 10; try { IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(Prop.Indexdirectory(null)))); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_42); QueryParser parser = new QueryParser(Version.LUCENE_42, field, analyzer); list1 = new ArrayList<Object>(); try { Query query = parser.parse(line); suche = new JLabel("Suche nach: " + line); panelLabel.add(suche); TopDocs results = indexSearcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int end = hits.length; //Liste erzeugen und mit gefundenen Dateinamen fllen for (int i = 0; i < end; i++) { Document doc = indexSearcher.doc(hits[i].doc); String path = doc.get("filename"); File file = new File(Prop.Filestoindex(null) + "\\" + doc.get("filename")); if (path != null) { list1.add(path + ", " + file.length() / 1024 + "kb"); } } labelAnzData = new JLabel(hits.length + " Gefundene Datei(en): "); panelLabel.add(labelAnzData); list = new JList(list1.toArray()); panel1.add(list); } catch (ParseException p) { alert = new JLabel("String eingeben!"); panel.add(alert); ; } } catch (IOException e) { dir.setText("<html><body>Index-Datei nicht gefunden!<br>Bitte Index erstellen!</body></html>"); panel.add(dir); } }
From source file:com.leavesfly.lia.advsearching.MultiFieldQueryParserTest.java
License:Apache License
public void testDefaultOperator() throws Exception { Query query = new MultiFieldQueryParser(Version.LUCENE_30, new String[] { "title", "subject" }, new SimpleAnalyzer()).parse("development"); Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir, true); TopDocs hits = searcher.search(query, 10); assertTrue(TestUtil.hitsIncludeTitle(searcher, hits, "Ant in Action")); assertTrue(TestUtil.hitsIncludeTitle( // A searcher, // A hits, // A "Extreme Programming Explained")); // A searcher.close();//from w w w . j a va 2s .co m dir.close(); }
From source file:com.leavesfly.lia.advsearching.MultiFieldQueryParserTest.java
License:Apache License
public void testSpecifiedOperator() throws Exception { Query query = MultiFieldQueryParser.parse(Version.LUCENE_30, "lucene", new String[] { "title", "subject" }, new BooleanClause.Occur[] { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST }, new SimpleAnalyzer()); Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir, true); TopDocs hits = searcher.search(query, 10); assertTrue(TestUtil.hitsIncludeTitle(searcher, hits, "Lucene in Action, Second Edition")); assertEquals("one and only one", 1, hits.scoreDocs.length); searcher.close();// w w w . ja v a 2 s . c o m dir.close(); }
From source file:com.leavesfly.lia.advsearching.TimeLimitingCollectorTest.java
License:Apache License
public void testTimeLimitingCollector() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Query q = new MatchAllDocsQuery(); int numAllBooks = TestUtil.hitCount(searcher, q); TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false); Collector collector = new TimeLimitingCollector(topDocs, // #A 1000); // #A try {/*from w w w .j av a 2 s. c om*/ searcher.search(q, collector); assertEquals(numAllBooks, topDocs.getTotalHits()); // #B } catch (TimeExceededException tee) { // #C System.out.println("Too much time taken."); // #C } // #C searcher.close(); dir.close(); }
From source file:com.leavesfly.lia.commom.TestUtil.java
License:Apache License
public static int hitCount(IndexSearcher searcher, Query query) throws IOException { return searcher.search(query, 1).totalHits; }
From source file:com.leavesfly.lia.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 v a 2 s .co 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(); searcher.close(); }
From source file:com.leavesfly.lia.meetlucene.Fragments.java
License:Apache License
public void simpleSearch() throws IOException { Directory dir = FSDirectory.open(new File("/tmp/index")); IndexSearcher searcher = new IndexSearcher(dir); Query q = new TermQuery(new Term("contents", "lucene")); TopDocs hits = searcher.search(q, 10); searcher.close();//from www .j av a 2s. com }
From source file:com.leavesfly.lia.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(dir); //3 QueryParser parser = new QueryParser(Version.LUCENE_30, // 4 "contents", //4 new StandardAnalyzer( //4 Version.LUCENE_30)); //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 }/*from w w w. ja v a2 s. c o m*/ is.close(); //9 }
From source file:com.leavesfly.lia.searching.BasicSearchingTest.java
License:Apache License
public void testTerm() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); //A IndexSearcher searcher = new IndexSearcher(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 searcher.close();/*from w ww .j a v a2 s.c om*/ dir.close(); }