Search in sources :

Example 1 with ZeroTermsQuery

use of org.opensearch.index.search.MatchQuery.ZeroTermsQuery in project OpenSearch by opensearch-project.

the class MatchPhraseQueryBuilder method fromXContent.

public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    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 (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 (MatchQueryBuilder.QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        value = parser.objectText();
                    } else if (MatchQueryBuilder.ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        boost = parser.floatValue();
                    } else if (SLOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        slop = parser.intValue();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        queryName = parser.text();
                    } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        String zeroTermsValue = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_query value [" + zeroTermsValue + "]");
                        }
                    } 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();
        }
    }
    MatchPhraseQueryBuilder matchQuery = new MatchPhraseQueryBuilder(fieldName, value);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return matchQuery;
}
Also used : ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with ZeroTermsQuery

use of org.opensearch.index.search.MatchQuery.ZeroTermsQuery in project OpenSearch by opensearch-project.

the class MatchPhrasePrefixQueryBuilder method fromXContent.

public static MatchPhrasePrefixQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    String queryName = null;
    XContentParser.Token token;
    String currentFieldName = null;
    ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } 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 (MatchQueryBuilder.QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        value = parser.objectText();
                    } else if (MatchQueryBuilder.ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        boost = parser.floatValue();
                    } else if (MatchPhraseQueryBuilder.SLOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        slop = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        maxExpansion = parser.intValue();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        queryName = parser.text();
                    } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        String zeroTermsValue = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_query value [" + zeroTermsValue + "]");
                        }
                    } 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();
        }
    }
    MatchPhrasePrefixQueryBuilder matchQuery = new MatchPhrasePrefixQueryBuilder(fieldName, value);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    return matchQuery;
}
Also used : ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 3 with ZeroTermsQuery

use of org.opensearch.index.search.MatchQuery.ZeroTermsQuery in project OpenSearch by opensearch-project.

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());
    if (analyzer != null) {
        matchQuery.setAnalyzer(analyzer);
    }
    matchQuery.setFuzziness(fuzziness);
    matchQuery.setFuzzyPrefixLength(prefixLength);
    matchQuery.setMaxExpansions(maxExpansions);
    matchQuery.setTranspositions(fuzzyTranspositions);
    matchQuery.setFuzzyRewriteMethod(QueryParsers.parseRewriteMethod(fuzzyRewrite, null, LoggingDeprecationHandler.INSTANCE));
    matchQuery.setLenient(lenient);
    matchQuery.setCommonTermsCutoff(cutoffFrequency);
    matchQuery.setZeroTermsQuery(zeroTermsQuery);
    matchQuery.setAutoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery);
    Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
    return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
Also used : Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) MatchQuery(org.opensearch.index.search.MatchQuery) MatchQuery(org.opensearch.index.search.MatchQuery)

Example 4 with ZeroTermsQuery

use of org.opensearch.index.search.MatchQuery.ZeroTermsQuery in project OpenSearch by opensearch-project.

the class MatchQueryBuilder method fromXContent.

public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String minimumShouldMatch = null;
    String analyzer = null;
    Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
    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;
    boolean autoGenerateSynonymsPhraseQuery = true;
    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 (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, parser.getDeprecationHandler())) {
                        value = parser.objectText();
                    } else if (ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        boost = parser.floatValue();
                    } else if (Fuzziness.FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        fuzziness = Fuzziness.parse(parser);
                    } else if (PREFIX_LENGTH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        prefixLength = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        maxExpansion = parser.intValue();
                    } else if (OPERATOR_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        operator = Operator.fromString(parser.text());
                    } else if (MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        minimumShouldMatch = parser.textOrNull();
                    } else if (FUZZY_REWRITE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        fuzzyRewrite = parser.textOrNull();
                    } else if (FUZZY_TRANSPOSITIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        fuzzyTranspositions = parser.booleanValue();
                    } else if (LENIENT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        lenient = parser.booleanValue();
                    } else if (CUTOFF_FREQUENCY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        cutOffFrequency = parser.floatValue();
                    } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        String zeroTermsValue = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_query value [" + zeroTermsValue + "]");
                        }
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        queryName = parser.text();
                    } else if (GENERATE_SYNONYMS_PHRASE_QUERY.match(currentFieldName, parser.getDeprecationHandler())) {
                        autoGenerateSynonymsPhraseQuery = parser.booleanValue();
                    } 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.analyzer(analyzer);
    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.autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return matchQuery;
}
Also used : Fuzziness(org.opensearch.common.unit.Fuzziness) ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

ZeroTermsQuery (org.opensearch.index.search.MatchQuery.ZeroTermsQuery)4 ParsingException (org.opensearch.common.ParsingException)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 Query (org.apache.lucene.search.Query)1 Fuzziness (org.opensearch.common.unit.Fuzziness)1 MatchQuery (org.opensearch.index.search.MatchQuery)1