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);
}
}
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);
}
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();
}
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();
}
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();
}
Aggregations