Search in sources :

Example 36 with DisjunctionMaxQuery

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()));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 37 with DisjunctionMaxQuery

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"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 38 with DisjunctionMaxQuery

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());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Example 39 with DisjunctionMaxQuery

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);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term)

Example 40 with DisjunctionMaxQuery

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);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term)

Aggregations

DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)49 Query (org.apache.lucene.search.Query)38 BooleanQuery (org.apache.lucene.search.BooleanQuery)34 BoostQuery (org.apache.lucene.search.BoostQuery)30 TermQuery (org.apache.lucene.search.TermQuery)26 Term (org.apache.lucene.index.Term)25 ArrayList (java.util.ArrayList)22 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)16 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)15 SynonymQuery (org.apache.lucene.search.SynonymQuery)15 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)15 BooleanClause (org.apache.lucene.search.BooleanClause)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)13 SpanQuery (org.apache.lucene.search.spans.SpanQuery)12 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)10 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 TopDocs (org.apache.lucene.search.TopDocs)9