ElasticSearch - 无法从多级嵌套对象中获取嵌套对象

ElasticSearc - unable to get nested object from multilevel nested object

我有多层嵌套对象。第一个嵌套对象是类别,在类别内部还有一个嵌套对象是组。 所以我想使用聚合查询获得不同的类别以及嵌套组。 我成功获得了独特的类别,但无法获得组的详细信息。

映射:

  "mappings": {
  "doc": {
    "properties": {
      "categories": {
        "type": "nested",
        "properties": {
          "cat_id": {
            "type": "integer"
          },
          "cat_name": {
            "type": "keyword"
          },
          "cat_slug": {
            "type": "keyword"
          },
          "cat_type": {
            "type": "long"
          },
          "groups": {
            "type": "nested",
            "properties": {
              "group": {
                "type": "keyword"
              },
              "group_name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "id": {
                "type": "long"
              }
            }
          },
          "ordering": {
            "type": "integer"
          },
          "parent_id": {
            "type": "integer"
          },
          "parent_name": {
            "type": "keyword"
          },
          "parent_slug": {
            "type": "keyword"
          },
          "parent_type": {
            "type": "long"
          }
        }
      }
    }
  }
}

示例数据:

    {
  "_index": "product",
  "_type": "doc",
  "_id": "18556",
  "_score": 1,
  "_source": {
    "sku": "GR0005P08",
    "product_id": 18556,
    "slug": "blue-garter-with-sexy-laces",
    "categories": [
      {
        "ordering": 10,
        "cat_id": 343,
        "cat_type": 1,
        "cat_slug": "t-thisr",
        "cat_name": "cat1"
      },
      {
        "ordering": 9999999,
        "cat_id": 2,
        "cat_type": 3,
        "cat_slug": "pajams",
        "cat_name": "pajams"
      },
      {
        "ordering": 5,
        "cat_id": 77,
        "cat_type": 3,
        "cat_slug": "accessories",
        "cat_name": "Accessories"
      },
      {
        "parent_name": "Pajams",
        "cat_name": "Night",
        "ordering": 1,
        "cat_id": 139,
        "parent_type": 3,
        "cat_slug": "night",
        "parent_id": 2,
        "groups": [
          {
            "id": 146,
            "group_name": "Shop By Style"
          },
          {
            "id": 481,
            "group_name": "Shop By Offer "
          }
        ],
        "parent_slug": "pajams",
        "cat_type": 1
      }
    ],
    "name": "love for pajams"
  }
}

这是我的聚合查询:

    GET product/_search
{
  "_source": [
    "product_id"
  ],
  "query": {
    "nested": {
      "path": "categories",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "categories.cat_slug": "xyz"
              }
            }

          ]
        }
      }
    }
  },
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "distinct_categories.cat_name": {
          "terms": {
            "field": "categories.cat_name"
          },
          "aggs": {
            "categories.groups.group_name": {
              "terms": {
                "field": "categories.groups.group_name.keyword"
              }
            }
          }
        }
      }
    }
  }
}

这是我的回复:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 3.232121,
    "hits": [
      {
        "_index": "product",
        "_type": "doc",
        "_id": "15621",
        "_score": 3.232121,
        "_source": {
          "product_id": 15621
        }
      },
      {
        "_index": "product",
        "_type": "doc",
        "_id": "18556",
        "_score": 2.5758784,
        "_source": {
          "product_id": 18556
        }
      }
    ]
  },
  "aggregations": {
    "categories": {
      "doc_count": 98,
      "distinct_categories.cat_name": {
        "doc_count_error_upper_bound": 2,
        "sum_other_doc_count": 50,
        "buckets": [
          {
            "key": "Accessories",
            "doc_count": 8,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "T-shirt",
            "doc_count": 8,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Sexy",
            "doc_count": 7,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "clothing",
            "doc_count": 6,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Pants",
            "doc_count": 6,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Colour Me",
            "doc_count": 4,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Pajamas",
            "doc_count": 3,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          }
        ]
      }
    }
  }
}

可能会尝试 "field": "categories.groups.group_name" 而不是 "field": "categories.groups.group_name.keyword" 在你最深的聚合中?

编辑:

使用"categories.groups.group_name.keyword" 是实现此目的的正确方法。问题应该是您需要为查询的嵌套结构的每个级别添加 nested

"aggs": {
"categories": {
  "nested": {
    "path": "categories"
  },
  "aggs": {
    "distinct_categories.cat_name": {
      "terms": {
        "field": "categories.cat_name"
      },
      "aggs": {
        "deeper_nested_agg": {
          "nested": {
            "path": "categories.groups"
          },
          "aggs": {
            "categories.groups.group_name": {
              "terms": {
                "field": "categories.groups.group_name.keyword"
              }
            }
          }
        }
      }
    }
  }
}

请试一试。希望这对您有所帮助!

我发现 groups 的映射定义在更改查询工作正常后存在问题。

{
"type": "nested",
"properties": {
  "group_name": {
    "type": "keyword"
  },
  "id": {
    "type": "long"
      }
    }
  }