美文网首页
ElasticSearch常用操作之QueryBuilders范

ElasticSearch常用操作之QueryBuilders范

作者: 在人间凑数的日子丶 | 来源:发表于2022-09-19 16:35 被阅读0次

1.BoolQuery( ) 用于组合多个叶子或复合查询子句的默认查询

must 相当于 与 & =

must not 相当于 非 ~ !=

should 相当于 或 | or

filter 过滤

boolQuery().must(termQuery("content", "query1"))

          .must(termQuery("content", "query2"))

          .mustNot(termQuery("content", "query3"))

          .should(termQuery("content", "query4"))

          .filter(termQuery("content", "query5"));

2.精确查询

以下字段名用 ${fieldName} 代替,具体值用 ${fieldValue} 代替

数字查询

//单个

QueryBuilder qb1 = QueryBuilders.termQuery("${fieldName}", "${fieldValue}");

//批量

QueryBuilder qb1 = QueryBuilders.termsQuery("${fieldName}", "${fieldValues}");

字符串查询

//单个

QueryBuilder qb1 = QueryBuilders.termQuery("${fieldName}.keyword", "${fieldValue}");

//批量

QueryBuilder qb1 = QueryBuilders.termsQuery("${fieldName}.keyword", "${fieldValues}");

3.模糊查询

数字

数字查询只能是精确查询

字符串

QueryBuilder qb1 = QueryBuilders.moreLikeThisQuery(

                                                    new String[]{"${fieldName}"},

                                                    new String[]{"${fieldValue}"}, null);

4.范围查询

数字

//闭区间查询

QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}).to(${fieldValue2});

//开区间查询

QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}, false).to(${fieldValue2}, false);

//大于

QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").gt(${fieldValue});

//大于等于

QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").gte(${fieldValue});

//小于

QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").lt(${fieldValue});

//小于等于

QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").lte(${fieldValue});

5.多条件查询

QueryBuilder qb1 = QueryBuilders.moreLikeThisQuery(new String[]{"${fieldName1}"}, new String[]{"${fieldValue1}"}, null);

QueryBuilder qb2 = QueryBuilders.rangeQuery("${fieldName2}").gt("${fieldValue2}");

QueryBuilder qb3 = QueryBuilders.boolQuery().must(qb1).must(qb2);

相关文章

网友评论

      本文标题:ElasticSearch常用操作之QueryBuilders范

      本文链接:https://www.haomeiwen.com/subject/iprvortx.html