|
@@ -26,6 +26,8 @@ import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.elasticsearch.index.query.*;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
@@ -196,11 +198,18 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
searchBuilder.highlighter(highlightBuilder);
|
|
|
|
|
|
//查询后分组
|
|
|
- TermsAggregationBuilder aggregation = AggregationBuilders
|
|
|
+
|
|
|
+ TermsAggregationBuilder aggregation= AggregationBuilders.terms("group_well")
|
|
|
+ .field("well_id")
|
|
|
+ .subAggregation(
|
|
|
+ AggregationBuilders.terms("group_well2")
|
|
|
+ .field("well_common_name.keyword")
|
|
|
+ );
|
|
|
+ /*TermsAggregationBuilder aggregation = AggregationBuilders
|
|
|
.terms("group_well")
|
|
|
- .field("well_id");
|
|
|
+ .field("well_common_name.keyword");
|
|
|
+ */
|
|
|
searchBuilder.aggregation(aggregation);
|
|
|
-
|
|
|
//[5]Es发起查询
|
|
|
searchRequest.source(searchBuilder);
|
|
|
|
|
@@ -337,70 +346,6 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
return query;
|
|
|
}
|
|
|
|
|
|
- /* public Map<String, Object> queryTest(String text, int page, int limit) {
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("total", 0);
|
|
|
-
|
|
|
- try {
|
|
|
- IndicesResponse indicesResponse = esClient.cat().indices();
|
|
|
- indicesResponse.valueBody().forEach(i -> {
|
|
|
- System.out.println("get all index, health: " + i.health() + ", status: " + i.status() + ", index: " + i.index());
|
|
|
- });
|
|
|
-
|
|
|
- //建立查询参数分析
|
|
|
- SearchRequest.Builder searchRequest = new SearchRequest.Builder();
|
|
|
- //要查询的索引列表
|
|
|
- String[] indexs = new String[]{"dws_basic_info_history", "dws_dm_test_history", "fact_dwr_well_basic_information"};
|
|
|
- searchRequest.index(Arrays.asList(indexs));
|
|
|
- //数据分页显示
|
|
|
- searchRequest.size(limit);
|
|
|
- searchRequest.from(page * limit);
|
|
|
-
|
|
|
- //生成查询参数
|
|
|
- if (!StringUtils.IsNullEmpty(text)) {
|
|
|
- //非嵌套字段查询
|
|
|
- String[] fields = new String[]{"well_common_name", "testing_name"};
|
|
|
- Query query1 = MultiMatchQuery.of(q -> q.fields(Arrays.asList(fields)).query(text).operator(Operator.Or))._toQuery();
|
|
|
- //嵌套字段查询
|
|
|
- String[] nestedFields = new String[]{"historys.testing_name"};
|
|
|
- Query nestedQuery = MultiMatchQuery.of(q -> q.fields(Arrays.asList(nestedFields)).query(text).operator(Operator.Or))._toQuery();
|
|
|
- Query query3 = NestedQuery.of(q -> q.path("historys").query(nestedQuery).ignoreUnmapped(true))._toQuery();
|
|
|
- //对比类型查询
|
|
|
- Query query2 = RangeQuery.of(q -> q.field("authorized_md").gte(JsonData.of(500)))._toQuery();
|
|
|
-
|
|
|
- Query[] arys = new Query[]{query1, query2, query3};
|
|
|
- searchRequest.query(q -> q.bool(b -> b.should(Arrays.asList(arys))));
|
|
|
- //高亮设置
|
|
|
- searchRequest.highlight(h -> h.fields("historys.testing_name", f -> f.matchedFields("historys.testing_name")));
|
|
|
- }
|
|
|
-
|
|
|
- //Es发起查询
|
|
|
- SearchRequest request = searchRequest.build();
|
|
|
- SearchResponse response = esClient.search(request, ObjectNode.class);
|
|
|
-
|
|
|
- //转换结果,可以对不同的index做出参数输出
|
|
|
- List<Map<String, Object>> rows = searchResponse2List(response);
|
|
|
- result.put("rows", rows);
|
|
|
- result.put("total", response.hits().total().value());
|
|
|
- System.out.println(response.hits().total() + " " + request.toString());
|
|
|
- } catch (Exception ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public void updateMappingTest() throws IOException {
|
|
|
- Map<String, Property> maps = new HashMap<>();
|
|
|
- maps.put("well_common_name", Property.of(p -> p.text(TextProperty.of(t -> t.index(true).analyzer("ik_max_word")))));
|
|
|
- updateMappings("fact_dwr_well_basic_information", maps);
|
|
|
- }
|
|
|
-
|
|
|
- public void updateMappings(String index, Map<String, Property> maps) throws IOException {
|
|
|
- PutMappingRequest putMappingRequest = PutMappingRequest.of(m -> m.index(index).properties(maps));
|
|
|
- PutMappingResponse putMappingResponse = esClient.indices().putMapping(putMappingRequest);
|
|
|
- boolean acknowledged = putMappingResponse.acknowledged();
|
|
|
- System.out.println("update mappings ack: " + acknowledged);
|
|
|
- }*/
|
|
|
|
|
|
public JsonNode stringToNodeJson(String jsonString) throws JsonProcessingException {
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
@@ -416,6 +361,7 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
for (Terms.Bucket bucket : groupWell.getBuckets()) {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("key", bucket.getKeyAsString());
|
|
|
+ map.put("name",((Terms)bucket.getAggregations().get("group_well2")).getBuckets().get(0).getKeyAsString());
|
|
|
map.put("doc_count", bucket.getDocCount());
|
|
|
aggs.add(map);
|
|
|
}
|