Example usage for org.apache.lucene.search IndexSearcher count

List of usage examples for org.apache.lucene.search IndexSearcher count

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher count.

Prototype

public int count(Query query) throws IOException 

Source Link

Document

Count how many documents match the given query.

Usage

From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImplJUnitPerformanceTest.java

License:Apache License

@Test
public void testLuceneWithRegionDirectory() throws Exception {
    doTest("RegionDirectory", new TestCallbacks() {

        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override/*from  w w w  . j  a v a  2 s. c  o m*/
        public void init() throws Exception {
            RegionDirectory dir = new RegionDirectory(new ConcurrentHashMap<String, File>(),
                    new ConcurrentHashMap<ChunkKey, byte[]>());
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
        }

        @Override
        public void waitForAsync() throws Exception {
            //do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImplJUnitPerformanceTest.java

License:Apache License

@Test
public void testLucene() throws Exception {
    doTest("Lucene", new TestCallbacks() {

        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override//from w w w.j ava2s  .co m
        public void init() throws Exception {
            RAMDirectory dir = new RAMDirectory();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
        }

        @Override
        public void waitForAsync() throws Exception {
            //do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLuceneWithRegionDirectory() throws Exception {
    doTest("RegionDirectory", new TestCallbacks() {

        public Cache cache;
        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override//  www .  j a v  a  2  s  .  co m
        public void init() throws Exception {
            cache = new CacheFactory().set(MCAST_PORT, "0").set(LOG_LEVEL, "warning").create();
            final FileSystemStats stats = new FileSystemStats(cache.getDistributedSystem(), "stats");
            RegionDirectory dir = new RegionDirectory(new ConcurrentHashMap<String, File>(),
                    new ConcurrentHashMap<ChunkKey, byte[]>(), stats);
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
            cache.close();
            ;
        }

        @Override
        public void waitForAsync() throws Exception {
            //do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLucene() throws Exception {
    doTest("Lucene", new TestCallbacks() {

        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override/*from  ww w . j  a  v a  2  s. c  o m*/
        public void init() throws Exception {
            RAMDirectory dir = new RAMDirectory();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
        }

        @Override
        public void waitForAsync() throws Exception {
            //do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:com.rapidminer.search.GlobalSearchHandler.java

License:Open Source License

/**
 * Executes the search with the given parameters.
 *
 * @param searchQueryString/*ww w. j a  v  a 2s .c o  m*/
 *       the search query string, must neither be {@code null} nor empty
 * @param categories
 *       <i>Optional</i>. If {@code null} or not specified, all registered search categories are included in the
 *       search.
 * @param simpleMode
 *       if {@code true}, will try to make the query automatically user-friendly; {@code false} will not make any changes to the query
 * @param maxNumberOfResults
 *       the maximum number of results
 * @param moreResults
 *       the number of optional additional search results. Will add these results if there are no additional results.
 *        Will not be used if parameter 'after' is set.
 * @param highlightResult
 *       if {@code true}, the {@link GlobalSearchResult#getBestFragments()} will be created
 * @param after
 *       <i>Optional</i>. If not {@code null}, then the search results are retrieved from ranks lower than the given
 *       {@link ScoreDoc}.
 * @return the search result, never {@code null}
 * @throws ParseException
 *       if the searchQuery was invalid
 */
protected GlobalSearchResult search(final String searchQueryString, final List<GlobalSearchCategory> categories,
        final boolean simpleMode, final int maxNumberOfResults, final int moreResults,
        final boolean highlightResult, final ScoreDoc after) throws ParseException {
    if (searchQueryString == null || searchQueryString.trim().isEmpty()) {
        return GlobalSearchResult.createEmptyResult(searchQueryString);
    }
    // build the query based on the default search fields, which can be specified in the search string anyway though
    try (IndexReader reader = GlobalSearchIndexer.INSTANCE.createIndexReader()) {
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs result;
        Query finalQuery;

        Query parsedQuery = parseDefaultQuery(searchQueryString, categories, simpleMode);
        if (categories == null) {
            finalQuery = parsedQuery;
        } else {
            // categories are restricted, create query for that and combine with original query via AND
            BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
            Query categoryQuery = createCategoryQuery(categories);
            finalQuery = booleanQueryBuilder.add(parsedQuery, BooleanClause.Occur.MUST)
                    .add(categoryQuery, BooleanClause.Occur.MUST).build();
        }

        // sort by the sort field, if provided
        if (after == null) {
            if (moreResults > 0 && (searcher.count(finalQuery) <= maxNumberOfResults + moreResults)) {
                result = searcher.search(finalQuery, maxNumberOfResults + moreResults, SORT, true, false);
            } else {
                result = searcher.search(finalQuery, maxNumberOfResults, SORT, true, false);
            }
        } else {
            result = searcher.searchAfter(after, finalQuery, maxNumberOfResults, SORT, true, false);
        }

        return createSearchResult(searchQueryString, parsedQuery, searcher, result, highlightResult);
    } catch (ParseException e) {
        // re-throw parse exceptions
        throw e;
    } catch (Exception e) {
        LogService.getRoot().log(Level.WARNING, "com.rapidminer.global_search.searchhandler.search_failed", e);
        return GlobalSearchResult.createEmptyResult(searchQueryString);
    }
}

From source file:org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLuceneWithRegionDirectory() throws Exception {
    doTest("RegionDirectory", new TestCallbacks() {

        public Cache cache;
        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override//from   ww  w.  j  a va2  s .  com
        public void init() throws Exception {
            cache = new CacheFactory().set(MCAST_PORT, "0").set(LOG_LEVEL, "warning").create();
            final FileSystemStats stats = new FileSystemStats(cache.getDistributedSystem(), "stats");
            RegionDirectory dir = new RegionDirectory(new ConcurrentHashMap<String, File>(),
                    new ConcurrentHashMap<ChunkKey, byte[]>(), stats);
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
            cache.close();
            ;
        }

        @Override
        public void waitForAsync() throws Exception {
            // do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImplPerformanceTest.java

License:Apache License

@Test
public void testLucene() throws Exception {
    doTest("Lucene", new TestCallbacks() {

        private IndexWriter writer;
        private SearcherManager searcherManager;

        @Override//from   w ww.  ja v a2s  . c o m
        public void init() throws Exception {
            RAMDirectory dir = new RAMDirectory();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(dir, config);
            searcherManager = new SearcherManager(writer, true, true, null);
        }

        @Override
        public void addObject(String key, String text) throws Exception {
            Document doc = new Document();
            doc.add(new TextField("key", key, Store.YES));
            doc.add(new TextField("text", text, Store.NO));
            writer.addDocument(doc);
        }

        @Override
        public void commit() throws Exception {
            writer.commit();
            searcherManager.maybeRefresh();
        }

        @Override
        public void cleanup() throws Exception {
            writer.close();
        }

        @Override
        public void waitForAsync() throws Exception {
            // do nothing
        }

        @Override
        public int query(Query query) throws Exception {
            IndexSearcher searcher = searcherManager.acquire();
            try {
                return searcher.count(query);
            } finally {
                searcherManager.release(searcher);
            }
        }

    });

}

From source file:org.elasticsearch.common.lucene.IndexCacheableQueryTests.java

License:Apache License

public void testCache() throws IOException {
    Directory dir = newDirectory();//  ww  w.jav a 2 s  . c o m
    LRUQueryCache cache = new LRUQueryCache(10000, Long.MAX_VALUE);
    QueryCachingPolicy policy = QueryCachingPolicy.ALWAYS_CACHE;
    RandomIndexWriter writer = new RandomIndexWriter(getRandom(), dir);
    for (int i = 0; i < 10; ++i) {
        writer.addDocument(new Document());
    }

    IndexReader reader = writer.getReader();
    IndexSearcher searcher = newSearcher(reader);
    reader = searcher.getIndexReader(); // reader might be wrapped
    searcher.setQueryCache(cache);
    searcher.setQueryCachingPolicy(policy);

    assertEquals(0, cache.getCacheSize());
    DummyIndexCacheableQuery query = new DummyIndexCacheableQuery();
    searcher.count(query);
    int expectedCacheSize = reader.leaves().size();
    assertEquals(expectedCacheSize, cache.getCacheSize());
    searcher.count(query);
    assertEquals(expectedCacheSize, cache.getCacheSize());

    writer.addDocument(new Document());

    IndexReader reader2 = writer.getReader();
    searcher = newSearcher(reader2);
    reader2 = searcher.getIndexReader(); // reader might be wrapped
    searcher.setQueryCache(cache);
    searcher.setQueryCachingPolicy(policy);

    // since the query is only cacheable at the index level, it has to be recomputed on all leaves
    expectedCacheSize += reader2.leaves().size();
    searcher.count(query);
    assertEquals(expectedCacheSize, cache.getCacheSize());
    searcher.count(query);
    assertEquals(expectedCacheSize, cache.getCacheSize());

    reader.close();
    reader2.close();
    writer.close();
    assertEquals(0, cache.getCacheSize());
    dir.close();
}

From source file:org.elasticsearch.common.lucene.search.MatchNoDocsQueryTests.java

License:Apache License

public void testSearch() throws Exception {
    IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document doc = new Document();
    doc.add(new Field("field", "aaa bbb ccc ddd", TextField.TYPE_NOT_STORED));
    writer.addDocument(doc);/*from   w ww.  j  av  a 2  s.co  m*/
    IndexReader reader = DirectoryReader.open(writer);
    IndexSearcher searcher = new IndexSearcher(reader);

    Query query = new MatchNoDocsQuery("field not found");
    assertThat(searcher.count(query), equalTo(0));

    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new BooleanClause(new TermQuery(new Term("field", "aaa")), BooleanClause.Occur.SHOULD));
    bq.add(new BooleanClause(new MatchNoDocsQuery("field not found"), BooleanClause.Occur.MUST));
    query = bq.build();
    assertThat(searcher.count(query), equalTo(0));
    assertThat(query.toString(), equalTo("field:aaa +MatchNoDocsQuery[\"field not found\"]"));

    bq = new BooleanQuery.Builder();
    bq.add(new BooleanClause(new TermQuery(new Term("field", "aaa")), BooleanClause.Occur.SHOULD));
    bq.add(new BooleanClause(new MatchNoDocsQuery("field not found"), BooleanClause.Occur.SHOULD));
    query = bq.build();
    assertThat(query.toString(), equalTo("field:aaa MatchNoDocsQuery[\"field not found\"]"));
    assertThat(searcher.count(query), equalTo(1));
    Query rewrite = query.rewrite(reader);
    assertThat(rewrite.toString(), equalTo("field:aaa MatchNoDocsQuery[\"field not found\"]"));
}

From source file:org.elasticsearch.common.lucene.search.morelikethis.MoreLikeThisQueryTests.java

License:Apache License

@Test
public void testSimple() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    indexWriter.commit();/*w w w  .  j a va2s .  c o m*/

    Document document = new Document();
    document.add(new TextField("_id", "1", Field.Store.YES));
    document.add(new TextField("text", "lucene", Field.Store.YES));
    indexWriter.addDocument(document);

    document = new Document();
    document.add(new TextField("_id", "2", Field.Store.YES));
    document.add(new TextField("text", "lucene release", Field.Store.YES));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter, true);
    IndexSearcher searcher = new IndexSearcher(reader);

    MoreLikeThisQuery mltQuery = new MoreLikeThisQuery("lucene", new String[] { "text" },
            Lucene.STANDARD_ANALYZER);
    mltQuery.setLikeText("lucene");
    mltQuery.setMinTermFrequency(1);
    mltQuery.setMinDocFreq(1);
    long count = searcher.count(mltQuery);
    assertThat(count, equalTo(2l));

    reader.close();
    indexWriter.close();
}