Search in sources :

Example 26 with DisjunctionMaxQuery

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();
}
Also used : 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) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryParser(org.apache.lucene.queryparser.classic.QueryParser)

Example 27 with DisjunctionMaxQuery

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);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Example 28 with DisjunctionMaxQuery

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);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Example 29 with DisjunctionMaxQuery

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);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Example 30 with DisjunctionMaxQuery

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);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList)

Aggregations

DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)49 Query (org.apache.lucene.search.Query)38 BooleanQuery (org.apache.lucene.search.BooleanQuery)34 BoostQuery (org.apache.lucene.search.BoostQuery)30 TermQuery (org.apache.lucene.search.TermQuery)26 Term (org.apache.lucene.index.Term)25 ArrayList (java.util.ArrayList)22 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)16 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)15 SynonymQuery (org.apache.lucene.search.SynonymQuery)15 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)15 BooleanClause (org.apache.lucene.search.BooleanClause)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)13 SpanQuery (org.apache.lucene.search.spans.SpanQuery)12 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)10 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 TopDocs (org.apache.lucene.search.TopDocs)9