suggest.should(QueryBuilders.fuzzyQuery(fieldName, q).fuzziness(Fuzziness.fromEdits(2))); suggest.should(QueryBuilders.moreLikeThisQuery(fieldName).like(q)); suggest.should(QueryBuilders.matchPhrasePrefixQuery(fieldName, q)); if (q.indexOf('*') >= 0 || q.indexOf('?') >= 0) suggest.should(QueryBuilders.wildcardQuery(fieldName, q)); suggest.minimumNumberShouldMatch(1);
private static QueryBuilder makeLikeQuery(String op, String name, String value, EsHint hint) { String fieldName = name; if (op.contains("ILIKE")) { // ILIKE will work only with a correct mapping value = value.toLowerCase(); fieldName = name + ".lowercase"; } if (hint != null && hint.index != null) { fieldName = hint.index; } // convert the value to a wildcard query String wildcard = likeToWildcard(value); // use match phrase prefix when possible if (StringUtils.countMatches(wildcard, "*") == 1 && wildcard.endsWith("*") && !wildcard.contains("?") && !wildcard.contains("\\")) { MatchPhrasePrefixQueryBuilder query = QueryBuilders.matchPhrasePrefixQuery(fieldName, wildcard.replace("*", "")); if (hint != null && hint.analyzer != null) { query.analyzer(hint.analyzer); } return query; } return QueryBuilders.wildcardQuery(fieldName, wildcard); }
return combine(values, BoolQueryBuilder::should, v -> "*".equals(v) ? QueryBuilders.matchAllQuery() : QueryBuilders.matchPhrasePrefixQuery(key, v)); : QueryBuilders.matchPhrasePrefixQuery(key, v));
MatchPhrasePrefixQueryBuilder matchPhrasePrefixQuery = QueryBuilders.matchPhrasePrefixQuery(name, value); if (hint.analyzer != null) { matchPhrasePrefixQuery.analyzer(hint.analyzer);