本文整理汇总了Java中org.elasticsearch.index.query.MultiMatchQueryBuilder类的典型用法代码示例。如果您正苦于以下问题:Java MultiMatchQueryBuilder类的具体用法?Java MultiMatchQueryBuilder怎么用?Java MultiMatchQueryBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MultiMatchQueryBuilder类属于org.elasticsearch.index.query包,在下文中一共展示了MultiMatchQueryBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testForClient
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
@Test
public void testForClient() throws Exception {
/**
* @see <a href='https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-multi-match-query.html'></a>
* MultiMatchQuery依赖于match query ,也就是其核心是基于MatchQuery构建的
*
*/
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("elasticsearch match query","title", "descrption");
multiMatchQueryBuilder.analyzer("standard");
multiMatchQueryBuilder.cutoffFrequency(0.001f);
multiMatchQueryBuilder.field("title",20);
multiMatchQueryBuilder.fuzziness(Fuzziness.TWO);
multiMatchQueryBuilder.maxExpansions(100);
multiMatchQueryBuilder.prefixLength(10);
multiMatchQueryBuilder.tieBreaker(20);
multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.BEST_FIELDS);
multiMatchQueryBuilder.boost(20);
SearchResponse searchResponse = client.prepareSearch()
.setIndices("blogs")
.setTypes("blog")
.setQuery(multiMatchQueryBuilder)
.execute()
.actionGet();
}
示例2: goodSearch
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
/**
* http://blog.csdn.net/xiaohulunb/article/details/37877435
*/
public static <Goods> Pagination<Goods> goodSearch(GoodsSearchCondition condition, String sortField, boolean desc, int from, int size) {
String value = condition.getGoodsName();
if (Pinyin4jUtil.isContainChinese(condition.getGoodsName())) {
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(value,
"categoryName1", "categoryName2", "categoryName3", "goodsName", "goodsSkuAttr");
multiMatchQueryBuilder.field("categoryName1", 0.8f);
multiMatchQueryBuilder.field("categoryName2", 1f);
multiMatchQueryBuilder.field("categoryName3", 1.5f);
multiMatchQueryBuilder.field("goodsName", 2f);
multiMatchQueryBuilder.field("goodsSkuAttr", 0.8f);
Pagination<Goods> goodsPagination =
search(multiMatchQueryBuilder, IndexType.GOODS, sortField, desc, from, size);
if (!CollectionUtils.isEmpty(goodsPagination.getDataList())) {
return goodsPagination;
}
}
return boolSearch(sortField, desc, from, size, StringUtils.lowerCase(value));
}
示例3: testCrossFieldMultiMatchQuery
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public void testCrossFieldMultiMatchQuery() throws IOException {
QueryShardContext queryShardContext = indexService.newQueryShardContext(
randomInt(20), null, () -> { throw new UnsupportedOperationException(); });
queryShardContext.setAllowUnmappedFields(true);
Query parsedQuery = multiMatchQuery("banon").field("name.first", 2).field("name.last", 3).field("foobar").type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
Query rewrittenQuery = searcher.searcher().rewrite(parsedQuery);
BooleanQuery.Builder expected = new BooleanQuery.Builder();
expected.add(new TermQuery(new Term("foobar", "banon")), BooleanClause.Occur.SHOULD);
Query tq1 = new BoostQuery(new TermQuery(new Term("name.first", "banon")), 2);
Query tq2 = new BoostQuery(new TermQuery(new Term("name.last", "banon")), 3);
expected.add(new DisjunctionMaxQuery(Arrays.<Query>asList(tq1, tq2), 0f), BooleanClause.Occur.SHOULD);
assertEquals(expected.build(), rewrittenQuery);
}
}
示例4: getMatchType
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public static String getMatchType(@Nullable String matchType, DataType columnType) {
if (matchType == null) {
return defaultMatchType(columnType);
}
if (columnType.equals(DataTypes.STRING)) {
try {
MultiMatchQueryBuilder.Type.parse(matchType, ParseFieldMatcher.STRICT);
return matchType;
} catch (ElasticsearchParseException e) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"invalid MATCH type '%s' for type '%s'", matchType, columnType), e);
}
} else if (columnType.equals(DataTypes.GEO_SHAPE)) {
if (!SUPPORTED_GEO_MATCH_TYPES.contains(matchType)) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"invalid MATCH type '%s' for type '%s', valid types are: [%s]",
matchType, columnType, Joiner.on(",").join(SUPPORTED_GEO_MATCH_TYPES)));
}
return matchType;
}
throw new IllegalArgumentException("No match type for dataType: " + columnType);
}
示例5: raiseIllegalOptions
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
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)
));
}
}
示例6: MatchQueryBuilder
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public MatchQueryBuilder(MapperService mapperService,
IndexCache indexCache,
@Nullable BytesRef matchType,
@Nullable Map options) throws IOException {
this.mapperService = mapperService;
this.indexCache = indexCache;
if (matchType == null) {
this.matchType = MultiMatchQueryBuilder.Type.BEST_FIELDS;
} else {
this.matchType = SUPPORTED_TYPES.get(matchType);
if (this.matchType == null) {
throw illegalMatchType(BytesRefs.toString(matchType));
}
}
this.options = OptionParser.parse(this.matchType, options);
}
示例7: parse
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
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);
}
示例8: buildQueryBuilder
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
private QueryBuilder buildQueryBuilder(ElasticsearchSearchOption option) {
ElasticsearchSearchOption.Operator op = option.getOperator();
MultiMatchQueryBuilder query = multiMatchQuery(
option.getKeyword(),
option.getSearchFields().stream().toArray(String[]::new));
if (op != null && ElasticsearchSearchOption.Operator.AND == op) {
query = query.operator(MatchQueryBuilder.Operator.AND);
}
if (option.getOptionalSearchConditions().isEmpty()) {
return query;
} else {
BoolQueryBuilder q = boolQuery().must(query);
option.getOptionalSearchConditions().forEach((k, v) -> {
q.must(matchQuery(k, v));
});
return q;
}
}
示例9: simpleQueryBuilder
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public static QueryBuilder simpleQueryBuilder(String q) {
if (q.equals("yacyall")) return new MatchAllQueryBuilder();
final MultiMatchQueryBuilder qb = QueryBuilders
.multiMatchQuery(q)
.operator(Operator.AND)
.zeroTermsQuery(ZeroTermsQuery.ALL);
QUERY_DEFAULT_FIELDS.forEach((mapping, boost) -> qb.field(mapping.getSolrFieldName(), boost));
return qb;
}
示例10: parse
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public Query parse(MultiMatchQueryBuilder.Type type, Map<String, Float> fieldNames, Object value, String minimumShouldMatch) throws IOException {
Query result;
if (fieldNames.size() == 1) {
Map.Entry<String, Float> fieldBoost = fieldNames.entrySet().iterator().next();
Float boostValue = fieldBoost.getValue();
result = parseAndApply(type.matchQueryType(), fieldBoost.getKey(), value, minimumShouldMatch, boostValue);
} else {
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);
result = queryBuilder.combineGrouped(queries);
}
assert result != null;
return result;
}
示例11: buildGroupedQueries
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
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;
}
示例12: testMultiMatchPrefixWithAllField
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public void testMultiMatchPrefixWithAllField() throws IOException {
QueryShardContext queryShardContext = indexService.newQueryShardContext(
randomInt(20), null, () -> { throw new UnsupportedOperationException(); });
queryShardContext.setAllowUnmappedFields(true);
Query parsedQuery =
multiMatchQuery("foo").field("_all").type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX).toQuery(queryShardContext);
assertThat(parsedQuery, instanceOf(MultiPhrasePrefixQuery.class));
assertThat(parsedQuery.toString(), equalTo("_all:\"foo*\""));
}
示例13: testMultiMatchCrossFieldsWithSynonyms
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
public void testMultiMatchCrossFieldsWithSynonyms() throws IOException {
QueryShardContext queryShardContext = indexService.newQueryShardContext(
randomInt(20), null, () -> { throw new UnsupportedOperationException(); });
// check that synonym query is used for a single field
Query parsedQuery =
multiMatchQuery("quick").field("name.first")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
Term[] terms = new Term[2];
terms[0] = new Term("name.first", "quick");
terms[1] = new Term("name.first", "fast");
Query expectedQuery = new SynonymQuery(terms);
assertThat(parsedQuery, equalTo(expectedQuery));
// check that blended term query is used for multiple fields
parsedQuery =
multiMatchQuery("quick").field("name.first").field("name.last")
.type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
terms = new Term[4];
terms[0] = new Term("name.first", "quick");
terms[1] = new Term("name.first", "fast");
terms[2] = new Term("name.last", "quick");
terms[3] = new Term("name.last", "fast");
float[] boosts = new float[4];
Arrays.fill(boosts, 1.0f);
expectedQuery = BlendedTermQuery.dismaxBlendedQuery(terms, boosts, 1.0f);
assertThat(parsedQuery, equalTo(expectedQuery));
}
示例14: parse
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
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;
}
示例15: createSemQuery
import org.elasticsearch.index.query.MultiMatchQueryBuilder; //导入依赖的package包/类
/**
* Method of creating semantic query based on Threshold
*
* @param input regular query
* @param T threshold raning from 0 to 1
* @param query_operator query mode
* @return a multiMatch query builder
*/
public BoolQueryBuilder createSemQuery(String input, double T, String query_operator) {
Map<String, Double> selected_Map = getRelatedTermsByT(input, T);
selected_Map.put(input, (double) 1);
String fieldsList[] = { "Dataset-Metadata", "Dataset-ShortName", "Dataset-LongName",
"DatasetParameter-Topic", "DatasetParameter-VariableDetail", "DatasetParameter-Category",
"DatasetParameter-Variable", "DatasetParameter-Term",
"DatasetSource-Source-LongName", "DatasetSource-Source-LongName-Full",
"DatasetSource-Source-ShortName", "DatasetSource-Source-ShortName-Full",
"DatasetSource-Sensor-LongName", "DatasetSource-Sensor-LongName-Full", "DatasetSource-Sensor-ShortName",
"DatasetSource-Sensor-ShortName-Full" };
BoolQueryBuilder qb = new BoolQueryBuilder();
for (Entry<String, Double> entry : selected_Map.entrySet()) {
if (query_operator.toLowerCase().trim().equals("phrase")) {
qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).type(MultiMatchQueryBuilder.Type.PHRASE).tieBreaker((float) 0.5)); // when
// set
// to
// 1.0,
// it
// would
// be
// equal
// to
// "most
// fields"
// query
} else if (query_operator.toLowerCase().trim().equals("and")) {
qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).operator(MatchQueryBuilder.DEFAULT_OPERATOR.AND).tieBreaker((float) 0.5));
} else {
qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).operator(MatchQueryBuilder.DEFAULT_OPERATOR.OR).tieBreaker((float) 0.5));
}
}
// LOG.info(qb.toString());
return qb;
}