当前位置:   article > 正文

stack-es-标准篇-ElasticsearchClient-filter_elasticsearchclient filter

elasticsearchclient filter

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
-侯氏工坊

功能

  • 对返回结果进行过滤
  • filter 对搜索和聚合有效
  • post_filter 只对搜索有效

关键字

  • bool
    • filter
  • post_filter

原语句

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"
    }
  }
}
  • 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
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

java实现

  • 实体
@Data
@EsIndex(index = "index_filter")
public class FilterEntity {
    private String brand;
    private String color;
    private String model;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 接口
public interface FilterService {

    /**
     * filter 过滤
     * @return
     * @throws Exception
     */
    SearchResponse<Object> filter() throws Exception;

    /**
     * post_filter过滤
     * @return
     * @throws Exception
     */
    SearchResponse<Object> postFilter() throws Exception;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 实现
@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"));
    }
}
  • 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
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • util
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))));
    }
}
  • 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

所需jar包

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/817735?site
推荐阅读
相关标签
  

闽ICP备14008679号