use of org.apache.lucene.search.DisjunctionMaxQuery in project elasticsearch by elastic.
the class QueryAnalyzerTests method testExtractQueryMetadata_disjunctionMaxQuery.
public void testExtractQueryMetadata_disjunctionMaxQuery() {
TermQuery termQuery1 = new TermQuery(new Term("_field", "_term1"));
TermQuery termQuery2 = new TermQuery(new Term("_field", "_term2"));
TermQuery termQuery3 = new TermQuery(new Term("_field", "_term3"));
TermQuery termQuery4 = new TermQuery(new Term("_field", "_term4"));
DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(termQuery1, termQuery2, termQuery3, termQuery4), 0.1f);
Result result = analyze(disjunctionMaxQuery);
assertThat(result.verified, is(true));
List<Term> terms = new ArrayList<>(result.terms);
Collections.sort(terms);
assertThat(terms.size(), equalTo(4));
assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
assertThat(terms.get(1).field(), equalTo(termQuery2.getTerm().field()));
assertThat(terms.get(1).bytes(), equalTo(termQuery2.getTerm().bytes()));
assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
assertThat(terms.get(3).field(), equalTo(termQuery4.getTerm().field()));
assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(termQuery1, termQuery2, termQuery3, new PhraseQuery("_field", "_term4")), 0.1f);
result = analyze(disjunctionMaxQuery);
assertThat(result.verified, is(false));
terms = new ArrayList<>(result.terms);
Collections.sort(terms);
assertThat(terms.size(), equalTo(4));
assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
assertThat(terms.get(1).field(), equalTo(termQuery2.getTerm().field()));
assertThat(terms.get(1).bytes(), equalTo(termQuery2.getTerm().bytes()));
assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
assertThat(terms.get(3).field(), equalTo(termQuery4.getTerm().field()));
assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project elasticsearch by elastic.
the class QueryAnalyzerTests method testExtractQueryMetadata_matchNoDocsQuery.
public void testExtractQueryMetadata_matchNoDocsQuery() {
Result result = analyze(new MatchNoDocsQuery("sometimes there is no reason at all"));
assertThat(result.verified, is(true));
assertEquals(0, result.terms.size());
BooleanQuery.Builder bq = new BooleanQuery.Builder();
bq.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.MUST);
bq.add(new MatchNoDocsQuery("sometimes there is no reason at all"), BooleanClause.Occur.MUST);
result = analyze(bq.build());
assertThat(result.verified, is(false));
assertEquals(0, result.terms.size());
bq = new BooleanQuery.Builder();
bq.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.SHOULD);
bq.add(new MatchNoDocsQuery("sometimes there is no reason at all"), BooleanClause.Occur.SHOULD);
result = analyze(bq.build());
assertThat(result.verified, is(true));
assertTermsEqual(result.terms, new Term("field", "value"));
DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("field", "value")), new MatchNoDocsQuery("sometimes there is no reason at all")), 1f);
result = analyze(disjunctionMaxQuery);
assertThat(result.verified, is(true));
assertTermsEqual(result.terms, new Term("field", "value"));
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project lucene-solr by apache.
the class SolrPluginUtilsTest method testMinShouldMatchAutoRelax.
@Test
public void testMinShouldMatchAutoRelax() {
/* The basics should not be affected by autoRelax */
BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new TermQuery(new Term("a", "b")), Occur.SHOULD);
q.add(new TermQuery(new Term("a", "c")), Occur.SHOULD);
q.add(new TermQuery(new Term("a", "d")), Occur.SHOULD);
q.add(new TermQuery(new Term("a", "d")), Occur.SHOULD);
SolrPluginUtils.setMinShouldMatch(q, "0", true);
assertEquals(0, q.build().getMinimumNumberShouldMatch());
SolrPluginUtils.setMinShouldMatch(q, "1", true);
assertEquals(1, q.build().getMinimumNumberShouldMatch());
SolrPluginUtils.setMinShouldMatch(q, "50%", true);
assertEquals(2, q.build().getMinimumNumberShouldMatch());
SolrPluginUtils.setMinShouldMatch(q, "99", true);
assertEquals(4, q.build().getMinimumNumberShouldMatch());
q.add(new TermQuery(new Term("a", "e")), Occur.MUST);
q.add(new TermQuery(new Term("a", "f")), Occur.MUST);
SolrPluginUtils.setMinShouldMatch(q, "50%", true);
assertEquals(2, q.build().getMinimumNumberShouldMatch());
/* Simulate stopwords through uneven disjuncts */
q = new BooleanQuery.Builder();
q.add(new DisjunctionMaxQuery(Collections.singleton(new TermQuery(new Term("a", "foo"))), 0.0f), Occur.SHOULD);
DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("a", "foo")), new TermQuery(new Term("b", "foo"))), 0f);
q.add(dmq, Occur.SHOULD);
dmq = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("a", "bar")), new TermQuery(new Term("b", "bar"))), 0f);
q.add(dmq, Occur.SHOULD);
// Without relax
SolrPluginUtils.setMinShouldMatch(q, "100%", false);
assertEquals(3, q.build().getMinimumNumberShouldMatch());
// With relax
SolrPluginUtils.setMinShouldMatch(q, "100%", true);
assertEquals(2, q.build().getMinimumNumberShouldMatch());
// Still same result with a MUST clause extra
q.add(new TermQuery(new Term("a", "must")), Occur.MUST);
SolrPluginUtils.setMinShouldMatch(q, "100%", true);
assertEquals(2, q.build().getMinimumNumberShouldMatch());
// Combination of dismax and non-dismax SHOULD clauses
q.add(new TermQuery(new Term("b", "should")), Occur.SHOULD);
SolrPluginUtils.setMinShouldMatch(q, "100%", true);
assertEquals(3, q.build().getMinimumNumberShouldMatch());
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project lucene-solr by apache.
the class TestClassicSimilarity method testDMQHitOrEmpty.
public void testDMQHitOrEmpty() throws IOException {
Query query = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("test", "hit")), new TermQuery(new Term("empty", "miss"))), 0);
TopDocs topDocs = indexSearcher.search(query, 1);
assertEquals(1, topDocs.totalHits);
assertEquals(1, topDocs.scoreDocs.length);
assertTrue(topDocs.scoreDocs[0].score != 0);
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project lucene-solr by apache.
the class TestClassicSimilarity method testDMQHitOrMiss.
public void testDMQHitOrMiss() throws IOException {
Query query = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("test", "hit")), new TermQuery(new Term("test", "miss"))), 0);
TopDocs topDocs = indexSearcher.search(query, 1);
assertEquals(1, topDocs.totalHits);
assertEquals(1, topDocs.scoreDocs.length);
assertTrue(topDocs.scoreDocs[0].score != 0);
}
Aggregations