【问题标题】:Count of nested documents by aggregation : elasticsearch按聚合计算的嵌套文档数:elasticsearch
【发布时间】:2020-11-13 08:52:51
【问题描述】:

我的映射中有 2 级嵌套:business -> funds -> admins/market 我想要与特定 admin_name 匹配的 f_id's 计数

ES 映射

{
  "text_index" : {
    "mappings" : {
      "properties" : {
        "buisness_id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
        },
        "buisness_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "funds" : {
          "type" : "nested",
          "properties" : {
            "f_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
            "admins" : {
              "type" : "nested",
              "properties" : {
                "admin_name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  },
                },
                "admin_id" : {
                  "type" : "text",
                    "fields" : {
                      "keyword" : {
                        "type" : "keyword",
                        "ignore_above" : 256
                      }
                    }
                }
              }
            },
            "market" : {
              "type" : "nested",
              "properties" : {
                "mkt_name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "mkt_id" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

对 COUNT 的 AGGS 查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "funds",
            "query": {
              "nested": {
                "path": "funds.admins",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "funds.admins.admin_name": "NOMAD"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "funds_count": {
      "nested": {
        "path": "funds"
      },
      "aggs": {
        "f_count": {
          "value_count": {
            "field": "funds.f_id.keyword"
          }
        }
      }
    }
  }
}

但它没有给出正确的 f_id's 计数。

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-7


    【解决方案1】:

    我假设您想要 f_ids不同计数,而不是 总计数

    为此,只需使用Cardinality Aggregation 而不是Value Count Aggregation

    您还可以缩短嵌套查询,即以下内容应该对您有所帮助:

    POST <your_index_name>/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "funds.admins",                    <---- Note this
                "query": {
                  "match": {
                    "funds.admins.admin_name": "nomad"     <---- Also note this to make use of keyword for case sensitivity and exact match
                  }
                }
              }
            }
          ]
        }
      }, 
      "aggs": {
        "funds_count": {
          "nested": {
            "path": "funds"
          },
          "aggs": {
            "f_count": {
              "cardinality": {                    <---- Note this
                "field": "funds.f_id.keyword"
              }
            }
          }
        }
      }
    } 
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 2020-08-26
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      相关资源
      最近更新 更多