org.elasticsearch.index.search.MatchQuery Java Examples

The following examples show how to use org.elasticsearch.index.search.MatchQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MatchQueries.java    From crate with Apache License 2.0 6 votes vote down vote up
public static Query singleMatch(QueryShardContext queryShardContext,
                                String fieldName,
                                String queryString,
                                @Nullable String matchType,
                                @Nullable Map<String, Object> options) throws IOException {
    MultiMatchQueryType type = getType(matchType);
    ParsedOptions parsedOptions = OptionParser.parse(type, options);

    MatchQuery matchQuery = new MatchQuery(queryShardContext);

    if (parsedOptions.analyzer() != null) {
        matchQuery.setAnalyzer(parsedOptions.analyzer());
    }
    matchQuery.setCommonTermsCutoff(parsedOptions.commonTermsCutoff());
    matchQuery.setFuzziness(parsedOptions.fuzziness());
    matchQuery.setFuzzyPrefixLength(parsedOptions.prefixLength());
    matchQuery.setFuzzyRewriteMethod(parsedOptions.rewriteMethod());
    matchQuery.setMaxExpansions(parsedOptions.maxExpansions());
    matchQuery.setPhraseSlop(parsedOptions.phraseSlop());
    matchQuery.setTranspositions(parsedOptions.transpositions());
    matchQuery.setZeroTermsQuery(parsedOptions.zeroTermsQuery());
    matchQuery.setOccur(parsedOptions.operator());

    MatchQuery.Type matchQueryType = type.matchQueryType();
    return matchQuery.parse(matchQueryType, fieldName, queryString);
}
 
Example #2
Source File: ParsedOptions.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ParsedOptions(Float boost,
                     String analyzer,
                     MatchQuery.ZeroTermsQuery zeroTermsQuery,
                     int maxExpansions,
                     Fuzziness fuzziness,
                     int prefixLength,
                     boolean transpositions) {
    this.boost = boost;
    this.analyzer = analyzer;
    this.zeroTermsQuery = zeroTermsQuery;
    this.maxExpansions = maxExpansions;
    this.fuzziness = fuzziness;
    this.prefixLength = prefixLength;
    this.transpositions = transpositions;
}
 
Example #3
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected Query singleQueryAndApply(MatchQuery.Type type,
                                    String fieldName,
                                    BytesRef queryString,
                                    Float boost) {
    Query query = singleQuery(type, fieldName, queryString);
    if (query instanceof BooleanQuery) {
        Queries.applyMinimumShouldMatch((BooleanQuery) query, options.minimumShouldMatch());
    }
    if (boost != null && query != null) {
        query.setBoost(boost);
    }
    return query;
}
 
Example #4
Source File: ParsedOptions.java    From crate with Apache License 2.0 5 votes vote down vote up
public ParsedOptions(Float boost,
                     String analyzer,
                     MatchQuery.ZeroTermsQuery zeroTermsQuery,
                     int maxExpansions,
                     Fuzziness fuzziness,
                     int prefixLength,
                     boolean transpositions) {
    this.boost = boost;
    this.analyzer = analyzer;
    this.zeroTermsQuery = zeroTermsQuery;
    this.maxExpansions = maxExpansions;
    this.fuzziness = fuzziness;
    this.prefixLength = prefixLength;
    this.transpositions = transpositions;
}
 
Example #5
Source File: ParsedOptions.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public MatchQuery.ZeroTermsQuery zeroTermsQuery() {
    return zeroTermsQuery;
}
 
Example #6
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected Query singleQuery(MatchQuery.Type type, String fieldName, BytesRef queryString) {
    String field;
    MappedFieldType fieldType = mapperService.smartNameFieldType(fieldName);
    if (fieldType == null) {
        field = fieldName;
    } else {
        field = fieldType.names().indexName();
    }

    if (fieldType != null && fieldType.useTermQueryWithQueryString() && !forceAnalyzeQueryString()) {
        try {
           return fieldType.termQuery(queryString, null);
        } catch (RuntimeException e) {
            return null;
        }
    }

    Analyzer analyzer = getAnalyzer(fieldType);
    InnerQueryBuilder builder = new InnerQueryBuilder(analyzer, fieldType);

    Query query;
    switch (type) {
        case BOOLEAN:
            if (options.commonTermsCutoff() == null) {
                query = builder.createBooleanQuery(field, BytesRefs.toString(queryString), options.operator());
            } else {
                query = builder.createCommonTermsQuery(
                        field,
                        BytesRefs.toString(queryString),
                        options.operator(),
                        options.operator(),
                        options.commonTermsCutoff(),
                        fieldType
                );
            }
            break;
        case PHRASE:
            query = builder.createPhraseQuery(field, BytesRefs.toString(queryString), options.phraseSlop());
            break;
        case PHRASE_PREFIX:
            query = builder.createPhrasePrefixQuery(
                    field,
                    BytesRefs.toString(queryString),
                    options.phraseSlop(),
                    options.maxExpansions()
            );
            break;
        default:
            throw new IllegalArgumentException("invalid type: " + type.toString());
    }

    if (query == null) {
        return zeroTermsQuery();
    } else {
        return query;
    }
}
 
Example #7
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private Query zeroTermsQuery() {
    return options.zeroTermsQuery() == MatchQuery.ZeroTermsQuery.NONE ?
            Queries.newMatchNoDocsQuery() :
            Queries.newMatchAllQuery();
}
 
Example #8
Source File: MultiMatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
Type (MatchQuery.Type matchQueryType, float tieBreaker, ParseField parseField) {
    this.matchQueryType = matchQueryType;
    this.tieBreaker = tieBreaker;
    this.parseField = parseField;
}
 
Example #9
Source File: MultiMatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public MatchQuery.Type matchQueryType() {
    return matchQueryType;
}
 
Example #10
Source File: PhraseCountQueryBuilder.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public static Optional<PhraseCountQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    boolean inOrder = false;
    boolean weightedCount = false;
    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 (PhraseCountQueryBuilder.QUERY_FIELD.match(currentFieldName)) {
                        value = parser.objectText();
                    } else if (ANALYZER_FIELD.match(currentFieldName)) {
                        analyzer = parser.text();
                    } else if(IN_ORDER_FIELD.match(currentFieldName)) {
                        inOrder = parser.booleanValue();
                    } else if (WEIGHTED_COUNT_FIELD.match(currentFieldName)) {
                        weightedCount = parser.booleanValue();
                    } else if (BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (SLOP_FIELD.match(currentFieldName)) {
                        slop = parser.intValue();
                    } 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();
        }
    }

    PhraseCountQueryBuilder phraseCountQuery = new PhraseCountQueryBuilder(fieldName, value);
    phraseCountQuery.analyzer(analyzer);
    phraseCountQuery.slop(slop);
    phraseCountQuery.inOrder(inOrder);
    phraseCountQuery.weightedCount(weightedCount);
    phraseCountQuery.queryName(queryName);
    phraseCountQuery.boost(boost);
    return Optional.of(phraseCountQuery);
}
 
Example #11
Source File: ParsedOptions.java    From crate with Apache License 2.0 4 votes vote down vote up
public MatchQuery.ZeroTermsQuery zeroTermsQuery() {
    return zeroTermsQuery;
}