Search in sources :

Example 11 with MultiReader

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

the class TestTermQuery method testEquals.

public void testEquals() throws IOException {
    QueryUtils.checkEqual(new TermQuery(new Term("foo", "bar")), new TermQuery(new Term("foo", "bar")));
    QueryUtils.checkUnequal(new TermQuery(new Term("foo", "bar")), new TermQuery(new Term("foo", "baz")));
    QueryUtils.checkEqual(new TermQuery(new Term("foo", "bar")), new TermQuery(new Term("foo", "bar"), TermContext.build(new MultiReader().getContext(), new Term("foo", "bar"))));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 12 with MultiReader

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

the class TestSortedSetSortField method testEmptyIndex.

public void testEmptyIndex() throws Exception {
    IndexSearcher empty = newSearcher(new MultiReader());
    Query query = new TermQuery(new Term("contents", "foo"));
    Sort sort = new Sort();
    sort.setSort(new SortedSetSortField("sortedset", false));
    TopDocs td = empty.search(query, 10, sort, true, true);
    assertEquals(0, td.totalHits);
    // for an empty index, any selector should work
    for (SortedSetSelector.Type v : SortedSetSelector.Type.values()) {
        sort.setSort(new SortedSetSortField("sortedset", false, v));
        td = empty.search(query, 10, sort, true, true);
        assertEquals(0, td.totalHits);
    }
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 13 with MultiReader

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

the class TestBooleanRewrites method testDuplicateMustOrFilterWithMustNot.

// Duplicate Must or Filter with MustNot returns no match
public void testDuplicateMustOrFilterWithMustNot() throws IOException {
    IndexSearcher searcher = newSearcher(new MultiReader());
    // Test Must with MustNot
    BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("foo", "baz")), Occur.MUST).add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD).add(new TermQuery(new Term("foo", "bar")), Occur.MUST_NOT).build();
    assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq));
    // Test Filter with MustNot
    BooleanQuery bq2 = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).add(new TermQuery(new Term("foo", "baz")), Occur.MUST).add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD).add(new TermQuery(new Term("foo", "bar")), Occur.MUST_NOT).build();
    assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq2));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 14 with MultiReader

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

the class TestBooleanRewrites method testSingleMustMatchAll.

public void testSingleMustMatchAll() throws IOException {
    IndexSearcher searcher = newSearcher(new MultiReader());
    BooleanQuery bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).build();
    assertEquals(new ConstantScoreQuery(new TermQuery(new Term("foo", "bar"))), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new BoostQuery(new MatchAllDocsQuery(), 42), Occur.MUST).add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).build();
    assertEquals(new BoostQuery(new ConstantScoreQuery(new TermQuery(new Term("foo", "bar"))), 42), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new MatchAllDocsQuery(), Occur.FILTER).build();
    assertEquals(new MatchAllDocsQuery(), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new BoostQuery(new MatchAllDocsQuery(), 42), Occur.MUST).add(new MatchAllDocsQuery(), Occur.FILTER).build();
    assertEquals(new BoostQuery(new MatchAllDocsQuery(), 42), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("foo", "bar")), Occur.MUST_NOT).build();
    assertEquals(bq, searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new MatchAllDocsQuery(), Occur.FILTER).build();
    assertEquals(new MatchAllDocsQuery(), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).add(new TermQuery(new Term("foo", "baz")), Occur.FILTER).build();
    Query expected = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).add(new TermQuery(new Term("foo", "baz")), Occur.FILTER).build();
    assertEquals(new ConstantScoreQuery(expected), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).add(new TermQuery(new Term("foo", "baz")), Occur.MUST_NOT).build();
    expected = new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.FILTER).add(new TermQuery(new Term("foo", "baz")), Occur.MUST_NOT).build();
    assertEquals(new ConstantScoreQuery(expected), searcher.rewrite(bq));
    bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("foo", "bar")), Occur.SHOULD).build();
    assertEquals(bq, searcher.rewrite(bq));
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term)

Example 15 with MultiReader

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

the class TestBooleanQuery method testExtractTerms.

public void testExtractTerms() throws IOException {
    Term a = new Term("f", "a");
    Term b = new Term("f", "b");
    Term c = new Term("f", "c");
    Term d = new Term("f", "d");
    BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder();
    bqBuilder.add(new TermQuery(a), Occur.SHOULD);
    bqBuilder.add(new TermQuery(b), Occur.MUST);
    bqBuilder.add(new TermQuery(c), Occur.FILTER);
    bqBuilder.add(new TermQuery(d), Occur.MUST_NOT);
    IndexSearcher searcher = new IndexSearcher(new MultiReader());
    BooleanQuery bq = bqBuilder.build();
    Set<Term> scoringTerms = new HashSet<>();
    searcher.createNormalizedWeight(bq, true).extractTerms(scoringTerms);
    assertEquals(new HashSet<>(Arrays.asList(a, b)), scoringTerms);
    Set<Term> matchingTerms = new HashSet<>();
    searcher.createNormalizedWeight(bq, false).extractTerms(matchingTerms);
    assertEquals(new HashSet<>(Arrays.asList(a, b, c)), matchingTerms);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiReader(org.apache.lucene.index.MultiReader) Term(org.apache.lucene.index.Term) HashSet(java.util.HashSet)

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