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.miliworks.virgo.test.LuceneIndexAndSearchDemo.java
License:Apache License
/** * // ww w . j a v a 2 s.c om * ??? * @param args */ public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; // String text = "IK Analyzer???????"; //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_40, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new StringField("ID", "10000", Field.Store.YES)); doc.add(new TextField(fieldName, text, Field.Store.YES)); iwriter.addDocument(doc); iwriter.close(); //?********************************** //? ireader = DirectoryReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; //QueryParser?Query QueryParser qp = new QueryParser(Version.LUCENE_40, 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(); } } } }
From source file:com.mycompany.lucenedemo.SearchFiles.java
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage] [-sim vsm or bm25]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; if (args.length == 0 || (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0])))) { System.out.println(usage); System.exit(0);//from w w w. j ava2s. c o m } String index = "index"; String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; SimilarityScore score = SimilarityScore.DEFAULT; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { index = args[i + 1]; i++; } else if ("-field".equals(args[i])) { field = args[i + 1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i + 1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i + 1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i + 1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i + 1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } else if ("-sim".equals(args[i])) { if (args[i + 1].equals("vsm")) { score = SimilarityScore.VSM; } else { score = SimilarityScore.BM25; } } } IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); BufferedReader in = null; if (queries != null) { in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8); } else { in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); } QueryParser parser = new QueryParser(field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); switch (score) { case DEFAULT: break; case VSM: searcher.setSimilarity(new ClassicSimilarity()); break; case BM25: searcher.setSimilarity(new BM25Similarity()); break; } if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
From source file:com.mycompany.lucenedemo.SearchFiles.java
/** * 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 ww. ja va 2 s .c o m * * 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 = Math.toIntExact(results.totalHits); System.out.println(numTotalHits + " total matching documents"); int start = 0; int end = Math.min(numTotalHits, hitsPerPage); while (true) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits + " total matching documents collected."); System.out.println("Collect more (y/n) ?"); 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++) { Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println((i + 1) + ". " + path); String title = doc.get("title"); if (title != null) { System.out.println(" Title: " + doc.get("title")); } } else { System.out.println((i + 1) + ". " + "No path for this document"); } if (raw) { // output raw format System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score); System.out.println(searcher.explain(query, hits[i].doc)); } } if (!interactive || end == 0) { break; } if (numTotalHits >= end) { boolean quit = false; while (true) { System.out.print("Press "); if (start - hitsPerPage >= 0) { System.out.print("(p)revious page, "); } if (start + hitsPerPage < numTotalHits) { System.out.print("(n)ext page, "); } System.out.println("(q)uit or enter number to jump to a page."); 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"); } } } if (quit) break; end = Math.min(numTotalHits, start + hitsPerPage); } } }
From source file:com.mycompany.mavenproject1.Main.java
public static void main(String[] args) throws IOException, ParseException { StandardAnalyzer analyzer = new StandardAnalyzer(); // Directory index = new RAMDirectory(); Directory index = new SimpleFSDirectory(Paths.get( "C:\\Users\\slete\\Documents\\NetBeansProjects\\mavenproject1\\src\\main\\java\\com\\mycompany\\mavenproject1\\data")); IndexWriterConfig config = new IndexWriterConfig(analyzer); //config.setOpenMode(OpenMode.CREATE); IndexWriter w = new IndexWriter(index, config); try (ItemProvider provider = new ItemProvider( "C:\\Users\\slete\\Documents\\NetBeansProjects\\mavenproject1\\src\\main\\java\\com\\mycompany\\mavenproject1\\items.xml")) { while (provider.hasNext()) { Item item = provider.next(); addItem(w, item);/*from w w w. j a v a 2 s . c om*/ } } catch (XMLStreamException | IOException ex) { ex.getMessage(); } // w.commit(); w.close(); // String queryStr = "id:1* NOT id:19*"; String a = "id:1* NOT id:19*"; String b = "name:Dekielek AND description:(ty AND obiektywu)"; String c = "category:Dek*"; String ds = "id:1232~2"; String e = "price:[0.0 TO 100.0]"; Query q = new QueryParser("name", analyzer).parse(ds); int hitsPerPage = 10; IndexReader reader = DirectoryReader.open(index); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(q, hitsPerPage); ScoreDoc[] hits = docs.scoreDocs; System.out.println("Found " + hits.length + " hits."); for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = searcher.doc(docId); System.out .println(d.get("id") + "\t" + d.get("price") + "\t" + d.get("name") + "\t" + d.get("category"));//+"\t" + d.get("description")); } }
From source file:com.mycompany.restlet.search.sample.searcher.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.//w ww.j a v a2s . 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"); int start = 0; int end = Math.min(numTotalHits, hitsPerPage); while (true) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits + " total matching documents collected."); System.out.println("Collect more (y/n) ?"); 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=" + hits[i].score); continue; } Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println((i + 1) + ". " + path); String title = doc.get("title"); if (title != null) { System.out.println(" Title: " + doc.get("title")); } } else { System.out.println((i + 1) + ". " + "No path for this document"); } } if (!interactive || end == 0) { break; } if (numTotalHits >= end) { boolean quit = false; while (true) { System.out.print("Press "); if (start - hitsPerPage >= 0) { System.out.print("(p)revious page, "); } if (start + hitsPerPage < numTotalHits) { System.out.print("(n)ext page, "); } System.out.println("(q)uit or enter number to jump to a page."); 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"); } } } if (quit) break; end = Math.min(numTotalHits, start + hitsPerPage); } } }
From source file:com.nearinfinity.blur.manager.IndexManager.java
License:Apache License
private void populateSelector(String table, Selector selector) throws IOException, BlurException { String rowId = selector.rowId; String recordId = selector.recordId; String shardName = MutationHelper.getShardName(table, rowId, getNumberOfShards(table), _blurPartitioner); Map<String, BlurIndex> indexes = _indexServer.getIndexes(table); BlurIndex blurIndex = indexes.get(shardName); if (blurIndex == null) { throw new BlurException("Shard [" + shardName + "] is not being servered by this shardserver.", null); }// ww w.j a va 2 s. c o m IndexReader reader = blurIndex.getIndexReader(); try { IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery query = new BooleanQuery(); if (selector.recordOnly) { query.add(new TermQuery(new Term(RECORD_ID, recordId)), Occur.MUST); query.add(new TermQuery(new Term(ROW_ID, rowId)), Occur.MUST); } else { query.add(new TermQuery(new Term(ROW_ID, rowId)), Occur.MUST); query.add(new TermQuery(BlurConstants.PRIME_DOC_TERM), Occur.MUST); } TopDocs topDocs = searcher.search(query, 1); if (topDocs.totalHits > 1) { if (selector.recordOnly) { LOG.warn("Rowid [" + rowId + "], recordId [" + recordId + "] has more than one prime doc that is not deleted."); } else { LOG.warn("Rowid [" + rowId + "] has more than one prime doc that is not deleted."); } } if (topDocs.totalHits == 1) { selector.setLocationId(shardName + "/" + topDocs.scoreDocs[0].doc); } else { selector.setLocationId(NOT_FOUND); } } finally { // this will allow for closing of index reader.decRef(); } }
From source file:com.nearinfinity.blur.search.RandomSuperQueryTest.java
License:Apache License
@Test public void testRandomSuperQuery() throws CorruptIndexException, IOException, InterruptedException, ParseException { long seed = seedGen.nextLong(); Filter filter = new QueryWrapperFilter(new MatchAllDocsQuery()); Random random = new Random(seed); Collection<String> sampler = new HashSet<String>(); System.out.print("Creating index... "); System.out.flush();// www . j ava 2 s . com Directory directory = createIndex(random, sampler); IndexReader reader = IndexReader.open(directory); System.out.print("Running searches [" + sampler.size() + "]... "); System.out.flush(); assertTrue(!sampler.isEmpty()); IndexSearcher searcher = new IndexSearcher(reader); long s = System.currentTimeMillis(); for (String str : sampler) { Query query = new SuperParser(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION), true, filter, ScoreType.AGGREGATE).parse(str); TopDocs topDocs = searcher.search(query, 10); assertTrue("seed [" + seed + "] {" + query + "} {" + s + "}", topDocs.totalHits > 0); } long e = System.currentTimeMillis(); System.out.println("Finished in [" + (e - s) + "] ms"); }
From source file:com.nearinfinity.blur.search.SuperQueryTest.java
License:Apache License
@Test public void testSimpleSuperQuery() throws CorruptIndexException, IOException, InterruptedException { BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(wrapSuper(new TermQuery(new Term(PERSON_NAME, NAME1))), Occur.MUST); booleanQuery.add(wrapSuper(new TermQuery(new Term(ADDRESS_STREET, STREET1))), Occur.MUST); Directory directory = createIndex(); IndexReader reader = IndexReader.open(directory); printAll(new Term(PERSON_NAME, NAME1), reader); printAll(new Term(ADDRESS_STREET, STREET1), reader); printAll(new Term(PRIME_DOC, PRIME_DOC_VALUE), reader); IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(booleanQuery, 10); assertEquals(2, topDocs.totalHits);/*from www .j a v a2s . c o m*/ assertEquals("1", searcher.doc(topDocs.scoreDocs[0].doc).get(ROW_ID)); assertEquals("3", searcher.doc(topDocs.scoreDocs[1].doc).get(ROW_ID)); }
From source file:com.nearinfinity.blur.search.SuperQueryTest.java
License:Apache License
@Test public void testAggregateScoreTypes() throws Exception { IndexSearcher searcher = createSearcher(); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.AGGREGATE), Occur.SHOULD); booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.AGGREGATE), Occur.MUST); TopDocs topDocs = searcher.search(booleanQuery, 10); printTopDocs(topDocs);//w ww . j a va2 s . co m assertEquals(3, topDocs.totalHits); assertEquals(3.30, topDocs.scoreDocs[0].score, 0.01); assertEquals(2.20, topDocs.scoreDocs[1].score, 0.01); assertEquals(0.55, topDocs.scoreDocs[2].score, 0.01); }
From source file:com.nearinfinity.blur.search.SuperQueryTest.java
License:Apache License
@Test public void testBestScoreTypes() throws Exception { IndexSearcher searcher = createSearcher(); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.BEST), Occur.SHOULD); booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.BEST), Occur.MUST); TopDocs topDocs = searcher.search(booleanQuery, 10); assertEquals(3, topDocs.totalHits);//from w w w . ja v a 2s . c o m printTopDocs(topDocs); assertEquals(2.20, topDocs.scoreDocs[0].score, 0.01); assertEquals(2.20, topDocs.scoreDocs[1].score, 0.01); assertEquals(0.55, topDocs.scoreDocs[2].score, 0.01); }