当前位置:   article > 正文

【ElasticSearch】RestHighLevelClient方式对ES进行操作(三)_resthighlevelclient新增字段

resthighlevelclient新增字段

使用 RestHighLevelClient 对ES进行操作(三)



上文【ElasticSearch】spring-data方式操作elasticsearch(一) 的延续


环境准备


1.添加依赖

<!-- 添加 elasticsearch 客户端 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.添加ES配置

#使用模板方式
spring:
  elasticsearch:
    rest:
      uris:
      - http://localhost:9200
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.添加测试模拟数据:

[
	{
		"addr":"香港",
		"age":45,
		"birthDay":"1977-01-01",
		"interest":"赛车",
		"name":"吴彦祖",
		"professional":"演员",
		"uid":1
	},
	{
		"addr":"大陆",
		"age":55,
		"birthDay":"1967-01-01",
		"interest":"演戏",
		"name":"吴奇隆",
		"professional":"歌手",
		"uid":2
	},
	{
		"addr":"大陆",
		"age":45,
		"birthDay":"1977-01-01",
		"interest":"武术",
		"name":"吴京",
		"professional":"武打演员",
		"uid":3
	},
	{
		"addr":"香港",
		"age":55,
		"birthDay":"1967-01-01",
		"interest":"唱歌",
		"name":"古天乐",
		"professional":"演员",
		"uid":4
	},
	{
		"addr":"大陆",
		"age":35,
		"birthDay":"1987-01-01",
		"interest":"跑步",
		"name":"苏炳添",
		"professional":"运动员",
		"uid":5
	},
	{
		"addr":"大陆",
		"age":30,
		"birthDay":"1992-01-01",
		"interest":"演戏",
		"name":"刘亦菲",
		"professional":"歌手",
		"uid":6
	},
	{
		"addr":"大陆",
		"age":30,
		"birthDay":"1992-01-01",
		"interest":"健身",
		"name":"刘翔",
		"professional":"运动员",
		"uid":7
	},
	{
		"addr":"香港",
		"age":45,
		"birthDay":"1977-01-01",
		"interest":"唱歌",
		"name":"张家辉",
		"professional":"演员",
		"uid":8
	},
	{
		"addr":"大陆",
		"age":45,
		"birthDay":"1977-01-01",
		"interest":"唱歌",
		"name":"毛不易",
		"professional":"歌手",
		"uid":9
	}
]
  • 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
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

3.0 查询所有

/**
 * @Description: 
 * 使用 client方式
 * @author: TianwYam
 * @date 2022年3月23日 下午9:47:23
 */
@SpringBootTest
public class PeopleTestUseClient {
	
	
	@Autowired
	private RestHighLevelClient esClient ;
	
	@Test
	public void getAll() throws Exception {
		
		SearchRequest searchRequest = new SearchRequest("people_index");
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		
		System.out.println("参数:");
		System.out.println(sourceBuilder);
		
		searchRequest.source(sourceBuilder);
		SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
		System.out.println("结果:");
		System.out.println(JSON.toJSONString(response, true));
	}
	
}
  • 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

3.1 等值查询 =

