QueryBuilder 是es中提供的一个查询接口, 可以对其进行参数设置来进行数据查询
1.termQuery("key",obj)完全匹配
QueryBuilders.termQuery("name","v1");
2.termsQuery("key",obj1,obj2...)一次匹配多值
QueryBuilders.termsQuery("name","v1","v2","v3");
3.matchQuery("key",obj)单个匹配,field不支持通配符,前缀具有高级特性
4.multiMatchQuery("text","field1","field2",...)匹配多个字段,field有通配符
5.matchAllQuery();匹配所有文件
mathcQuery与termQuery区别:
matchQuery:会将搜索词分词,再与目标查询字段进行匹配,若分词中的任意一个词与目标字段匹配上,则可查询到。
termQuery:不会对搜索词进行分词处理,而是作为一个整体与目标字段进行匹配,若完全匹配,则可查询到。
组合查询
6.must(QueryBuilders): AND
7.mustNot(QueryBUilders):NOT
8.should :OR
QueryBuilders
.boolQuery()
.must(QueryBuilders.termQuery("name","Hello"))
.mustNot(QueryBuilders.termQuery("isEnable",true))
.should(QueryBuilders.termQuery("age","233"));
9.只根据id查询
QueryBuilders.idsQuery(String ..type).ids(Collection<String>ids);
QueryBuilders.idsQuery().ids("CHszwWRURyK08j01p0Mmug", "ojGrYKMEQCCPvh75lHJm3A");
10.范围查询
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("user")
.from("kim")
.to("lucy")
.includeLower(true) //包含上界
.includeUpper(true); //包含下届
11.通配符查询(支持* 匹配任何字符序列,包括空 避免*开始,会检索大量内容造成效率缓慢)
QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("user","he*o");
12.分词 模糊查询 fuzzy query
QueryBuilders.fuzzyQuery("name","hello");
13.前缀匹配查询 prefix query
QueryBuilders.prefixQuery("name","hello");
14.嵌套查询
QueryBuilders.nestedQuery("policy", //path
QueryBuilders.boolQuery() // Your query
.must(QueryBuilders.termQuery("name", "hello"))
.must(QueryBuilders.rangeQuery("age").lt("23"))
,ScoreMode.None);// max,total,avg,none
public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query, ScoreMode scoreMode) {
return new NestedQueryBuilder(path, query, scoreMode);
}