Search in sources :

Example 16 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project elasticsearch by elastic.

the class MultiMatchQuery method parseAndApply.

private Query parseAndApply(Type type, String fieldName, Object value, String minimumShouldMatch, Float boostValue) throws IOException {
    Query query = parse(type, fieldName, value);
    query = Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
    if (query != null && boostValue != null && boostValue != AbstractQueryBuilder.DEFAULT_BOOST) {
        query = new BoostQuery(query, boostValue);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 17 with BoostQuery

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

the class TestSolrQueryParser method testCSQ.

@Test
public void testCSQ() throws Exception {
    SolrQueryRequest req = req();
    QParser qParser = QParser.getParser("text:x^=3", req);
    Query q = qParser.getQuery();
    assertTrue(q instanceof BoostQuery);
    assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
    assertEquals(3.0, ((BoostQuery) q).getBoost(), 0.0f);
    qParser = QParser.getParser("(text:x text:y)^=-3", req);
    q = qParser.getQuery();
    assertTrue(q instanceof BoostQuery);
    assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
    assertEquals(-3.0, ((BoostQuery) q).getBoost(), 0.0f);
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Query(org.apache.lucene.search.Query) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Test(org.junit.Test)

Example 18 with BoostQuery

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

the class TestExtendedDismaxParser method containsClause.

private boolean containsClause(Query query, String field, String value, int boost, boolean fuzzy) {
    float queryBoost = 1f;
    if (query instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) query;
        query = bq.getQuery();
        queryBoost = bq.getBoost();
    }
    if (query instanceof BooleanQuery) {
        return containsClause((BooleanQuery) query, field, value, boost, fuzzy);
    }
    if (query instanceof DisjunctionMaxQuery) {
        return containsClause((DisjunctionMaxQuery) query, field, value, boost, fuzzy);
    }
    if (boost != queryBoost) {
        return false;
    }
    if (query instanceof TermQuery && !fuzzy) {
        return containsClause((TermQuery) query, field, value);
    }
    if (query instanceof FuzzyQuery && fuzzy) {
        return containsClause((FuzzyQuery) query, field, value);
    }
    return false;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 19 with BoostQuery

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

the class CommonTermsQuery method buildQuery.

protected Query buildQuery(final int maxDoc, final TermContext[] contextArray, final Term[] queryTerms) {
    List<Query> lowFreqQueries = new ArrayList<>();
    List<Query> highFreqQueries = new ArrayList<>();
    for (int i = 0; i < queryTerms.length; i++) {
        TermContext termContext = contextArray[i];
        if (termContext == null) {
            lowFreqQueries.add(newTermQuery(queryTerms[i], null));
        } else {
            if ((maxTermFrequency >= 1f && termContext.docFreq() > maxTermFrequency) || (termContext.docFreq() > (int) Math.ceil(maxTermFrequency * (float) maxDoc))) {
                highFreqQueries.add(newTermQuery(queryTerms[i], termContext));
            } else {
                lowFreqQueries.add(newTermQuery(queryTerms[i], termContext));
            }
        }
    }
    final int numLowFreqClauses = lowFreqQueries.size();
    final int numHighFreqClauses = highFreqQueries.size();
    Occur lowFreqOccur = this.lowFreqOccur;
    Occur highFreqOccur = this.highFreqOccur;
    int lowFreqMinShouldMatch = 0;
    int highFreqMinShouldMatch = 0;
    if (lowFreqOccur == Occur.SHOULD && numLowFreqClauses > 0) {
        lowFreqMinShouldMatch = calcLowFreqMinimumNumberShouldMatch(numLowFreqClauses);
    }
    if (highFreqOccur == Occur.SHOULD && numHighFreqClauses > 0) {
        highFreqMinShouldMatch = calcHighFreqMinimumNumberShouldMatch(numHighFreqClauses);
    }
    if (lowFreqQueries.isEmpty()) {
        /*
       * if lowFreq is empty we rewrite the high freq terms in a conjunction to
       * prevent slow queries.
       */
        if (highFreqMinShouldMatch == 0 && highFreqOccur != Occur.MUST) {
            highFreqOccur = Occur.MUST;
        }
    }
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    if (lowFreqQueries.isEmpty() == false) {
        BooleanQuery.Builder lowFreq = new BooleanQuery.Builder();
        for (Query query : lowFreqQueries) {
            lowFreq.add(query, lowFreqOccur);
        }
        lowFreq.setMinimumNumberShouldMatch(lowFreqMinShouldMatch);
        Query lowFreqQuery = lowFreq.build();
        builder.add(new BoostQuery(lowFreqQuery, lowFreqBoost), Occur.MUST);
    }
    if (highFreqQueries.isEmpty() == false) {
        BooleanQuery.Builder highFreq = new BooleanQuery.Builder();
        for (Query query : highFreqQueries) {
            highFreq.add(query, highFreqOccur);
        }
        highFreq.setMinimumNumberShouldMatch(highFreqMinShouldMatch);
        Query highFreqQuery = highFreq.build();
        builder.add(new BoostQuery(highFreqQuery, highFreqBoost), Occur.SHOULD);
    }
    return builder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ArrayList(java.util.ArrayList) Occur(org.apache.lucene.search.BooleanClause.Occur) BoostQuery(org.apache.lucene.search.BoostQuery) TermContext(org.apache.lucene.index.TermContext)

Example 20 with BoostQuery

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

the class SimpleQueryParser method newPhraseQuery.

/**
   * Factory method to generate a phrase query with slop.
   */
protected Query newPhraseQuery(String text, int slop) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    for (Map.Entry<String, Float> entry : weights.entrySet()) {
        Query q = createPhraseQuery(entry.getKey(), text, slop);
        if (q != null) {
            float boost = entry.getValue();
            if (boost != 1f) {
                q = new BoostQuery(q, boost);
            }
            bq.add(q, BooleanClause.Occur.SHOULD);
        }
    }
    return simplify(bq.build());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Map(java.util.Map) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)109 Query (org.apache.lucene.search.Query)91 BooleanQuery (org.apache.lucene.search.BooleanQuery)80 TermQuery (org.apache.lucene.search.TermQuery)71 Term (org.apache.lucene.index.Term)48 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)43 PhraseQuery (org.apache.lucene.search.PhraseQuery)32 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 PrefixQuery (org.apache.lucene.search.PrefixQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)25 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)22 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)21 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 BooleanClause (org.apache.lucene.search.BooleanClause)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 ArrayList (java.util.ArrayList)14