Search in sources :

Example 46 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery in project lucene-solr by apache.

the class TestCoreParser method testDisjunctionMaxQueryXML.

public void testDisjunctionMaxQueryXML() throws ParserException, IOException {
    Query q = parse("DisjunctionMaxQuery.xml");
    assertTrue(q instanceof DisjunctionMaxQuery);
    DisjunctionMaxQuery d = (DisjunctionMaxQuery) q;
    assertEquals(0.0f, d.getTieBreakerMultiplier(), 0.0001f);
    assertEquals(2, d.getDisjuncts().size());
    DisjunctionMaxQuery ndq = (DisjunctionMaxQuery) d.getDisjuncts().get(1);
    assertEquals(1.2f, ndq.getTieBreakerMultiplier(), 0.0001f);
    assertEquals(1, ndq.getDisjuncts().size());
}
Also used : Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery)

Example 47 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery in project Anserini by castorini.

the class DisjunctionMaxQueryGeneratorTest method testMultipleFields.

@Test
public void testMultipleFields() {
    float tiebreaker = 0.5f;
    QueryGenerator queryGenerator = new DisjunctionMaxQueryGenerator(tiebreaker);
    Map<String, Float> fields = Map.of("field1", 3.14f, "field2", 2.718f, "field3", 42.0f);
    Query query = queryGenerator.buildQuery(fields, IndexCollection.DEFAULT_ANALYZER, "sample query");
    assertTrue(query instanceof DisjunctionMaxQuery);
    DisjunctionMaxQuery combinedQuery = (DisjunctionMaxQuery) query;
    assertEquals(tiebreaker, combinedQuery.getTieBreakerMultiplier(), 1e-6);
    assertEquals(3, combinedQuery.getDisjuncts().size());
    assertTrue(combinedQuery.getDisjuncts().get(0) instanceof BoostQuery);
    BoostQuery boostQuery = (BoostQuery) combinedQuery.getDisjuncts().get(0);
    assertTrue(boostQuery.getBoost() > 1.0f);
    assertTrue(boostQuery.getQuery() instanceof BooleanQuery);
    BooleanQuery booleanQuery = (BooleanQuery) boostQuery.getQuery();
    assertEquals(2, booleanQuery.clauses().size());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Test(org.junit.Test)

Example 48 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery in project Anserini by castorini.

the class DisjunctionMaxQueryGenerator method buildQuery.

@Override
public Query buildQuery(Map<String, Float> fields, Analyzer analyzer, String queryText) {
    List<Query> clauses = new ArrayList<>(fields.size());
    for (Map.Entry<String, Float> entry : fields.entrySet()) {
        String field = entry.getKey();
        float boost = entry.getValue();
        Query clause = buildQuery(field, analyzer, queryText);
        clauses.add(new BoostQuery(clause, boost));
    }
    return new DisjunctionMaxQuery(clauses, tiebreaker);
}
Also used : Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) Map(java.util.Map) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 49 with DisjunctionMaxQuery

use of org.apache.lucene.search.DisjunctionMaxQuery in project crate by crate.

the class BlendedTermQuery method dismaxBlendedQuery.

public static BlendedTermQuery dismaxBlendedQuery(Term[] terms, final float[] boosts, final float tieBreakerMultiplier) {
    return new BlendedTermQuery(terms, boosts) {

        @Override
        protected Query topLevelQuery(Term[] terms, TermStates[] ctx, int[] docFreqs, int maxDoc) {
            List<Query> queries = new ArrayList<>(ctx.length);
            for (int i = 0; i < terms.length; i++) {
                Query query = new TermQuery(terms[i], ctx[i]);
                if (boosts != null && boosts[i] != 1f) {
                    query = new BoostQuery(query, boosts[i]);
                }
                queries.add(query);
            }
            return new DisjunctionMaxQuery(queries, tieBreakerMultiplier);
        }
    };
}
Also used : 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) BoostQuery(org.apache.lucene.search.BoostQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) BoostQuery(org.apache.lucene.search.BoostQuery)

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