赞
踩
世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
-侯氏工坊
PUT index_filter
{
"mappings": {
"properties": {
"brand": {"type": "keyword"},
"color": {"type": "keyword"},
"model": {"type": "keyword"}
}
}
}
GET index_filter/_mapping
POST index_filter/_bulk
{"create": {}}
{"brand": "gucci", "color": "red", "model": "slim"}
{"create": {}}
{"brand": "good", "color": "red", "model": "simple"}
{"create": {}}
{"brand": "gucci", "color": "yellow", "model": "slim"}
{"create": {}}
{"brand": "good", "color": "yellow", "model": "tim"}
{"create": {}}
{"brand": "gucci", "color": "blue", "model": "lim"}
{"create": {}}
{"brand": "good", "color": "blue", "model": "slim"}
# bool filter and aggs
GET /index_filter/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"color": "red"
}
}
]
}
},
"aggs": {
"model_aggs": {
"terms": {
"field": "model",
"size": 10
}
}
}
}
# post_filter and aggs
GET /index_filter/_search
{
"aggs": {
"model_aggs": {
"terms": {
"field": "model",
"size": 10
}
}
},
"post_filter": {
"term": {
"color": "red"
}
}
}
@Data
@EsIndex(index = "index_filter")
public class FilterEntity {
private String brand;
private String color;
private String model;
}
public interface FilterService {
/**
* filter 过滤
* @return
* @throws Exception
*/
SearchResponse<Object> filter() throws Exception;
/**
* post_filter过滤
* @return
* @throws Exception
*/
SearchResponse<Object> postFilter() throws Exception;
}
@Service
public class FilterServiceImpl implements FilterService {
private final ElasticsearchClient elasticsearchClient;
@Autowired
public FilterServiceImpl(ElasticsearchClient elasticsearchClient) {
this.elasticsearchClient = elasticsearchClient;
}
@Override
public SearchResponse<Object> filter() throws Exception {
return elasticsearchClient.search(search -> search
.index(getIndex())
.source(FilterUtils.sourceConfig())
.fields(getFields())
.query(filterQuery())
.aggregations(termsAggregation()),
Object.class);
}
@Override
public SearchResponse<Object> postFilter() throws Exception {
return elasticsearchClient.search(search -> search
.index(getIndex())
.source(FilterUtils.sourceConfig())
.fields(getFields())
.aggregations(termsAggregation())
.postFilter(termQuery()),
Object.class);
}
//--------------------------------------------------------------------------
private String getIndex() {
return EsAnnotationUtils.getIndex(FilterEntity.class);
}
private List<FieldAndFormat> getFields() {
return FilterUtils.fields(EsAnnotationUtils
.getFields(FilterEntity.class));
}
private Query filterQuery() {
return FilterUtils.filterQuery(FilterUtils.termQuery(EsAnnotationUtils
.getField(FilterEntity::getColor), FieldValue.of("red")));
}
private Map<String, Aggregation> termsAggregation() {
return FilterUtils.termsAggregation("model_aggs",
EsAnnotationUtils.getField(FilterEntity::getModel), 10);
}
private Query termQuery() {
return FilterUtils.termQuery(EsAnnotationUtils.getField(
FilterEntity::getColor), FieldValue.of("red"));
}
}
public class FilterUtils {
private FilterUtils() {}
public static SourceConfig sourceConfig() {
return SourceConfig.of(source -> source.fetch(false));
}
public static List<FieldAndFormat> fields(List<String> fields) {
return fields.stream().map(field -> FieldAndFormat.of(fieldAndFormat ->
fieldAndFormat.field(field))).collect(Collectors.toList());
}
public static Query filterQuery(List<Query> qs) {
return Query.of(query -> query.bool(bool -> bool.filter(qs)));
}
public static Query filterQuery(Query q) {
return Query.of(query -> query.bool(bool -> bool.filter(q)));
}
public static Query termQuery(String field, FieldValue fieldValue) {
return Query.of(query -> query.term(term -> term.field(field)
.value(fieldValue)));
}
public static Map<String, Aggregation> termsAggregation(
String name, String field, Integer size) {
return MapBuilder.of(name, Aggregation.of(aggregation -> aggregation
.terms(termsAggregation -> termsAggregation.field(field)
.size(size))));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。