赞
踩
上文【ElasticSearch】spring-data方式操作elasticsearch(一) 的延续
1.添加依赖
<!-- 添加 elasticsearch 客户端 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.添加ES配置
#使用模板方式
spring:
elasticsearch:
rest:
uris:
- http://localhost:9200
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 } ]
/** * @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)); } }
@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)); }
类似SQL:
select * from people where age = 45
URL查询:
{
"query":{
"term":{
"age":{
"boost":1.0,
"value":45
}
}
}
}
@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)); }
类似SQL:
select * from people where professional in ('演员', '歌手')
URL:
{
"query":{
"terms":{
"professional.keyword":[
"演员",
"歌手"
],
"boost":1.0
}
}
}
@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)); }
类似SQL:
select * from people where age between 50 and 55
URL:
{
"query":{
"range":{
"age":{
"include_lower":false,
"include_upper":true,
"from":50,
"boost":1.0,
"to":55
}
}
}
}
@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)); }
类似SQL:
select * from people where professional like '武打%'
URL:
{
"query":{
"prefix":{
"professional.keyword":{
"boost":1.0,
"value":"武打"
}
}
}
}
@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)); }
类似SQL:
select * from people where professional like '%打%员'
URL:
{
"query":{
"wildcard":{
"professional.keyword":{
"boost":1.0,
"wildcard":"*打*员"
}
}
}
}
@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)); }
类似SQL:
select max(age) as ageMax from people
URL:
{
"size":0,
"aggregations":{
"ageMax":{
"max":{
"field":"age"
}
}
}
}
@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)); }
类似SQL:
select count(distinct uid) as uidCount from people
URL:
{
"size":0,
"aggregations":{
"uidCount":{
"cardinality":{
"field":"uid"
}
}
}
}
@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)); }
l类似SQL:
select age, count(*) from people group by age
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" } ] } } } }
@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)); }
类似SQL:
select age as key , count(*) as count
from people group by age
order by count asc, key desc
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" } ] } } } }
@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)); }
l类似SQL:
select age , professional, count(*) as count
from people
group by age, professional
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" } ] } } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。