use of org.apache.lucene.search.DisjunctionMaxQuery in project Anserini by castorini.
the class Covid19QueryGenerator method buildQuery.
@Override
public Query buildQuery(String field, Analyzer analyzer, String queryText) {
// Remove boilerplate
queryText = removeBoilerplate(queryText);
// If query doesn't contain variants of COVID-19, then just pass through with BoW generator.
if (!isCovidQuery(queryText)) {
return bowQueryGenerator.buildQuery(field, analyzer, queryText);
}
// Remove the variant of covid-19 itself.
queryText = queryText.replaceAll("(?i)" + COVID_NAMES, " ");
List<String> tokens = AnalyzerUtils.analyze(analyzer, queryText);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (String t : tokens) {
builder.add(new TermQuery(new Term(field, t)), BooleanClause.Occur.SHOULD);
}
QueryParser parser = new QueryParser(IndexArgs.CONTENTS, analyzer);
try {
List<Query> disjuncts = new ArrayList<>();
disjuncts.add(parser.parse("\"COVID-19\""));
disjuncts.add(parser.parse("\"2019-nCov\""));
disjuncts.add(parser.parse("\"SARS-CoV-2\""));
builder.add(new DisjunctionMaxQuery(disjuncts, 0.0f), BooleanClause.Occur.SHOULD);
} catch (Exception ParseException) {
// Do nothing.
}
return builder.build();
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project elasticsearch by elastic.
the class MapperQueryParser method getFuzzyQuery.
protected Query getFuzzyQuery(String field, String termStr, String minSimilarity) throws ParseException {
Collection<String> fields = extractMultiFields(field);
if (fields != null) {
if (fields.size() == 1) {
return getFuzzyQuerySingle(fields.iterator().next(), termStr, minSimilarity);
}
if (settings.useDisMax()) {
List<Query> queries = new ArrayList<>();
boolean added = false;
for (String mField : fields) {
Query q = getFuzzyQuerySingle(mField, termStr, minSimilarity);
if (q != null) {
added = true;
queries.add(applyBoost(mField, q));
}
}
if (!added) {
return null;
}
return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else {
List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) {
Query q = getFuzzyQuerySingle(mField, termStr, minSimilarity);
if (q != null) {
clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
}
}
return getBooleanQueryCoordDisabled(clauses);
}
} else {
return getFuzzyQuerySingle(field, termStr, minSimilarity);
}
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project elasticsearch by elastic.
the class MapperQueryParser method getWildcardQuery.
@Override
protected Query getWildcardQuery(String field, String termStr) throws ParseException {
if (termStr.equals("*") && field != null) {
if ("*".equals(field)) {
return newMatchAllDocsQuery();
}
String actualField = field;
if (actualField == null) {
actualField = this.field;
}
// effectively, we check if a field exists or not
return FIELD_QUERY_EXTENSIONS.get(ExistsFieldQueryExtension.NAME).query(context, actualField);
}
Collection<String> fields = extractMultiFields(field);
if (fields != null) {
if (fields.size() == 1) {
return getWildcardQuerySingle(fields.iterator().next(), termStr);
}
if (settings.useDisMax()) {
List<Query> queries = new ArrayList<>();
boolean added = false;
for (String mField : fields) {
Query q = getWildcardQuerySingle(mField, termStr);
if (q != null) {
added = true;
queries.add(applyBoost(mField, q));
}
}
if (!added) {
return null;
}
return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else {
List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) {
Query q = getWildcardQuerySingle(mField, termStr);
if (q != null) {
clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
}
}
// happens for stopwords
if (clauses.isEmpty())
return null;
return getBooleanQueryCoordDisabled(clauses);
}
} else {
return getWildcardQuerySingle(field, termStr);
}
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project elasticsearch by elastic.
the class MapperQueryParser method getPrefixQuery.
@Override
protected Query getPrefixQuery(String field, String termStr) throws ParseException {
Collection<String> fields = extractMultiFields(field);
if (fields != null) {
if (fields.size() == 1) {
return getPrefixQuerySingle(fields.iterator().next(), termStr);
}
if (settings.useDisMax()) {
List<Query> queries = new ArrayList<>();
boolean added = false;
for (String mField : fields) {
Query q = getPrefixQuerySingle(mField, termStr);
if (q != null) {
added = true;
queries.add(applyBoost(mField, q));
}
}
if (!added) {
return null;
}
return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else {
List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) {
Query q = getPrefixQuerySingle(mField, termStr);
if (q != null) {
clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
}
}
// happens for stopwords
if (clauses.isEmpty())
return null;
return getBooleanQueryCoordDisabled(clauses);
}
} else {
return getPrefixQuerySingle(field, termStr);
}
}
use of org.apache.lucene.search.DisjunctionMaxQuery in project elasticsearch by elastic.
the class MapperQueryParser method getRangeQuery.
@Override
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException {
if ("*".equals(part1)) {
part1 = null;
}
if ("*".equals(part2)) {
part2 = null;
}
Collection<String> fields = extractMultiFields(field);
if (fields == null) {
return getRangeQuerySingle(field, part1, part2, startInclusive, endInclusive, context);
}
if (fields.size() == 1) {
return getRangeQuerySingle(fields.iterator().next(), part1, part2, startInclusive, endInclusive, context);
}
if (settings.useDisMax()) {
List<Query> queries = new ArrayList<>();
boolean added = false;
for (String mField : fields) {
Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive, context);
if (q != null) {
added = true;
queries.add(applyBoost(mField, q));
}
}
if (!added) {
return null;
}
return new DisjunctionMaxQuery(queries, settings.tieBreaker());
} else {
List<BooleanClause> clauses = new ArrayList<>();
for (String mField : fields) {
Query q = getRangeQuerySingle(mField, part1, part2, startInclusive, endInclusive, context);
if (q != null) {
clauses.add(new BooleanClause(applyBoost(mField, q), BooleanClause.Occur.SHOULD));
}
}
// happens for stopwords
if (clauses.isEmpty())
return null;
return getBooleanQueryCoordDisabled(clauses);
}
}
Aggregations