Search in sources :

Example 36 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestIndexSearcher method testGetQueryCachingPolicy.

public void testGetQueryCachingPolicy() throws IOException {
    IndexSearcher searcher = new IndexSearcher(new MultiReader());
    assertEquals(IndexSearcher.getDefaultQueryCachingPolicy(), searcher.getQueryCachingPolicy());
    QueryCachingPolicy dummyPolicy = new QueryCachingPolicy() {

        @Override
        public boolean shouldCache(Query query) throws IOException {
            return false;
        }

        @Override
        public void onUse(Query query) {
        }
    };
    searcher.setQueryCachingPolicy(dummyPolicy);
    assertEquals(dummyPolicy, searcher.getQueryCachingPolicy());
    IndexSearcher.setDefaultQueryCachingPolicy(dummyPolicy);
    searcher = new IndexSearcher(new MultiReader());
    assertEquals(dummyPolicy, searcher.getQueryCachingPolicy());
}
Also used : MultiReader(org.apache.lucene.index.MultiReader)

Example 37 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class QueryUtils method wrapUnderlyingReader.

/**
   * Given an IndexSearcher, returns a new IndexSearcher whose IndexReader
   * is a MultiReader containing the Reader of the original IndexSearcher,
   * as well as several "empty" IndexReaders -- some of which will have
   * deleted documents in them.  This new IndexSearcher should
   * behave exactly the same as the original IndexSearcher.
   * @param s the searcher to wrap
   * @param edge if negative, s will be the first sub; if 0, s will be in the middle, if positive s will be the last sub
   */
public static IndexSearcher wrapUnderlyingReader(Random random, final IndexSearcher s, final int edge) throws IOException {
    IndexReader r = s.getIndexReader();
    // we can't put deleted docs before the nested reader, because
    // it will throw off the docIds
    IndexReader[] readers = new IndexReader[] { edge < 0 ? r : new MultiReader(), new MultiReader(), new MultiReader(edge < 0 ? emptyReader(4) : new MultiReader(), new MultiReader(), 0 == edge ? r : new MultiReader()), 0 < edge ? new MultiReader() : emptyReader(7), new MultiReader(), new MultiReader(0 < edge ? new MultiReader() : emptyReader(5), new MultiReader(), 0 < edge ? r : new MultiReader()) };
    IndexSearcher out = LuceneTestCase.newSearcher(new MultiReader(readers));
    out.setSimilarity(s.getSimilarity(true));
    return out;
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) IndexReader(org.apache.lucene.index.IndexReader)

Example 38 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestFieldCacheSort method testEmptyIndex.

/** test sorts when there's nothing in the index */
public void testEmptyIndex() throws Exception {
    IndexSearcher empty = newSearcher(new MultiReader());
    Query query = new TermQuery(new Term("contents", "foo"));
    Sort sort = new Sort();
    TopDocs td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
    sort.setSort(SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
    sort.setSort(new SortField("int", SortField.Type.INT), SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
    sort.setSort(new SortField("string", SortField.Type.STRING, true), SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
    sort.setSort(new SortField("string_val", SortField.Type.STRING_VAL, true), SortField.FIELD_DOC);
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
    sort.setSort(new SortField("float", SortField.Type.FLOAT), new SortField("string", SortField.Type.STRING));
    td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiReader(org.apache.lucene.index.MultiReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term)

Example 39 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestBooleanRewrites method testRemoveMatchAllFilter.

public void testRemoveMatchAllFilter() throws IOException {
    IndexSearcher searcher = newSearcher(new MultiReader());
    BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new MatchAllDocsQuery(), Occur.FILTER).build();
    assertEquals(new TermQuery(new Term("foo", "bar")), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().setMinimumNumberShouldMatch(random().nextInt(5)).add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("foo", "baz")), Occur.MUST).add(new MatchAllDocsQuery(), Occur.FILTER).build();
    BooleanQuery expected = new BooleanQuery.Builder().setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch()).add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("foo", "baz")), Occur.MUST).build();
    assertEquals(expected, searcher.rewrite(bq));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 40 with MultiReader

use of org.apache.lucene.index.MultiReader in project lucene-solr by apache.

the class TestBoostQuery method testRewrite.

public void testRewrite() throws IOException {
    IndexSearcher searcher = new IndexSearcher(new MultiReader());
    // inner queries are rewritten
    Query q = new BoostQuery(new BooleanQuery.Builder().build(), 2);
    assertEquals(new BoostQuery(new MatchNoDocsQuery(), 2), searcher.rewrite(q));
    // boosts are merged
    q = new BoostQuery(new BoostQuery(new MatchAllDocsQuery(), 3), 2);
    assertEquals(new BoostQuery(new MatchAllDocsQuery(), 6), searcher.rewrite(q));
    // scores are not computed when the boost is 0
    q = new BoostQuery(new MatchAllDocsQuery(), 0);
    assertEquals(new BoostQuery(new ConstantScoreQuery(new MatchAllDocsQuery()), 0), searcher.rewrite(q));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader)

Aggregations

MultiReader (org.apache.lucene.index.MultiReader)47 IndexReader (org.apache.lucene.index.IndexReader)20 Term (org.apache.lucene.index.Term)20 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 Document (org.apache.lucene.document.Document)6 Query (org.apache.lucene.search.Query)6 IOException (java.io.IOException)5 TermQuery (org.apache.lucene.search.TermQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 HashSet (java.util.HashSet)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 Sort (org.apache.lucene.search.Sort)3 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LuceneIndexReader (org.apache.jackrabbit.oak.plugins.index.lucene.reader.LuceneIndexReader)2 LeafReader (org.apache.lucene.index.LeafReader)2