List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher
public IndexSearcher(IndexReaderContext context, Executor executor)
From source file:byrne.mitre.main.NameMatcher.java
License:Apache License
public static void main(String[] args) { try {// w w w . j av a 2 s. co m long startTime = System.currentTimeMillis(); System.out.println("Loading Index..."); final Analyzer analyzer = new NGramAnalyzer(2, 4); final Directory index = new RAMDirectory(); final IndexWriter writer = new IndexWriter(index, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); loadIndex("index.txt", writer); writer.close(); System.out.println("Running queries..."); final BufferedReader bufferedReader = new BufferedReader(new FileReader("queries.txt")); final BufferedWriter out = new BufferedWriter(new FileWriter("results.txt")); final IndexSearcher searcher = new IndexSearcher(index, true); String line = null; final int N_THREADS = Runtime.getRuntime().availableProcessors(); System.out.println("Total threads: " + N_THREADS); final ExecutorService executor = Executors.newFixedThreadPool(N_THREADS); while ((line = bufferedReader.readLine()) != null) { final NameEntry entry = new NameEntry(line); final MitreQuery q = new MitreQuery(entry, analyzer, searcher, out); executor.execute(q); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES); bufferedReader.close(); searcher.close(); out.close(); long endTime = System.currentTimeMillis(); System.out.println("Total run time: " + (endTime - startTime) / 60000 + " minutes"); } catch (IOException e) { System.out.println(e); } catch (InterruptedException e) { System.out.println(e); } }
From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java
License:BSD License
private synchronized IndexSearcher getSearcher() throws Exception { if (searcher == null) { searcher = new IndexSearcher(getDirectory(), true); }/*from w ww .j a va2 s .c o m*/ final IndexReader readerOld = searcher.getIndexReader(); final IndexReader readerNew = readerOld.reopen(true); /** get new instance only when index is updated */ if (readerNew != readerOld) { readerOld.close(); searcher = new IndexSearcher(readerNew); } return searcher; }
From source file:com.edgenius.wiki.search.lucene.ParallelSearcherFactory.java
License:Open Source License
@Override public IndexSearcher getSearcher() throws SearchException { try {/*from w ww . ja va 2 s .c o m*/ if (container.get() != null) { return container.get(); } IndexReader reader; if (directories.length > 1) { IndexReader[] readers = new IndexReader[directories.length]; for (int idx = 0; idx < directories.length; idx++) { readers[idx] = IndexReader.open(directories[idx], true); } reader = new MultiReader(readers, true); } else { reader = IndexReader.open(directories[0]); } IndexSearcher searcher = new IndexSearcher(reader, exectorService); container.set(searcher); return searcher; } catch (CorruptIndexException e) { throw new SearchException(e); } catch (IOException e) { throw new SearchException(e); } }
From source file:com.flaptor.indextank.index.lsi.LsiIndex.java
License:Apache License
private void reopenSearcher() { BlockingDeque<IndexSearcher> searcherPool = new LinkedBlockingDeque<IndexSearcher>(); BlockingDeque<QueryMatcher> matcherPool = new LinkedBlockingDeque<QueryMatcher>(); for (int i = 0; i < SEARCHER_POOL_SIZE; i++) { try {/*from w ww. ja v a 2s .com*/ IndexSearcher searcher = new IndexSearcher(directory, true); //read-only for better concurrent performance. TermMatcher termMatcher = new IndexReaderTermMatcher(searcher.getIndexReader(), PAYLOAD_TERM); QueryMatcher matcher = new TermBasedQueryMatcher(scorer, termMatcher, this.facetingManager); searcherPool.addFirst(searcher); //no blocking, throws exception. matcherPool.addFirst(matcher); } catch (CorruptIndexException cie) { logger.fatal("HORROR!!! corrupted index. unable to reopen", cie); } catch (IOException ioe) { logger.fatal("HORROR!!! IO exception. unable to reopen", ioe); } } searchObjects.set( new Pair<BlockingDeque<IndexSearcher>, BlockingDeque<QueryMatcher>>(searcherPool, matcherPool)); }
From source file:com.github.le11.nls.lucene.UIMATypeBasedSimilarityTest.java
License:Apache License
@Test public void baseSimilarityTest() { try {// w w w .ja va 2 s .co m IndexSearcher searcher = new IndexSearcher(dir, true); Similarity payloadSimilarity = new UIMATypeBasedSimilarity(); searcher.setSimilarity(payloadSimilarity); // BooleanQuery booleanQuery = new BooleanQuery(); // booleanQuery.add(new PayloadTermQuery(new Term("title", "London"), new MaxPayloadFunction()), BooleanClause.Occur.SHOULD); // booleanQuery.add(new PayloadTermQuery(new Term("title", "English"), new MaxPayloadFunction()), BooleanClause.Occur.SHOULD); // SpanQuery[] clauses = new SpanQuery[]{new PayloadTermQuery(new Term("title","London"),new MaxPayloadFunction()), // new PayloadTermQuery(new Term("title","English"),new MaxPayloadFunction())}; // int slop = 3; // boolean inOrder = true; // Query query = new PayloadNearQuery(clauses, slop, inOrder); Query directQuery = new TermQuery(new Term("title", "London")); TopDocs topDocs = searcher.search(directQuery, 10); System.out.println("Number of matching docs: " + topDocs.totalHits); ScoreDoc doc1 = topDocs.scoreDocs[0]; System.out.println("Doc: " + doc1.toString()); System.out.println("Explain: " + searcher.explain(directQuery, doc1.doc)); Query payloadQuery = new PayloadTermQuery(new Term("title", "London"), new MaxPayloadFunction()); topDocs = searcher.search(payloadQuery, 10); System.out.println("Number of matching docs: " + topDocs.totalHits); ScoreDoc doc2 = topDocs.scoreDocs[0]; System.out.println("Doc: " + doc2.toString()); System.out.println("Explain: " + searcher.explain(payloadQuery, doc2.doc)); assertTrue(doc1.score < doc2.score); } catch (Exception e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } }
From source file:com.leavesfly.lia.advsearching.FunctionQueryTest.java
License:Apache License
public void setUp() throws Exception { Directory dir = new RAMDirectory(); w = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED); addDoc(7, "this hat is green"); addDoc(42, "this hat is blue"); w.close();/*from w ww . j a va2 s.c om*/ s = new IndexSearcher(dir, true); }
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 .ja v a 2 s . c o 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();/* ww w . j av a 2 s.co m*/ dir.close(); }
From source file:com.leavesfly.lia.analysis.positional.PositionalPorterStopAnalyzerTest.java
License:Apache License
public void setUp() throws Exception { RAMDirectory directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, porterAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("contents", "The quick brown fox jumps over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from w ww .ja v a 2 s.com writer.close(); searcher = new IndexSearcher(directory, true); parser = new QueryParser(Version.LUCENE_30, "contents", porterAnalyzer); }
From source file:com.leavesfly.lia.analysis.synonym.SynonymAnalyzerTest.java
License:Apache License
public void setUp() throws Exception { RAMDirectory directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, synonymAnalyzer, // #1 IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); // #2 writer.addDocument(doc);//from www .j av a2 s . c o m writer.close(); searcher = new IndexSearcher(directory, true); }