Search in sources :

Example 61 with BoostQuery

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

the class MultiMatchQuery method blendTerms.

static Query blendTerms(QueryShardContext context, BytesRef[] values, Float commonTermsCutoff, float tieBreaker, FieldAndFieldType... blendedFields) {
    List<Query> queries = new ArrayList<>();
    Term[] terms = new Term[blendedFields.length * values.length];
    float[] blendedBoost = new float[blendedFields.length * values.length];
    int i = 0;
    for (FieldAndFieldType ft : blendedFields) {
        for (BytesRef term : values) {
            Query query;
            try {
                query = new TermQuery(new Term(ft.fieldType.name(), term));
            } catch (IllegalArgumentException e) {
                // field
                continue;
            } catch (ElasticsearchParseException parseException) {
                // the case
                if (parseException.getCause() instanceof IllegalArgumentException) {
                    continue;
                }
                throw parseException;
            }
            float boost = ft.boost;
            while (query instanceof BoostQuery) {
                BoostQuery bq = (BoostQuery) query;
                query = bq.getQuery();
                boost *= bq.getBoost();
            }
            if (query.getClass() == TermQuery.class) {
                terms[i] = ((TermQuery) query).getTerm();
                blendedBoost[i] = boost;
                i++;
            } else {
                if (boost != 1f && query instanceof MatchNoDocsQuery == false) {
                    query = new BoostQuery(query, boost);
                }
                queries.add(query);
            }
        }
    }
    if (i > 0) {
        terms = Arrays.copyOf(terms, i);
        blendedBoost = Arrays.copyOf(blendedBoost, i);
        if (commonTermsCutoff != null) {
            queries.add(BlendedTermQuery.commonTermsBlendedQuery(terms, blendedBoost, commonTermsCutoff));
        } else {
            queries.add(BlendedTermQuery.dismaxBlendedQuery(terms, blendedBoost, tieBreaker));
        }
    }
    if (queries.size() == 1) {
        return queries.get(0);
    } else {
        // TODO: can we improve this?
        return new DisjunctionMaxQuery(queries, 1.0f);
    }
}
Also used : BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) 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) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) BytesRef(org.apache.lucene.util.BytesRef)

Example 62 with BoostQuery

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

the class MultiMatchQuery method blendPhrase.

/**
 * Expand a {@link PhraseQuery} to multiple fields that share the same analyzer.
 * Returns a {@link DisjunctionMaxQuery} with a disjunction for each expanded field.
 */
static Query blendPhrase(PhraseQuery query, float tiebreaker, FieldAndFieldType... fields) {
    List<Query> disjunctions = new ArrayList<>();
    for (FieldAndFieldType field : fields) {
        int[] positions = query.getPositions();
        Term[] terms = query.getTerms();
        PhraseQuery.Builder builder = new PhraseQuery.Builder();
        for (int i = 0; i < terms.length; i++) {
            builder.add(new Term(field.fieldType.name(), terms[i].bytes()), positions[i]);
        }
        Query q = builder.build();
        if (field.boost != MultiMatchQuery.DEFAULT_BOOST) {
            q = new BoostQuery(q, field.boost);
        }
        disjunctions.add(q);
    }
    return new DisjunctionMaxQuery(disjunctions, tiebreaker);
}
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) PhraseQuery(org.apache.lucene.search.PhraseQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 63 with BoostQuery

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

the class BagOfWordsQueryGenerator method buildQuery.

@Override
public Query buildQuery(String field, Analyzer analyzer, String queryText) {
    List<String> tokens = AnalyzerUtils.analyze(analyzer, queryText);
    Map<String, Long> collect = tokens.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (String t : collect.keySet()) {
        builder.add(new BoostQuery(new TermQuery(new Term(field, t)), (float) collect.get(t)), BooleanClause.Occur.SHOULD);
    }
    return builder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 64 with BoostQuery

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

the class BagOfWordsQueryGenerator method buildQuery.

public Query buildQuery(Map<String, Float> fields, Map<String, Float> queryTokenWeights) {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (Map.Entry<String, Float> entry : fields.entrySet()) {
        String field = entry.getKey();
        float boost = entry.getValue();
        Query clause = buildQuery(field, queryTokenWeights);
        builder.add(new BoostQuery(clause, boost), BooleanClause.Occur.SHOULD);
    }
    return builder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Map(java.util.Map) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 65 with BoostQuery

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

the class SdmQueryGenerator method buildQuery.

/*
  * Sequential Dependency Model
  */
@Override
public Query buildQuery(String field, Analyzer analyzer, String queryText) {
    List<String> tokens = AnalyzerUtils.analyze(analyzer, queryText);
    BooleanQuery.Builder termsBuilder = new BooleanQuery.Builder();
    if (tokens.size() == 1) {
        termsBuilder.add(new TermQuery(new Term(field, tokens.get(0))), BooleanClause.Occur.SHOULD);
        return termsBuilder.build();
    }
    BooleanQuery.Builder orderedWindowBuilder = new BooleanQuery.Builder();
    BooleanQuery.Builder unorderedWindowBuilder = new BooleanQuery.Builder();
    for (int i = 0; i < tokens.size() - 1; i++) {
        termsBuilder.add(new TermQuery(new Term(field, tokens.get(i))), BooleanClause.Occur.SHOULD);
        SpanTermQuery t1 = new SpanTermQuery(new Term(field, tokens.get(i)));
        SpanTermQuery t2 = new SpanTermQuery(new Term(field, tokens.get(i + 1)));
        SpanNearQuery orderedQ = new SpanNearQuery(new SpanQuery[] { t1, t2 }, 1, true);
        SpanNearQuery unorderedQ = new SpanNearQuery(new SpanQuery[] { t1, t2 }, 8, false);
        orderedWindowBuilder.add(orderedQ, BooleanClause.Occur.SHOULD);
        unorderedWindowBuilder.add(unorderedQ, BooleanClause.Occur.SHOULD);
    }
    termsBuilder.add(new TermQuery(new Term(field, tokens.get(tokens.size() - 1))), BooleanClause.Occur.SHOULD);
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BoostQuery(termsBuilder.build(), termWeight), BooleanClause.Occur.SHOULD);
    builder.add(new BoostQuery(orderedWindowBuilder.build(), orderWindowWeight), BooleanClause.Occur.SHOULD);
    builder.add(new BoostQuery(unorderedWindowBuilder.build(), unorderWindowWeight), BooleanClause.Occur.SHOULD);
    return builder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) 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