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.b2international.index.lucene.SearchWarmerFactory.java
License:Apache License
@Override public IndexSearcher newSearcher(IndexReader reader, IndexReader previousReader) throws IOException { IndexSearcher searcher = super.newSearcher(reader, previousReader); // TODO: experiment with different queries (MatchAllDocs, a set of "typical" queries, etc.) final BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add(new TermQuery(new Term(EMPTY_STRING, EMPTY_STRING)), Occur.MUST); searcher.search(query.build(), new TotalHitCountCollector()); return searcher; }
From source file:com.b2international.snowowl.snomed.api.impl.ClassificationRunIndex.java
License:Apache License
public void invalidateClassificationRuns() throws IOException { final Query statusQuery = Fields.newQuery().field(FIELD_STATUS, ClassificationStatus.COMPLETED.name()) .field(FIELD_STATUS, ClassificationStatus.RUNNING.name()) .field(FIELD_STATUS, ClassificationStatus.SAVING_IN_PROGRESS.name()) .field(FIELD_STATUS, ClassificationStatus.SCHEDULED.name()).matchAny(); final Query query = Fields.newQuery().field(FIELD_CLASS, ClassificationRun.class.getSimpleName()) .and(statusQuery).matchAll(); IndexSearcher searcher = null; try {/*from ww w .j a v a2s. c o m*/ searcher = manager.acquire(); final TotalHitCountCollector collector = new TotalHitCountCollector(); searcher.search(query, collector); final int totalHits = collector.getTotalHits(); final int docsToRetrieve = Ints.min(searcher.getIndexReader().maxDoc(), totalHits); if (docsToRetrieve < 1) { return; } final TopDocs docs = searcher.search(query, docsToRetrieve, Sort.INDEXORDER, false, false); final ScoreDoc[] scoreDocs = docs.scoreDocs; final ObjectReader reader = objectMapper.reader(ClassificationRun.class); for (int i = 0; i < scoreDocs.length; i++) { final Document sourceDocument = searcher.doc(scoreDocs[i].doc, ImmutableSet.of(FIELD_BRANCH_PATH, FIELD_SOURCE)); final String branchPath = sourceDocument.get(FIELD_BRANCH_PATH); final String source = sourceDocument.get(FIELD_SOURCE); final ClassificationRun run = reader.readValue(source); run.setStatus(ClassificationStatus.STALE); upsertClassificationRunNoCommit(branchPath, run); } commit(); } finally { if (null != searcher) { manager.release(searcher); } } }
From source file:com.b2international.snowowl.snomed.api.impl.ClassificationRunIndex.java
License:Apache License
private <T> List<T> search(final Query query, final Class<? extends T> sourceClass, Sort sort, final int offset, final int limit) throws IOException { IndexSearcher searcher = null; try {//from w ww .j av a 2 s. c o m searcher = manager.acquire(); final TotalHitCountCollector collector = new TotalHitCountCollector(); searcher.search(query, collector); final int totalHits = collector.getTotalHits(); final int saturatedSum = Ints.saturatedCast((long) offset + limit); final int docsToRetrieve = Ints.min(saturatedSum, searcher.getIndexReader().maxDoc(), totalHits); final ImmutableList.Builder<T> resultBuilder = ImmutableList.builder(); if (docsToRetrieve < 1) { return resultBuilder.build(); } final TopDocs docs = searcher.search(query, docsToRetrieve, sort, false, false); final ScoreDoc[] scoreDocs = docs.scoreDocs; final ObjectReader reader = objectMapper.reader(sourceClass); for (int i = offset; i < docsToRetrieve && i < scoreDocs.length; i++) { final Document sourceDocument = searcher.doc(scoreDocs[i].doc, ImmutableSet.of(FIELD_SOURCE)); final String source = sourceDocument.get(FIELD_SOURCE); final T deserializedSource = reader.readValue(source); resultBuilder.add(deserializedSource); } return resultBuilder.build(); } finally { if (null != searcher) { manager.release(searcher); } } }
From source file:com.b2international.snowowl.snomed.api.impl.ClassificationRunIndex.java
License:Apache License
private int getHitCount(final Query query) throws IOException { IndexSearcher searcher = null; try {//from w w w .java2 s . co m searcher = manager.acquire(); final TotalHitCountCollector collector = new TotalHitCountCollector(); searcher.search(query, collector); return collector.getTotalHits(); } finally { if (null != searcher) { manager.release(searcher); } } }
From source file:com.bah.lucene.BaseDirectoryTestSuite.java
License:Apache License
@Test public void testCreateIndex() throws IOException { long s = System.nanoTime(); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer()); FSDirectory control = FSDirectory.open(fileControl); Directory dir = getControlDir(control, directory); // The serial merge scheduler can be useful for debugging. // conf.setMergeScheduler(new SerialMergeScheduler()); IndexWriter writer = new IndexWriter(dir, conf); int numDocs = 10000; DirectoryReader reader = null;// ww w .jav a 2s . c om for (int i = 0; i < 100; i++) { if (reader == null) { reader = DirectoryReader.open(writer, true); } else { DirectoryReader old = reader; reader = DirectoryReader.openIfChanged(old, writer, true); if (reader == null) { reader = old; } else { old.close(); } } assertEquals(i * numDocs, reader.numDocs()); IndexSearcher searcher = new IndexSearcher(reader); NumericRangeQuery<Integer> query = NumericRangeQuery.newIntRange("id", 42, 42, true, true); TopDocs topDocs = searcher.search(query, 10); assertEquals(i, topDocs.totalHits); addDocuments(writer, numDocs); } writer.close(false); reader.close(); long e = System.nanoTime(); System.out.println("Total time [" + (e - s) / 1000000.0 + " ms]"); }
From source file:com.bah.lucene.blockcache_v2.CacheDirectoryTest.java
License:Apache License
@Test public void test3() throws IOException, InterruptedException { // Thread.sleep(30000); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer()); IndexWriter writer = new IndexWriter(_cacheDirectory, conf); int docs = 100000; for (int i = 0; i < docs; i++) { if (i % 500 == 0) { System.out.println(i); }/*from ww w. j av a2 s .c o m*/ writer.addDocument(newDoc()); // Thread.sleep(1); } writer.close(); System.out.println("done writing"); DirectoryReader reader = DirectoryReader.open(_cacheDirectory); System.out.println("done opening"); assertEquals(docs, reader.numDocs()); Document document = reader.document(0); System.out.println("done fetching"); System.out.println(document); IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(new TermQuery(new Term("test", "test")), 10); System.out.println("done searching"); assertEquals(docs, topDocs.totalHits); reader.close(); }
From source file:com.barchart.feed.ddf.resolver.provider.CodecHelper.java
License:BSD License
static boolean isPresent(final IndexSearcher searcher, final Instrument instrument) throws Exception { final Term term = getKeyTerm(instrument); final Query query = new TermQuery(term); final int hits = searcher.search(query, 1).totalHits; return hits > 0; }
From source file:com.barchart.feed.ddf.resolver.provider.CodecHelper.java
License:BSD License
static boolean isPresent(final IndexSearcher searcher, final String symbol) throws Exception { final String name = CodecHelper.FIELD_INST_ID; final String value = symbol; final Term term = new Term(name, value); final Query query = new TermQuery(term); final int hits = searcher.search(query, 1).totalHits; return hits > 0; }
From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java
License:BSD License
private List<Document> searchDocument(final Query query) throws Exception { final IndexSearcher searcher = getSearcher(); final TopScoreDocCollector collector = TopScoreDocCollector.create(limit, true); searcher.search(query, collector); final ScoreDoc[] hits = collector.topDocs().scoreDocs; final int size = Math.min(hits.length, limit); log.debug("hits size : {}", size); final List<Document> list = new ArrayList<Document>(size); for (int k = 0; k < size; k++) { final int index = hits[k].doc; final Document doc = searcher.doc(index); list.add(doc);/* www . ja v a 2s . c o m*/ } return list; }
From source file:com.basistech.lucene.tools.LuceneQueryTool.java
License:Apache License
private void runQuery(String queryString, final PrintStream out) throws IOException, org.apache.lucene.queryparser.classic.ParseException { final IndexSearcher searcher = new IndexSearcher(indexReader); docsPrinted = 0;/*from ww w. j a v a 2 s. c o m*/ Query query; if (queryString == null) { query = new MatchAllDocsQuery(); } else { if (!queryString.contains(":") && defaultField == null) { throw new RuntimeException("query has no ':' and no query-field defined"); } QueryParser queryParser = new QueryParser(defaultField, analyzer); queryParser.setLowercaseExpandedTerms(false); query = queryParser.parse(queryString).rewrite(indexReader); Set<Term> terms = Sets.newHashSet(); query.createWeight(searcher, false).extractTerms(terms); List<String> invalidFieldNames = Lists.newArrayList(); for (Term term : terms) { if (!allFieldNames.contains(term.field())) { invalidFieldNames.add(term.field()); } } if (!invalidFieldNames.isEmpty()) { throw new RuntimeException("Invalid field names: " + invalidFieldNames); } } final Set<String> fieldSet = Sets.newHashSet(fieldNames); // use a Collector instead of TopDocs for memory efficiency, especially // for the %all query class MyCollector extends SimpleCollector { private Scorer scorer; private long totalHits; private int docBase; @Override protected void doSetNextReader(LeafReaderContext context) throws IOException { docBase = context.docBase; } @Override public void collect(int id) throws IOException { totalHits++; if (docsPrinted >= outputLimit) { return; } id += docBase; Document doc = fieldSet.isEmpty() ? searcher.doc(id) : searcher.doc(id, fieldSet); boolean passedFilter = regexField == null; if (regexField != null) { String value = doc.get(regexField); if (value != null && regex.matcher(value).matches()) { passedFilter = true; } } if (passedFilter) { float score = scorer.score(); printDocument(doc, id, score, out); } } @Override public boolean needsScores() { return true; } @Override public void setScorer(Scorer scorer) throws IOException { this.scorer = scorer; } } MyCollector collector = new MyCollector(); searcher.search(query, collector); if (showHits) { out.println("totalHits: " + collector.totalHits); out.println(); } }