Search in sources :

Example 56 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 57 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 58 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project ddf by codice.

the class GeoNamesQueryLuceneIndex method createQuery.

protected Query createQuery(final String queryString) throws ParseException {
    final StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
    final QueryParser nameQueryParser = new QueryParser(GeoNamesLuceneConstants.NAME_FIELD, standardAnalyzer);
    nameQueryParser.setEnablePositionIncrements(false);
    /* For the name, we construct a query searching for exactly the query string (the phrase
        query), a query searching for all the terms in the query string (the AND query), and a
        query searching for any of the terms in the query string (the OR query). We take the
        maximum of the scores generated by these three queries and use that as the score for the
        name. */
    // Surround with quotes so Lucene looks for the words in the query as a phrase.
    // Phrase query gets the biggest boost - 3.2 was obtained after some experimentation.
    final Query phraseNameQuery = new BoostQuery(nameQueryParser.parse("\"" + queryString + "\""), 3.2f);
    // By default, QueryParser uses OR to separate terms.
    // We give OR queries the lowest boost because they're not as good as phrase matches or
    // AND matches - 1 (the default boost value) was obtained after some experimentation.
    final Query orNameQuery = nameQueryParser.parse(queryString);
    nameQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    // We give AND queries the second-biggest boost because they're better than OR matches but
    // not as good as phrase matches - 2 was obtained after some experimentation.
    final Query andNameQuery = new BoostQuery(nameQueryParser.parse(queryString), 2f);
    final List<Query> nameQueryList = Arrays.asList(phraseNameQuery, orNameQuery, andNameQuery);
    // This query will score each document by the maximum of the three sub-queries.
    final Query nameQuery = new DisjunctionMaxQuery(nameQueryList, 0);
    final QueryParser alternateNamesQueryParser = new QueryParser(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD, standardAnalyzer);
    // For the alternate names, we perform an AND query and an OR query, both of which are
    // boosted less than the name query because the alternate names are generally not as
    // important.
    // The OR query gets a lower boost - 0.5 was obtained after some experimentation.
    final Query orAlternateNamesQuery = new BoostQuery(alternateNamesQueryParser.parse(queryString), 0.5f);
    alternateNamesQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    // The AND query gets a higher boost - 1 (the default boost value) was obtained after some
    // experimentation.
    final Query andAlternateNamesQuery = alternateNamesQueryParser.parse(queryString);
    final List<Query> alternateNamesQueryList = Arrays.asList(orAlternateNamesQuery, andAlternateNamesQuery);
    // This query will score each document by the maximum of the two sub-queries.
    final Query alternateNamesQuery = new DisjunctionMaxQuery(alternateNamesQueryList, 0);
    final List<Query> queryList = Arrays.asList(nameQuery, alternateNamesQuery);
    // This query will score each document by the sum of the two sub-queries, since both the
    // name and the alternate names are important.
    // The boost values ensure that how well the query matches the name has a bigger impact on
    // the final score than how well it matches the alternate names.
    final DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(queryList, 1.0f);
    // This is the boost we calculated at index time, and it is applied in the CustomScoreQuery.
    final FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource(GeoNamesLuceneConstants.BOOST_FIELD));
    return new CustomScoreQuery(disjunctionMaxQuery, boostQuery);
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 59 with BoostQuery

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

the class BlendedTermQuery method commonTermsBlendedQuery.

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

        @Override
        protected Query topLevelQuery(Term[] terms, TermStates[] ctx, int[] docFreqs, int maxDoc) {
            BooleanQuery.Builder highBuilder = new BooleanQuery.Builder();
            BooleanQuery.Builder lowBuilder = new BooleanQuery.Builder();
            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]);
                }
                if ((maxTermFrequency >= 1f && docFreqs[i] > maxTermFrequency) || (docFreqs[i] > (int) Math.ceil(maxTermFrequency * maxDoc))) {
                    highBuilder.add(query, BooleanClause.Occur.SHOULD);
                } else {
                    lowBuilder.add(query, BooleanClause.Occur.SHOULD);
                }
            }
            BooleanQuery high = highBuilder.build();
            BooleanQuery low = lowBuilder.build();
            if (low.clauses().isEmpty()) {
                BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
                for (BooleanClause booleanClause : high) {
                    queryBuilder.add(booleanClause.getQuery(), Occur.MUST);
                }
                return queryBuilder.build();
            } else if (high.clauses().isEmpty()) {
                return low;
            } else {
                return new BooleanQuery.Builder().add(high, BooleanClause.Occur.SHOULD).add(low, BooleanClause.Occur.MUST).build();
            }
        }
    };
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 60 with BoostQuery

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

the class MultiMatchQuery method parseAndApply.

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

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)128 Query (org.apache.lucene.search.Query)107 BooleanQuery (org.apache.lucene.search.BooleanQuery)96 TermQuery (org.apache.lucene.search.TermQuery)84 Term (org.apache.lucene.index.Term)54 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)35 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)32 PrefixQuery (org.apache.lucene.search.PrefixQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)24 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)23 BooleanClause (org.apache.lucene.search.BooleanClause)20 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 ArrayList (java.util.ArrayList)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)18 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16