【问题标题】:ElasticSearc - unable to get nested object from multilevel nested objectElasticSearch - 无法从多级嵌套对象中获取嵌套对象
【发布时间】:2019-02-18 15:39:27
【问题描述】:

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

映射:

  "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": []
            }
          }
        ]
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    在您最深入的聚合中可能尝试"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"
                  }
                }
              }
            }
          }
        }
      }
    }
    

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

    【讨论】:

    • 嗨飞,感谢您的回复。如果我使用“categories.groups.group_name”,我会出错。“type”:“illegal_argument_exception”,“reason”:“Fielddata is disabled on默认为文本字段。在 [categories.groups.group_name] 上设置 fielddata=true,以便通过反转倒排索引将字段数据加载到内存中。请注意,这可能会占用大量内存。或者,请改用关键字字段。"
    • 嘿。抱歉,我看你的问题不够仔细。我认为您可能需要在查询中添加两个嵌套才能使聚合起作用。我已经更新了我的答案,请试一试。
    • 你好 Fei,感谢您宝贵的时间和精力,问题已解决。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多