@Test
public void where() throws Exception {

    // 类似:select * from people where age = 45

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.query(QueryBuilders.termQuery("age", 45));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

类似SQL:

select * from people where age = 45
  • 1

URL查询:

{
	"query":{
		"term":{
			"age":{
				"boost":1.0,
				"value":45
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.2 多值查询 in

@Test
public void in() throws Exception {

    // 类似:select * from people where professional in ('演员', '歌手')

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.query(QueryBuilders.termsQuery("professional.keyword", Arrays.asList("演员","歌手")));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

类似SQL:

select * from people where professional in ('演员', '歌手')
  • 1

URL:

{
	"query":{
		"terms":{
			"professional.keyword":[
				"演员",
				"歌手"
			],
			"boost":1.0
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.3 范围查询

@Test
public void betweenAnd() throws Exception {

    // 类似:select * from people where age between 50 and 55

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    //		sourceBuilder.query(QueryBuilders.rangeQuery("age").from(50).to(55));

    //  50 < age <= 55
    sourceBuilder.query(QueryBuilders.rangeQuery("age").gt(50).lte(55));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

类似SQL:

select * from people where age between 50 and 55
  • 1

URL:

{
	"query":{
		"range":{
			"age":{
				"include_lower":false,
				"include_upper":true,
				"from":50,
				"boost":1.0,
				"to":55
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.4 模糊查询

@Test
public void likeLeft() throws Exception {

    // 类似:select * from people where professional like '武打%'

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.query(QueryBuilders.prefixQuery("professional.keyword", "武打"));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

类似SQL:

select * from people where professional like '武打%'
  • 1

URL:

{
	"query":{
		"prefix":{
			"professional.keyword":{
				"boost":1.0,
				"value":"武打"
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.5 通配符模糊查询

@Test
public void likeAny() throws Exception {

    // 类似:select * from people where professional like '%打%员'

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    // 通配符模糊查询
    sourceBuilder.query(QueryBuilders.wildcardQuery("professional.keyword", "*打*员"));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

类似SQL:

select * from people where professional like '%打%员'
  • 1

URL:

{
	"query":{
		"wildcard":{
			"professional.keyword":{
				"boost":1.0,
				"wildcard":"*打*员"
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.6 统计查询(最大、最小、平均、求和)


max\min\avg\sum\count等
@Test
public void max() throws Exception {

    // 类似:select max(age) as ageMax from people 

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.size(0);
    sourceBuilder.aggregation(AggregationBuilders.max("ageMax").field("age"));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

类似SQL:

select max(age) as ageMax from people
  • 1

URL:

{
	"size":0,
	"aggregations":{
		"ageMax":{
			"max":{
				"field":"age"
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.7 去重

@Test
public void distinct() throws Exception {

    // 类似:select count(distinct uid) as uidCount from people 

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.size(0);
    sourceBuilder.aggregation(AggregationBuilders.cardinality("uidCount").field("uid"));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

类似SQL:

select count(distinct uid) as uidCount from people 
  • 1

URL:

{
	"size":0,
	"aggregations":{
		"uidCount":{
			"cardinality":{
				"field":"uid"
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.8 分组聚合

@Test
public void groupBy() throws Exception {

    // 类似:select age, count(*) from people group by age

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.size(0);
    sourceBuilder.aggregation(AggregationBuilders.terms("ageGroup").field("age"));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

l类似SQL:

select age, count(*) from people group by age
  • 1

URL:

{
	"size":0,
	"aggregations":{
		"ageGroup":{
			"terms":{
				"shard_min_doc_count":0,
				"field":"age",
				"size":10,
				"show_term_doc_count_error":false,
				"min_doc_count":1,
				"order":[
					{
						"_count":"desc"
					},
					{
						"_key":"asc"
					}
				]
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3.9 分组排序

@Test
public void groupByOrder() throws Exception {

    // 类似:select age as key , count(*) as count from people group by age order by count asc, key desc

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.size(0);
    sourceBuilder.aggregation(AggregationBuilders.terms("ageGroup")
                              .field("age")
                              .order(BucketOrder.compound(BucketOrder.count(true), BucketOrder.key(false))));
    //		sourceBuilder.aggregation(AggregationBuilders.terms("ageGroup").field("age").order(BucketOrder.key(false)));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(JSON.toJSONString(response, true));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

类似SQL:

select age as key , count(*) as count 
from people group by age 
order by count asc, key desc
  • 1
  • 2
  • 3

URL:

{
	"size":0,
	"aggregations":{
		"ageGroup":{
			"terms":{
				"shard_min_doc_count":0,
				"field":"age",
				"size":10,
				"show_term_doc_count_error":false,
				"min_doc_count":1,
				"order":[
					{
						"_count":"asc"
					},
					{
						"_key":"desc"
					}
				]
			}
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3.10 多值分组聚合

@Test
public void groupByMore() throws Exception {

    // 类似:select age , professional, count(*) as count from people group by age, professional

    SearchRequest searchRequest = new SearchRequest("people_index");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    sourceBuilder.size(0);
    sourceBuilder.aggregation(AggregationBuilders.terms("ageGroup")
                              .field("age")
                              .subAggregation(AggregationBuilders.terms("profGroup")
                                              .field("professional.keyword")));
    //		sourceBuilder.aggregation(AggregationBuilders.terms("ageGroup").field("age").order(BucketOrder.key(false)));

    System.out.println("参数:");
    System.out.println(JSON.toJSONString(JSON.parse(sourceBuilder.toString()), true));

    searchRequest.source(sourceBuilder);
    SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println("结果:");
    System.out.println(response);
    //		System.out.println(JSON.toJSONString(response, true));
}

  • 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

l类似SQL:

select age , professional, count(*) as count 
from people 
group by age, professional
  • 1
  • 2
  • 3

URL:

{
	"size":0,
	"aggregations":{
		"ageGroup":{
			"terms":{
				"shard_min_doc_count":0,
				"field":"age",
				"size":10,
				"show_term_doc_count_error":false,
				"min_doc_count":1,
				"order":[
					{
						"_count":"desc"
					},
					{
						"_key":"asc"
					}
				]
			},
			"aggregations":{
				"profGroup":{
					"terms":{
						"shard_min_doc_count":0,
						"field":"professional.keyword",
						"size":10,
						"show_term_doc_count_error":false,
						"min_doc_count":1,
						"order":[
							{
								"_count":"desc"
							},
							{
								"_key":"asc"
							}
						]
					}
				}
			}
		}
	}
}
  • 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
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/68577
推荐阅读
相关标签
  

闽ICP备14008679号