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"))));
}
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);
}
}
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));
}
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));
}
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);
}
Aggregations