Java Code Examples for org.elasticsearch.index.query.MultiMatchQueryBuilder#Type

The following examples show how to use org.elasticsearch.index.query.MultiMatchQueryBuilder#Type . 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: OptionParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static void raiseIllegalOptions(MultiMatchQueryBuilder.Type matchType, Map options) {
    List<String> unknownOptions = new ArrayList<>();
    List<String> invalidOptions = new ArrayList<>();
    for (Object o : options.keySet()) {
        assert o instanceof String;
        if (!SUPPORTED_OPTIONS.contains(o)) {
            unknownOptions.add((String) o);
        } else {
            invalidOptions.add((String) o);
        }
    }
    if (!unknownOptions.isEmpty()) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "match predicate doesn't support any of the given options: %s",
                Joiner.on(", ").join(unknownOptions)));
    } else {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "match predicate option(s) \"%s\" cannot be used with matchType \"%s\"",
                Joiner.on(", ").join(invalidOptions),
                matchType.name().toLowerCase(Locale.ENGLISH)
        ));
    }
}
 
Example 2
Source File: MultiMatchQuery.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Query parse(MultiMatchQueryBuilder.Type type, Map<String, Float> fieldNames, Object value, String minimumShouldMatch) throws IOException {
    if (fieldNames.size() == 1) {
        Map.Entry<String, Float> fieldBoost = fieldNames.entrySet().iterator().next();
        Float boostValue = fieldBoost.getValue();
        return parseAndApply(type.matchQueryType(), fieldBoost.getKey(), value, minimumShouldMatch, boostValue);
    }

    final float tieBreaker = groupTieBreaker == null ? type.tieBreaker() : groupTieBreaker;
    switch (type) {
        case PHRASE:
        case PHRASE_PREFIX:
        case BEST_FIELDS:
        case MOST_FIELDS:
            queryBuilder = new QueryBuilder(tieBreaker);
            break;
        case CROSS_FIELDS:
            queryBuilder = new CrossFieldsQueryBuilder(tieBreaker);
            break;
        default:
            throw new IllegalStateException("No such type: " + type);
    }
    final List<? extends Query> queries = queryBuilder.buildGroupedQueries(type, fieldNames, value, minimumShouldMatch);
    return queryBuilder.combineGrouped(queries);
}
 
Example 3
Source File: OptionParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static ParsedOptions parse(MultiMatchQueryBuilder.Type matchType,
                                  @Nullable Map options) throws IllegalArgumentException {
    if (options == null) {
        options = Collections.emptyMap();
    } else {
        // need a copy. Otherwise manipulations on a shared option will lead to strange race conditions.
        options = new HashMap(options);
    }
    ParsedOptions parsedOptions = new ParsedOptions(
            floatValue(options, OPTIONS.BOOST, null),
            analyzer(options.remove(OPTIONS.ANALYZER)),
            zeroTermsQuery(options.remove(OPTIONS.ZERO_TERMS_QUERY)),
            intValue(options, OPTIONS.MAX_EXPANSIONS, FuzzyQuery.defaultMaxExpansions),
            fuzziness(options.remove(OPTIONS.FUZZINESS)),
            intValue(options, OPTIONS.PREFIX_LENGTH, FuzzyQuery.defaultPrefixLength),
            transpositions(options.remove(OPTIONS.FUZZY_TRANSPOSITIONS))
    );

    switch (matchType.matchQueryType()) {
        case BOOLEAN:
            parsedOptions.commonTermsCutoff(floatValue(options, OPTIONS.CUTOFF_FREQUENCY, null));
            parsedOptions.operator(operator(options.remove(OPTIONS.OPERATOR)));
            parsedOptions.minimumShouldMatch(minimumShouldMatch(options.remove(OPTIONS.MINIMUM_SHOULD_MATCH)));
            break;
        case PHRASE:
            parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
            parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
            break;
        case PHRASE_PREFIX:
            parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
            parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
            parsedOptions.rewrite(rewrite(options.remove(OPTIONS.REWRITE)));
            break;
    }
    if (!options.isEmpty()) {
        raiseIllegalOptions(matchType, options);
    }
    return parsedOptions;
}
 
Example 4
Source File: MultiMatchQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public List<Query> buildGroupedQueries(MultiMatchQueryBuilder.Type type, Map<String, Float> fieldNames, Object value, String minimumShouldMatch) throws IOException{
    List<Query> queries = new ArrayList<>();
    for (String fieldName : fieldNames.keySet()) {
        Float boostValue = fieldNames.get(fieldName);
        Query query = parseGroup(type.matchQueryType(), fieldName, boostValue, value, minimumShouldMatch);
        if (query != null) {
            queries.add(query);
        }
    }
    return queries;
}
 
Example 5
Source File: IndexSearchDaoImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Private method to build a multi match query.
 *
 * @param searchTerm the term on which to search
 * @param queryType the query type for this multi match query
 * @param queryBoost the query boost for this multi match query
 * @param fieldType the field type for this multi match query
 * @param match the set of match fields that are to be searched upon in the index search
 *
 * @return the multi match query
 */
private MultiMatchQueryBuilder buildMultiMatchQuery(final String searchTerm, final MultiMatchQueryBuilder.Type queryType, final float queryBoost,
    final String fieldType, Set<String> match)
{
    // Get the slop value for this multi match query
    Integer phraseQuerySlop = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_PHRASE_QUERY_SLOP, Integer.class);

    MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(searchTerm).type(queryType);
    multiMatchQueryBuilder.boost(queryBoost);

    if (fieldType.equals(FIELD_TYPE_STEMMED))
    {
        // Get the configured value for 'stemmed' fields and their respective boosts if any
        String stemmedFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_STEMMED);

        // build the query
        buildMultiMatchQueryWithBoosts(multiMatchQueryBuilder, stemmedFieldsValue, match);

        if (queryType.equals(PHRASE))
        {
            // Set a "slop" value to allow the matched phrase to be slightly different from an exact phrase match
            // The slop parameter tells the match phrase query how far apart terms are allowed to be while still considering the document a match
            multiMatchQueryBuilder.slop(phraseQuerySlop);
        }
    }

    if (fieldType.equals(FIELD_TYPE_NGRAMS))
    {
        // Get the configured value for 'ngrams' fields and their respective boosts if any
        String ngramsFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_NGRAMS);

        // build the query
        buildMultiMatchQueryWithBoosts(multiMatchQueryBuilder, ngramsFieldsValue, match);
    }

    if (fieldType.equals(FIELD_TYPE_SHINGLES))
    {
        // Set a "slop" value to allow the matched phrase to be slightly different from an exact phrase match
        // The slop parameter tells the match phrase query how far apart terms are allowed to be while still considering the document a match
        multiMatchQueryBuilder.slop(phraseQuerySlop);

        // Get the configured value for 'shingles' fields and their respective boosts if any
        String shinglesFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_SEARCHABLE_FIELDS_SHINGLES);

        // build the query
        buildMultiMatchQueryWithBoosts(multiMatchQueryBuilder, shinglesFieldsValue, match);
    }

    return multiMatchQueryBuilder;
}