TestDslController.java 2 KB
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());
	}

}