TestDslController.java
2 KB
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
package com.yoho.search.restapi.tools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.utils.SearchParamUtils;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
public class TestDslController {
@RequestMapping(method = RequestMethod.GET, value = "/testDsl")
@ResponseBody
public JSONObject testDsl() {
SearchParam searchParam = new SearchParam();
TermsQueryBuilder sknFilter = QueryBuilders.termsQuery("productSkn", "50002406");
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("pools", QueryBuilders.termQuery("pools.pool_id", "130") ,ScoreMode.None);
searchParam.setFiter(QueryBuilders.boolQuery().must(sknFilter).must(nestedQueryBuilder));
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<AbstractAggregationBuilder<?>>();
aggregationBuilders.add(AggregationBuilders.nested("test1", "pools").subAggregation(AggregationBuilders.filter("nested_filter", QueryBuilders.termQuery("pools.pool_id", "130")).subAggregation(AggregationBuilders.terms("pool_id").field("pools.pool_id"))));
searchParam.setAggregationBuilders(aggregationBuilders);
SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
return JSON.parseObject(searchSourceBuilder.toString());
}
}