本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
QueryBuilder 是es中提供的一个查询接口
private SearchResponse getApiResponseByDetail(SearchRequestBuilder responseBuilder,String condition) {
String time1 = "2020-01-02T00:00:00.000Z";
String time2 = "2020-01-02T15:59:59.000Z";
RangeQueryBuilder rangequerybuilder = QueryBuilders
//传入时间,目标格式2020-01-02T03:17:37.638Z
.rangeQuery("@timestamp")
.from(time1).to(time2);
SearchResponse searchResponse = responseBuilder.setQuery(
QueryBuilders.boolQuery()
//must表示and
.must(rangequerybuilder) //根据时间范围查询
.must(QueryBuilders.existsQuery("api_id"))
.must(QueryBuilders.matchPhraseQuery("detail", condition))
).setExplain(true).execute().actionGet();
return searchResponse;
复制代码
注意:es存储日志 是按照UTC时间格式存放,以@timestamp 作为时间范围查询条件,即from(Date1) to(Date2)Date1、Date2入参必须是标准的utc格式;本地时间转utc?不清楚的小伙伴可以看下这篇
java如何实现本地时间转成UTC时间格式
?
1、BoolQuery( ) 用于组合多个叶子或复合查询子句的默认查询
-
must 相当于 与 & =
-
must not 相当于 非 ~ !=
-
should 相当于 或 | or
-
filter 过滤
boolQuery().must(termQuery("content", "test1"))
.must(termQuery("content", "test4"))
.mustNot(termQuery("content", "test2"))
.should(termQuery("content", "test3"))
.filter(termQuery("content", "test5"));
复制代码
2、Elasticsearch java api 常用查询方法QueryBuilder构造举例
ps:以下来源:
www.mamicode.com/info-detail…
精确查询
以下字段名用${fieldName}代替,具体值用${fieldValue}代替
1、数字
//单个
QueryBuilder qb1 = QueryBuilders.termQuery("${fieldName}", "${fieldValue}");
QueryBuilder qb1 = QueryBuilders.termsQuery("${fieldName}", "${fieldValues}");
复制代码
2、字符串
//单个
QueryBuilder qb1 = QueryBuilders.termQuery("${fieldName}.keyword", "${fieldValue}");
QueryBuilder qb1 = QueryBuilders.termsQuery("${fieldName}.keyword", "${fieldValues}");
复制代码
模糊查询
1、数字
数字查询都为精确查询
2、字符串
QueryBuilder qb1 = QueryBuilders.moreLikeThisQuery(new String[]{"${fieldName}"}, new String[]{"${fieldValue}"}, null);
复制代码
范围查询
数字
//闭区间查询
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});
复制代码
多条件查询
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);