当前位置:   article > 正文

Es QueryBuilder 简单查询_querybuilders.multimatchquery

querybuilders.multimatchquery

1.matchQuery(String name,Object  text)

matchQuery("filedname","value")匹配单个字段,匹配字段名为filedname,值为value的文档

//单个匹配,搜索name为jack的文档

QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "jack");

2.multiMatchQuery(Object text, String... fieldNames)

多个字段匹配某一个值

//搜索name中或interest中包含有music的文档(必须与music一致)

QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music","name", "interest");

3.wildcardQuery()模糊查询

模糊查询,?匹配单个字符,*匹配多个字符

//搜索名字中含有jack文档(name中只要包含jack即可)

WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name","*jack*");

4.BoolQueryBuilder进行复合查询

4.1使用must

//模糊查询并搜索名字中含有jack的文档
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery( "name", "*jack*");

//模糊查询并搜索interest中含有read的文档
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery( "interest", "*read*");
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//name中必须含有jack,interest中必须含有read,相当于and
boolQueryBuilder.must(queryBuilder1);
boolQueryBuilder.must(queryBuilder2);

输出结果:name中必须含有jack,interest中必须含有read

4.2 使用 should

WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery("name", "*jack*");//搜索名字中含有jack的文档
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery("interest", "*read*");//搜索interest中含有read的文档
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//name中含有jack或者interest含有read,相当于or
boolQueryBuilder.should(queryBuilder1);
boolQueryBuilder.should(queryBuilder2);

输出结果:name中含有jack或者interest中含有read的都将被匹配
 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/698087
推荐阅读
相关标签
  

闽ICP备14008679号