elasticsearch工具
舟率率 11/2/2022 elasticsearch
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.0</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
# es dsl语法与java实现案例
# 案例一
{"query":{"bool":{"must":[{"range":{"createAt":{"from":"2022-01-01 00:00:00","to":"2022-01-02 00:00:00","include_lower":true,"include_upper":false,"format":"yyyy-MM-dd HH:mm:ss","boost":1.0}}},{"terms":{"object_col1.object_col2.kw_col3":["v1","v2"],"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"_source":{"includes":["id","object_col4.object_col5"],"excludes":[]},"track_total_hits":2147483647}
1
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("createAt").gte("2022-01-01 00:00:00").lt("2022-01-02 00:00:00").format("yyyy-MM-dd HH:mm:ss"));
boolQuery.must(QueryBuilders.termsQuery("object_col1.object_col2.kw_col3",Lists.newArrayList("v1","v2")));
String[]includeFields=new String[]{"id","object_col4.object_col5"};
searchSourceBuilder.fetchSource(includeFields,new String[]{});
searchSourceBuilder.query(boolQuery);
// 如果查询结果超过1w条数据 需要加上 "track_total_hits":true ,不然只能显示出9999条
searchSourceBuilder.trackTotalHits(true);
System.out.println(searchSourceBuilder);
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 案例二
{"size":0,"query":{"bool":{"must":[{"term":{"kw_following":{"value":1111111111,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"track_total_hits":2147483647,"aggregations":{"user_quality":{"filters":{"filters":{"bots_zombies":{"bool":{"must":[{"term":{"bool_verified":{"value":false,"boost":1.0}}},{"range":{"long_followersCount":{"from":null,"to":10,"include_lower":true,"include_upper":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"high_quality_audience":{"bool":{"should":[{"bool":{"must":[{"range":{"long_followersCount":{"from":1001,"to":null,"include_lower":true,"include_upper":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},{"term":{"bool_verified":{"value":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"regular_audience":{"bool":{"must":[{"term":{"bool_verified":{"value":false,"boost":1.0}}},{"range":{"long_followersCount":{"from":11,"to":1000,"include_lower":true,"include_upper":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}},"other_bucket":false,"other_bucket_key":"_other_"}}}}
1
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
// 构建查询语句
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("kw_following",1111111111));
searchSourceBuilder.size(0);
searchSourceBuilder.query(boolQuery);
// 如果查询结果超过1w条数据 需要加上 "track_total_hits":true ,不然只能显示出9999条
searchSourceBuilder.trackTotalHits(true);
BoolQueryBuilder hqaBuilder=QueryBuilders.boolQuery();
hqaBuilder.should(QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("long_followersCount").from(1001L)));
hqaBuilder.should(QueryBuilders.termQuery("bool_verified",true));
FiltersAggregator.KeyedFilter hqaFilter=new FiltersAggregator.KeyedFilter("high_quality_audience",hqaBuilder);
BoolQueryBuilder raBuilder=QueryBuilders.boolQuery();
List<QueryBuilder> raMust=raBuilder.must();
raMust.add(QueryBuilders.termQuery("bool_verified",false));
raMust.add(QueryBuilders.rangeQuery("long_followersCount").from(11L).to(1000L));
FiltersAggregator.KeyedFilter raFilter=new FiltersAggregator.KeyedFilter("regular_audience",raBuilder);
BoolQueryBuilder bzBuilder=QueryBuilders.boolQuery();
List<QueryBuilder> bzMust=bzBuilder.must();
bzMust.add(QueryBuilders.termQuery("bool_verified",false));
bzMust.add(QueryBuilders.rangeQuery("long_followersCount").to(10L));
FiltersAggregator.KeyedFilter bzFilter=new FiltersAggregator.KeyedFilter("bots_zombies",bzBuilder);
searchSourceBuilder.aggregation(AggregationBuilders.filters("user_quality",hqaFilter,raFilter,bzFilter));
System.out.println(searchSourceBuilder);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 案例三
{"query":{"bool":{"must":[{"term":{"tkw_username.keyword":{"value":"username","case_insensitive":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"_source":{"includes":["id","long_followersCount"],"excludes":[]},"track_total_hits":2147483647}
1
// 根据索引创建查询请求
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
// 构建查询语句
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();
// 忽略大小写
boolQuery.must(QueryBuilders.termQuery("tkw_username.keyword","username").caseInsensitive(true));
String[]includeFields=new String[]{"id","long_followersCount"};
searchSourceBuilder.fetchSource(includeFields,new String[]{});
searchSourceBuilder.query(boolQuery);
System.out.println(searchSourceBuilder);
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 案例四
{"query":{"bool":{"must":[{"exists":{"field":"long_accountType","boost":1.0}},{"range":{"date_crawledAt":{"from":"2022-01-01 00:00:00","to":"2022-01-02 00:00:00","include_lower":true,"include_upper":false,"format":"yyyy-MM-dd HH:mm:ss","boost":1.0}}},{"bool":{"should":[{"range":{"date_col":{"from":null,"to":"2022-01-01 00:00:00","include_lower":true,"include_upper":false,"format":"yyyy-MM-dd HH:mm:ss","boost":1.0}}},{"bool":{"must_not":[{"exists":{"field":"date_col","boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"_source":{"includes":["id","long_followersCount","tkw_username","tkw_name","kw_profileImageUrl","long_followingCount","long_tweetCount","long_listedCount","bool_canDm","bool_suspended"],"excludes":[]},"track_total_hits":2147483647}
1
// 根据索引创建查询请求
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
searchSourceBuilder.trackTotalHits(true);
// 构建查询语句
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.existsQuery("long_accountType"));
boolQuery.must(QueryBuilders.rangeQuery("date_crawledAt").gte("2022-01-01 00:00:00").lt("2022-01-02 00:00:00").format("yyyy-MM-dd HH:mm:ss"));
boolQuery.must(QueryBuilders.boolQuery().should(QueryBuilders.rangeQuery("date_col").lt("2022-01-01 00:00:00").format("yyyy-MM-dd HH:mm:ss"))
.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("date_col"))));
searchSourceBuilder.fetchSource(new String[]{"id","long_followersCount","tkw_username","tkw_name","kw_profileImageUrl","long_followingCount","long_tweetCount","long_listedCount","bool_canDm","bool_suspended"},new String[]{});
searchSourceBuilder.query(boolQuery);
System.out.println(searchSourceBuilder);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13