|
@ -3,131 +3,25 @@ package com.ai.bss.es.esmanage.controller;
|
3
|
3
|
import com.ai.bss.es.esmanage.service.EsService;
|
4
|
4
|
import com.ai.bss.es.esmanage.utils.RequestEntity;
|
5
|
5
|
import com.ai.bss.es.esmanage.utils.ResponseResult;
|
6
|
|
import org.elasticsearch.action.index.IndexRequest;
|
7
|
|
import org.elasticsearch.action.index.IndexResponse;
|
8
|
|
import org.elasticsearch.action.search.SearchRequest;
|
9
|
|
import org.elasticsearch.action.search.SearchResponse;
|
10
|
|
import org.elasticsearch.client.RequestOptions;
|
11
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
12
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
13
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
14
|
|
import org.elasticsearch.index.query.MatchQueryBuilder;
|
15
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
16
|
|
import org.elasticsearch.search.SearchHit;
|
17
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
18
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
19
|
|
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
|
20
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
21
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
|
import org.springframework.beans.factory.annotation.Value;
|
23
|
7
|
import org.springframework.web.bind.annotation.PostMapping;
|
24
|
8
|
import org.springframework.web.bind.annotation.RequestBody;
|
25
|
9
|
import org.springframework.web.bind.annotation.RequestMapping;
|
26
|
10
|
import org.springframework.web.bind.annotation.RestController;
|
27
|
11
|
|
28
|
12
|
import java.io.IOException;
|
29
|
|
import java.util.ArrayList;
|
30
|
|
import java.util.List;
|
31
|
|
import java.util.Map;
|
32
|
13
|
|
33
|
14
|
@RestController
|
34
|
15
|
@RequestMapping("/es")
|
35
|
16
|
public class EsController {
|
36
|
17
|
|
37
|
18
|
@Autowired
|
38
|
|
private RestHighLevelClient client;
|
39
|
|
|
40
|
|
@Autowired
|
41
|
19
|
private EsService esService;
|
42
|
20
|
|
43
|
21
|
@PostMapping("/execute")
|
44
|
22
|
ResponseResult execute(@RequestBody RequestEntity requestEntity) throws IOException {
|
45
|
|
// SearchRequest requestEs = null;
|
46
|
|
// String entity = requestEntity.getEntity();
|
47
|
|
// String methods = requestEntity.getMethods();
|
48
|
|
// String tableName = requestEntity.getTableName();
|
49
|
|
// String type = requestEntity.getType();
|
50
|
|
// if("search".equals(methods)){
|
51
|
|
// requestEs = new SearchRequest(tableName);
|
52
|
|
// SearchResponse searchResponse = client.search(requestEs, RequestOptions.DEFAULT);
|
53
|
|
//
|
54
|
|
// //6、开始处理返回的数据
|
55
|
|
// SearchHit[] hits = searchResponse.getHits().getHits();
|
56
|
|
// List<String> list = new ArrayList<String>();
|
57
|
|
// for (SearchHit hit : hits) {
|
58
|
|
// String hitString = hit.getSourceAsString();
|
59
|
|
// System.out.println(hitString);
|
60
|
|
// list.add(hitString);
|
61
|
|
// }
|
62
|
|
// ResponseResult.sucess(list);
|
63
|
|
// }
|
64
|
|
//
|
65
|
|
// if("GET".equals(methods)){
|
66
|
|
// requestEs = new SearchRequest(tableName);
|
67
|
|
// SearchResponse searchResponse = null;//purePersist(requestEs, RequestOptions.DEFAULT);
|
68
|
|
//
|
69
|
|
// //6、开始处理返回的数据
|
70
|
|
// SearchHit[] hits = searchResponse.getHits().getHits();
|
71
|
|
// List<String> list = new ArrayList<String>();
|
72
|
|
// for (SearchHit hit : hits) {
|
73
|
|
// String hitString = hit.getSourceAsString();
|
74
|
|
// System.out.println(hitString);
|
75
|
|
// list.add(hitString);
|
76
|
|
// }
|
77
|
|
// ResponseResult.sucess(list);
|
78
|
|
// }
|
79
|
23
|
|
80
|
24
|
return esService.executeEsEntity(requestEntity);
|
81
|
25
|
}
|
82
|
26
|
|
83
|
|
|
84
|
|
@RequestMapping("/test")
|
85
|
|
List<String> contextLoads() throws IOException {
|
86
|
|
//1、创建查询请求,规定查询的索引
|
87
|
|
SearchRequest request = new SearchRequest("es_terminal_event");
|
88
|
|
//2、创建条件构造
|
89
|
|
SearchSourceBuilder builder = new SearchSourceBuilder();
|
90
|
|
//3、构造条件
|
91
|
|
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("address", "Lane");
|
92
|
|
builder.query(matchQueryBuilder);
|
93
|
|
//聚合年龄分布
|
94
|
|
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age");
|
95
|
|
builder.aggregation(ageAgg);
|
96
|
|
//
|
97
|
|
// //聚合平均年龄
|
98
|
|
AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
|
99
|
|
builder.aggregation(balanceAvg);
|
100
|
|
|
101
|
|
List<String> ids = new ArrayList();
|
102
|
|
ids.add("981489");
|
103
|
|
ids.add("155682");
|
104
|
|
BoolQueryBuilder exersiceBoolQuery = QueryBuilders.boolQuery();
|
105
|
|
for (String exerciseId: ids)
|
106
|
|
{
|
107
|
|
exersiceBoolQuery.must(QueryBuilders.termQuery("resourceId", exerciseId));
|
108
|
|
}
|
109
|
|
builder.query(exersiceBoolQuery);
|
110
|
|
|
111
|
|
//4、将构造好的条件放入请求中
|
112
|
|
request.source(builder);
|
113
|
|
|
114
|
|
//5、开始执行发送request请求
|
115
|
|
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
//6、开始处理返回的数据
|
120
|
|
SearchHit[] hits = searchResponse.getHits().getHits();
|
121
|
|
List<String> list = new ArrayList<String>();
|
122
|
|
for (SearchHit hit : hits) {
|
123
|
|
String hitString = hit.getSourceAsString();
|
124
|
|
System.out.println(hitString);
|
125
|
|
list.add(hitString);
|
126
|
|
}
|
127
|
|
return list;
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
}
|
133
|
27
|
}
|