Search in sources :

Example 1 with ZeroTermsQuery

use of org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery in project elasticsearch by elastic.

the class MatchQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // validate context specific fields
    if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
        throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
    }
    MatchQuery matchQuery = new MatchQuery(context);
    matchQuery.setOccur(operator.toBooleanClauseOccur());
    matchQuery.setAnalyzer(analyzer);
    matchQuery.setPhraseSlop(slop);
    matchQuery.setFuzziness(fuzziness);
    matchQuery.setFuzzyPrefixLength(prefixLength);
    matchQuery.setMaxExpansions(maxExpansions);
    matchQuery.setTranspositions(fuzzyTranspositions);
    matchQuery.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(fuzzyRewrite, null));
    matchQuery.setLenient(lenient);
    matchQuery.setCommonTermsCutoff(cutoffFrequency);
    matchQuery.setZeroTermsQuery(zeroTermsQuery);
    Query query = matchQuery.parse(type, fieldName, value);
    return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
Also used : Query(org.apache.lucene.search.Query) ZeroTermsQuery(org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery)

Example 2 with ZeroTermsQuery

use of org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery in project elasticsearch by elastic.

the class MatchQueryBuilder method fromXContent.

public static MatchQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String minimumShouldMatch = null;
    String analyzer = null;
    Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    Fuzziness fuzziness = null;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    boolean fuzzyTranspositions = FuzzyQuery.defaultTranspositions;
    String fuzzyRewrite = null;
    boolean lenient = MatchQuery.DEFAULT_LENIENCY;
    Float cutOffFrequency = null;
    ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (QUERY_FIELD.match(currentFieldName)) {
                        value = parser.objectText();
                    } else if (TYPE_FIELD.match(currentFieldName)) {
                        String tStr = parser.text();
                        if ("boolean".equals(tStr)) {
                            type = MatchQuery.Type.BOOLEAN;
                        } else if ("phrase".equals(tStr)) {
                            type = MatchQuery.Type.PHRASE;
                        } else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
                            type = MatchQuery.Type.PHRASE_PREFIX;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support type " + tStr);
                        }
                    } else if (ANALYZER_FIELD.match(currentFieldName)) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (SLOP_FIELD.match(currentFieldName)) {
                        slop = parser.intValue();
                    } else if (Fuzziness.FIELD.match(currentFieldName)) {
                        fuzziness = Fuzziness.parse(parser);
                    } else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
                        prefixLength = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName)) {
                        maxExpansion = parser.intValue();
                    } else if (OPERATOR_FIELD.match(currentFieldName)) {
                        operator = Operator.fromString(parser.text());
                    } else if (MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName)) {
                        minimumShouldMatch = parser.textOrNull();
                    } else if (FUZZY_REWRITE_FIELD.match(currentFieldName)) {
                        fuzzyRewrite = parser.textOrNull();
                    } else if (FUZZY_TRANSPOSITIONS_FIELD.match(currentFieldName)) {
                        fuzzyTranspositions = parser.booleanValue();
                    } else if (LENIENT_FIELD.match(currentFieldName)) {
                        lenient = parser.booleanValue();
                    } else if (CUTOFF_FREQUENCY_FIELD.match(currentFieldName)) {
                        cutOffFrequency = parser.floatValue();
                    } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName)) {
                        String zeroTermsDocs = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsDocs)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsDocs)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]");
                        }
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }
    if (value == null) {
        throw new ParsingException(parser.getTokenLocation(), "No text specified for text query");
    }
    MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
    matchQuery.operator(operator);
    matchQuery.type(type);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.minimumShouldMatch(minimumShouldMatch);
    if (fuzziness != null) {
        matchQuery.fuzziness(fuzziness);
    }
    matchQuery.fuzzyRewrite(fuzzyRewrite);
    matchQuery.prefixLength(prefixLength);
    matchQuery.fuzzyTranspositions(fuzzyTranspositions);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.lenient(lenient);
    if (cutOffFrequency != null) {
        matchQuery.cutoffFrequency(cutOffFrequency);
    }
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return matchQuery;
}
Also used : Fuzziness(org.elasticsearch.common.unit.Fuzziness) ZeroTermsQuery(org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.elasticsearch.common.ParsingException) MatchQuery(org.elasticsearch.index.search.MatchQuery) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

MatchQuery (org.elasticsearch.index.search.MatchQuery)2 ZeroTermsQuery (org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery)2 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 Query (org.apache.lucene.search.Query)1 ParsingException (org.elasticsearch.common.ParsingException)1 Fuzziness (org.elasticsearch.common.unit.Fuzziness)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1