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.sg.business.vault.index.demo.SearchFiles.java
License:Apache License
/** * This demonstrates a typical paging search scenario, where the search * engine presents pages of size n to the user. The user can then go to the * next page if interested in the next hits. * /*from w w w . j ava2 s . c om*/ * When the query is executed for the first time, then only enough results * are collected to fill 5 result pages. If the user wants to page beyond * this limit, then the query is executed another time and all hits are * collected. * */ public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage, boolean raw, boolean interactive) throws IOException { // Collect enough docs to show 5 pages TopDocs results = searcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; System.out.println(numTotalHits + " total matching documents"); //$NON-NLS-1$ int start = 0; int end = Math.min(numTotalHits, hitsPerPage); while (true) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length + " of " //$NON-NLS-1$ //$NON-NLS-2$ + numTotalHits + " total matching documents collected."); //$NON-NLS-1$ System.out.println("Collect more (y/n) ?"); //$NON-NLS-1$ String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'n') { break; } hits = searcher.search(query, numTotalHits).scoreDocs; } end = Math.min(hits.length, start + hitsPerPage); for (int i = start; i < end; i++) { if (raw) { // output raw format System.out.println("doc=" + hits[i].doc + " score=" //$NON-NLS-1$ //$NON-NLS-2$ + hits[i].score); continue; } Document doc = searcher.doc(hits[i].doc); String path = doc.get("filename"); //$NON-NLS-1$ if (path != null) { System.out.println((i + 1) + ". " + path); //$NON-NLS-1$ String title = doc.get("title"); //$NON-NLS-1$ if (title != null) { System.out.println(" Title: " + doc.get("title")); //$NON-NLS-1$ //$NON-NLS-2$ } } else { System.out.println((i + 1) + ". " //$NON-NLS-1$ + "No path for this document"); //$NON-NLS-1$ } } if (!interactive || end == 0) { break; } if (numTotalHits >= end) { boolean quit = false; while (true) { System.out.print("Press "); //$NON-NLS-1$ if (start - hitsPerPage >= 0) { System.out.print("(p)revious page, "); //$NON-NLS-1$ } if (start + hitsPerPage < numTotalHits) { System.out.print("(n)ext page, "); //$NON-NLS-1$ } System.out.println("(q)uit or enter number to jump to a page."); //$NON-NLS-1$ String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'q') { quit = true; break; } if (line.charAt(0) == 'p') { start = Math.max(0, start - hitsPerPage); break; } else if (line.charAt(0) == 'n') { if (start + hitsPerPage < numTotalHits) { start += hitsPerPage; } break; } else { int page = Integer.parseInt(line); if ((page - 1) * hitsPerPage < numTotalHits) { start = (page - 1) * hitsPerPage; break; } else { System.out.println("No such page"); //$NON-NLS-1$ } } } if (quit) break; end = Math.min(numTotalHits, start + hitsPerPage); } } }
From source file:com.shaie.annots.AnnotationSearchExample.java
License:Apache License
public static void main(String[] args) throws Exception { Directory dir = new RAMDirectory(); IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer()); IndexWriter writer = new IndexWriter(dir, conf); // we need to add the annotation as a TokenStream field, therefore cannot use an Analyzer passed in the // IndexWriterConfig. Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader("quick brown fox ate the blue red chicken")); TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer); TokenStream colorAnnotationStream = new AnnotatingTokenFilter( textStream.newSinkTokenStream(new ColorsSinkFilter()), COLOR_ANNOT_TERM); Document doc = new Document(); doc.add(new TextField("text", textStream)); doc.add(new TextField("annot", colorAnnotationStream)); writer.addDocument(doc);// w ww .ja va 2 s . c o m writer.close(); DirectoryReader reader = DirectoryReader.open(dir); LeafReader ar = reader.leaves().get(0).reader(); // we only have one segment printFieldTerms(ar, "text"); System.out.println(); final ByteArrayDataInput in = new ByteArrayDataInput(); PostingsEnum dape = ar.postings(new Term("annot", COLOR_ANNOT_TERM)); int docID = dape.nextDoc(); int freq = dape.freq(); System.out.println("Color annotation spans: doc=" + docID + ", freq=" + freq); for (int i = 0; i < freq; i++) { dape.nextPosition(); BytesRef payload = dape.getPayload(); in.reset(payload.bytes, payload.offset, payload.length); System.out.println(" start=" + in.readVInt() + ", length=" + in.readVInt()); } IndexSearcher searcher = new IndexSearcher(reader); System.out.println("\nsearching for 'red WITHIN color':"); Query q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)), new SpanInclusivePositionTermQuery(new Term("text", "red"))); TopDocs td = searcher.search(q, 10); System.out.println(" num results: " + td.scoreDocs.length); System.out.println("\nsearching for 'ate WITHIN color':"); q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)), new SpanInclusivePositionTermQuery(new Term("text", "ate"))); td = searcher.search(q, 10); System.out.println(" num results: " + td.scoreDocs.length); reader.close(); dir.close(); }
From source file:com.shaie.annots.example.AnnotatorAnyExample.java
License:Apache License
private static void search(IndexSearcher searcher, Query q) throws IOException { System.out.println(format("Searching for [%s]:", q)); final TopDocs results = searcher.search(q, 10); for (final ScoreDoc sd : results.scoreDocs) { System.out.println(format(" doc=%d, text=%s", sd.doc, searcher.doc(sd.doc).get(TEXT_FIELD))); }//www .jav a2 s . c o m }
From source file:com.shaie.facet.NotDrillDownExample.java
License:Apache License
public static void main(String[] args) throws Exception { createIndex();// www.j a v a 2 s . c o m try (DirectoryReader indexReader = DirectoryReader.open(indexDir); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);) { final IndexSearcher searcher = new IndexSearcher(indexReader); // Find the index field which holds the 'Author' facets final String indexedField = config.getDimConfig(AUTHOR_FACET).indexFieldName; final Query q = new BooleanQuery.Builder() // Here you would usually use a different query .add(new MatchAllDocsQuery(), Occur.MUST) // Exclude results with Author/Lisa .add(new TermQuery(DrillDownQuery.term(indexedField, AUTHOR_FACET, "Lisa")), Occur.MUST_NOT) .build(); final TopDocs topDocs = searcher.search(q, 10); assert topDocs.scoreDocs.length == 1 : "should have found 1 document with Author/Bob"; final Document doc = searcher.doc(topDocs.scoreDocs[0].doc); System.out.println(doc); } }
From source file:com.shaie.PhraseVsSpanQuery.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { final Directory dir = new RAMDirectory(); final IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer()); final IndexWriter writer = new IndexWriter(dir, conf); final Document doc = new Document(); doc.add(new TextField("f", new TokenStream() { final PositionIncrementAttribute pos = addAttribute(PositionIncrementAttribute.class); final CharTermAttribute term = addAttribute(CharTermAttribute.class); boolean first = true, done = false; @Override/*from w w w . jav a 2s . c om*/ public boolean incrementToken() throws IOException { if (done) { return false; } if (first) { term.setEmpty().append("a"); pos.setPositionIncrement(1); first = false; } else { term.setEmpty().append("b"); pos.setPositionIncrement(0); done = true; } return true; } })); writer.addDocument(doc); writer.close(); final DirectoryReader reader = DirectoryReader.open(dir); final IndexSearcher searcher = new IndexSearcher(reader); final LeafReader ar = reader.leaves().get(0).reader(); final TermsEnum te = ar.terms("f").iterator(); BytesRef scratch = new BytesRef(); while ((scratch = te.next()) != null) { System.out.println(scratch.utf8ToString()); final PostingsEnum dape = ar.postings(new Term("f", scratch.utf8ToString())); System.out.println(" doc=" + dape.nextDoc() + ", pos=" + dape.nextPosition()); } System.out.println(); // try a phrase query with a slop final PhraseQuery pqNoSlop = buildPhraseQuery(0); System.out.println("searching for \"a b\"; num results = " + searcher.search(pqNoSlop, 10).totalHits); final PhraseQuery pqSlop1 = buildPhraseQuery(1); System.out.println("searching for \"a b\"~1; num results = " + searcher.search(pqSlop1, 10).totalHits); final PhraseQuery pqSlop3 = buildPhraseQuery(3); System.out.println("searching for \"a b\"~3; num results = " + searcher.search(pqSlop3, 10).totalHits); final SpanNearQuery snqUnOrdered = new SpanNearQuery( new SpanQuery[] { new SpanTermQuery(new Term("f", "a")), new SpanTermQuery(new Term("f", "b")) }, 1, false); System.out.println("searching for SpanNearUnordered('a', 'b'), slop=1; num results = " + searcher.search(snqUnOrdered, 10).totalHits); final SpanNearQuery snqOrdered = new SpanNearQuery( new SpanQuery[] { new SpanTermQuery(new Term("f", "a")), new SpanTermQuery(new Term("f", "b")) }, 1, true); System.out.println("searching for SpanNearOrdered('a', 'b'), slop=1; num results = " + searcher.search(snqOrdered, 10).totalHits); reader.close(); }
From source file:com.shaie.UTF8Indexing.java
License:Apache License
private static void search(IndexSearcher searcher, QueryParser qp, String text) throws Exception { System.out.println("search for [" + text + "]: " + searcher.search(qp.parse(text), 10).totalHits); }
From source file:com.silverwrist.dynamo.index.IndexServiceImpl.java
License:Mozilla Public License
private final List doQuery(Query query, int offset, int count) throws IndexException { SubsetCollector subc = new SubsetCollector(offset, count + 1); List rc = null;/*from w w w. jav a2 s .c o m*/ IndexReader irdr = null; IndexSearcher srch = null; try { // run that puppy! irdr = IndexReader.open(m_directory); srch = new IndexSearcher(irdr); if (query == null) visitAllDocuments(srch, subc); else srch.search(query, subc); rc = subc.outputItems(irdr); } // end try catch (IOException e) { // the query failed somehow - throw an error throw new IndexException(IndexServiceImpl.class, "IndexMessages", "query.fail", e); } // end catch finally { // make sure we close down OK try { // close the search and index reader if (srch != null) srch.close(); if (irdr != null) irdr.close(); } // end try catch (IOException e) { // shouldn't happen logger.warn("query(): error closing stuff", e); } // end catch } // end finally return rc; }
From source file:com.silverwrist.dynamo.index.IndexServiceImpl.java
License:Mozilla Public License
private final int doQueryCount(Query query) throws IndexException { CountingCollector cc = new CountingCollector(); IndexSearcher srch = null; try { // run that puppy! srch = new IndexSearcher(m_directory); if (query == null) visitAllDocuments(srch, cc); else//w w w . j a va 2 s . c o m srch.search(query, cc); } // end try catch (IOException e) { // the query failed somehow - throw an error throw new IndexException(IndexServiceImpl.class, "IndexMessages", "query.fail", e); } // end catch finally { // make sure we close down OK try { // close the search and index reader if (srch != null) srch.close(); } // end try catch (IOException e) { // shouldn't happen logger.warn("queryCount(): error closing stuff", e); } // end catch } // end finally return cc.getCount(); }
From source file:com.sindicetech.siren.search.node.TestNodeFuzzyQuery.java
License:Open Source License
/** * MultiTermQuery provides (via attribute) information about which values * must be competitive to enter the priority queue. * * FuzzyQuery optimizes itself around this information, if the attribute * is not implemented correctly, there will be problems! *//*from w ww . ja va2 s. co m*/ public void testTieBreaker() throws Exception { this.addDocument("<a123456>"); this.addDocument("<c123456>"); this.addDocument("<d123456>"); this.addDocument("<e123456>"); final Directory directory2 = newDirectory(); final RandomIndexWriter writer2 = newRandomIndexWriter(directory2, analyzer, codec); addDocument(writer2, "<a123456>"); addDocument(writer2, "<b123456>"); addDocument(writer2, "<b123456>"); addDocument(writer2, "<b123456>"); addDocument(writer2, "<c123456>"); addDocument(writer2, "<f123456>"); final IndexReader ir1 = writer.getReader(); final IndexReader ir2 = writer2.getReader(); final MultiReader mr = new MultiReader(ir1, ir2); final IndexSearcher searcher = newSearcher(mr); final FuzzyQuery fq = new FuzzyQuery(new Term(DEFAULT_TEST_FIELD, "z123456"), 1, 0, 2, false); final TopDocs docs = searcher.search(fq, 2); assertEquals(5, docs.totalHits); // 5 docs, from the a and b's mr.close(); ir2.close(); writer2.close(); directory2.close(); }
From source file:com.slieer.app.lecene3x.LuceneIndexAndSearchDemo.java
License:Apache License
/** * ???// w w w . j av a2 s . c o m * * @param args */ public static void main(String[] args) { // Lucene Document?? String fieldName = "text"; // String text = "IK Analyzer???????"; String text1 = "? (Chinese Word Segmentation) ???????????"; String text2 = "?????,,??,?"; // IKAnalyzer? Analyzer analyzer = new IKAnalyzer(true); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); // ?IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_4_9, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); //document.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED)); Field strField = new StringField("ID", "10000", Field.Store.YES); Field textFild = new StringField(fieldName, text, Field.Store.YES); //textFild.setBoost(2); doc.add(strField); doc.add(textFild); iwriter.addDocument(doc); doc = new Document(); strField = new StringField("ID", "10001", Field.Store.YES); textFild = new StringField(fieldName, text1, Field.Store.YES); //strField.setBoost(1); doc.add(strField); doc.add(textFild); iwriter.addDocument(doc); doc = new Document(); strField = new StringField("ID", "10002", Field.Store.YES); // textFild = new TextField(fieldName, text2, Field.Store.YES); textFild = new StringField(fieldName, text2, Field.Store.YES); //strField.setBoost(1); doc.add(strField); doc.add(textFild); iwriter.addDocument(doc); iwriter.close(); // ?********************************** // ? ireader = DirectoryReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; // QueryParser?Query QueryParser qp = new QueryParser(Version.LUCENE_4_9, fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); System.out.println("Query = " + query); // ?5? TopDocs topDocs = isearcher.search(query, 5); System.out.println("" + topDocs.totalHits); // ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < topDocs.totalHits; i++) { Document targetDoc = isearcher.doc(scoreDocs[i].doc); System.out.println("" + targetDoc.toString()); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (ireader != null) { try { ireader.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory != null) { try { directory.close(); } catch (IOException e) { e.printStackTrace(); } } } }