【问题标题】:Elasticsearch aggregation on different search in same query同一查询中不同搜索的 Elasticsearch 聚合
【发布时间】:2020-05-31 23:53:58
【问题描述】:

无论使用什么其他参数(术语、术语等),我都想进行查询以仅基于匹配进行聚合。 更具体地说,我有一个在线商店,我在其中使用多个过滤器(颜色、大小等)。如果我检查一个字段,例如颜色:红色,则不再聚合其他颜色。 我正在使用的一个解决方案是进行 2 个单独的查询(一个用于应用过滤器的搜索,另一个用于聚合。知道如何组合这 2 个单独的查询吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以利用post_filter,它不适用于您的聚合,但只会过滤要返回的hits。例如:

    创建商店

    PUT online_shop
    {
      "mappings": {
        "properties": {
          "color": {
            "type": "keyword"
          },
          "size": {
            "type": "integer"
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    

    用一些产品填充它

    POST online_shop/_doc
    {"color":"red","size":35,"name":"Louboutin High heels abc"}
    
    POST online_shop/_doc
    {"color":"black","size":34,"name":"Louboutin Boots abc"}
    
    POST online_shop/_doc
    {"color":"yellow","size":36,"name":"XYZ abc"}
    

    hitsaggregations 应用共享查询并使用post_filter 来...过滤后点击:

    GET online_shop/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "abc"
              }
            }
          ]
        }
      },
      "aggs": {
        "by_color": {
          "terms": {
            "field": "color"
          }
        },
        "by_size": {
          "terms": {
            "field": "size"
          }
        }
      },
      "post_filter": {
        "bool": {
          "must": [
            {
              "term": {
                "color": {
                  "value": "red"
                }
              }
            }
          ]
        }
      }
    }
    

    预期结果

    {
      ...
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.11750763,
        "hits" : [
          {
            "_index" : "online_shop",
            "_type" : "_doc",
            "_id" : "cehma3IBG_KW3EFn1QYa",
            "_score" : 0.11750763,
            "_source" : {
              "color" : "red",
              "size" : 35,
              "name" : "Louboutin High heels abc"
            }
          }
        ]
      },
      "aggregations" : {
        "by_color" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "black",
              "doc_count" : 1
            },
            {
              "key" : "red",
              "doc_count" : 1
            },
            {
              "key" : "yellow",
              "doc_count" : 1
            }
          ]
        },
        "by_size" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : 34,
              "doc_count" : 1
            },
            {
              "key" : 35,
              "doc_count" : 1
            },
            {
              "key" : 36,
              "doc_count" : 1
            }
          ]
        }
      }
    }
    
    

    【讨论】:

    • 有什么方法可以使用 from 和 size 实现分页,但仅适用于 post_filter?
    • from & size 不会影响 aggs,只会影响已查询的 hits。所以不确定你的意思。
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 2021-03-08
    • 2017-11-14
    • 2018-07-13
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多