模糊查询
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//Elasticsearch 中文会把汉字分词,“王大”会匹配到like“王”和like“大”,要在字段后面接keyword
boolBuilder.must(QueryBuilders.wildcardQuery("userName.keyword","*王大*"));
等于、不等于
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//等于 must
boolBuilder.must(QueryBuilders.termQuery("age","30"));
//不等于 mustNot
boolBuilder.mustNot(QueryBuilders.termQuery("sex","1"));
大于、小于
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//大于
boolBuilder.must(QueryBuilders.rangeQuery("createTime").gt(1609430400000));
//小于
boolBuilder.must(QueryBuilders.rangeQuery("createTime").lt(1672502400000));
and 、or 同时使用
想实现下面这个sql
select * from user where a=1 and ((b=2 and c=3 and d !=4) or (e=4 and f=5))
java Elasticsearch 实现
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
boolBuilder.must(QueryBuilders.termQuery("a",1));
QueryBuilder queryBuilder1 = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("b", 2))
.must(QueryBuilders.termQuery("c", 3))
.mustNot(QueryBuilders.termQuery("d", 4));
QueryBuilder queryBuilder2 = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("e", 4))
.must(QueryBuilders.termQuery("f", 5));
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(queryBuilder1)
.should(queryBuilder2);
boolBuilder.must(queryBuilder);
where in条件文章来源:https://www.toymoban.com/news/detail-538787.html
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
boolBuilder.filter(QueryBuilders.termsQuery("type",[1,2,3]));
排序文章来源地址https://www.toymoban.com/news/detail-538787.html
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//where 条件
searchSourceBuilder.query(boolBuilder);
//排序 SortOrder.ASC SortOrder.DESC
searchSourceBuilder.sort("createTime", SortOrder.ASC);
到了这里,关于java Elasticsearch QueryBuilders 用法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!