elasticsearch bucket terms复合聚合

elasticsearch bucket terms composite aggregation

这是我的示例文档,我正在 kibana 中借助 POST /user_data1/_doc/ 为类似文档编制索引。

{
  "id": 15,
  "name": "abcd",
  "source": "csv_status",
  "profile_complition": "70%",
  "creation_date": "2020-04-02",
  "current_position": [
    {
      "position": "Financial Reporting",
      "position_category": "Finance",
      "position_level": 2
    }
  ],
  "seeking_position": [
    {
      "position": "Financial Planning and Analysis",
      "position_category": "Finance",
      "position_level": 3
    }
  ],
  "last_updation_date": "2021-02-02",
  "experience": [
    {
      "brand": "Other",
      "company": "Other-Apartments and Residences",
      "brand_segment": "Luxury",
      "property_type": "All-Inclusive",
      "duration": "2 years",
      "real_estate_type": "Institutional"
    },
    {
      "brand": "Accor",
      "company": "Accor LLC",
      "brand_segment": "Luxury",
      "property_type": "Condo",
      "duration": "2 years",
      "real_estate_type": "Family Office"
    },
    {
      "brand": "SO",
      "company": "Accor LLC",
      "brand_segment": "Luxury",
      "property_type": "Condo",
      "duration": "2 years",
      "real_estate_type": "Family Office"
    },
    {
      "brand": "Other",
      "company": "Other-Multi-Family",
      "brand_segment": "Independent",
      "property_type": "Convention",
      "duration": "2 years",
      "real_estate_type": "Family Office"
    },
    {
      "brand": "Other Lifestyle – Luxury",
      "company": "Other Lifestyle – Luxury",
      "brand_segment": "Extended Stay",
      "property_type": "Condo",
      "duration": "2 years",
      "real_estate_type": "Family Office"
    }
  ]
}

现在文档中 'experience' 键 'brand' 是主组 'company' 的子组。正如您在文档中看到的那样,'brand' 可能有类似 'other' 的条目,但由 'company' 键分隔。类似的结构可以存在于文档内部和文档之间。我打算根据 'brand' 和 'company' 聚合文档。我试过的查询是-

GET user_data1/_search
{
  "size": 0,
  "aggs": {
    "nested_aggs": {
      "nested": {
        "path": "experience"
      },
      "aggs": {
        "by_brand": {
          "terms": {
            "field": "experience.brand"
          },
          "aggs": {
            "by_company": {
              "terms": {
                "field": "experience.company"
              }
            }
          }
        }
      }
    }
  }
}

为此我收到错误消息-

{
  "error" : {
    "root_cause" : [
      {
        "type" : "aggregation_execution_exception",
        "reason" : "[nested] nested path [experience] is not nested"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "user_data1",
        "node" : "aa0oyCw6RLa2J5aDALunsA",
        "reason" : {
          "type" : "aggregation_execution_exception",
          "reason" : "[nested] nested path [experience] is not nested"
        }
      }
    ]
  },
  "status" : 500
}

这是我文档的映射-

{
  "user_data1" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "creation_date" : {
          "type" : "date"
        },
        "current_position" : {
          "properties" : {
            "position" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "position_category" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "position_level" : {
              "type" : "long"
            }
          }
        },
        "experience" : {
          "properties" : {
            "brand" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "brand_segment" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "company" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "duration" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "property_type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "real_estate_type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "last_updation_date" : {
          "type" : "date"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "profile_complition" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "seeking_position" : {
          "properties" : {
            "position" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "position_category" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "position_level" : {
              "type" : "long"
            }
          }
        },
        "source" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "user_data1",
        "creation_date" : "1615227708037",
        "number_of_replicas" : "1",
        "uuid" : "8B3dnDKKQjmDNkjAksduJQ",
        "version" : {
          "created" : "7100299"
        }
      }
    }
  }
}

我知道我的经验字段没有嵌套,这可能就是这个错误的原因。我对 elasticsearch 比较陌生,因此发现在索引时很难使我的文档嵌套类型。我尝试通过映射使其嵌套,然后填充文档,但错误仍然存​​在。 帮我

  1. 在索引时嵌套所需的字段。
  2. 根据'brand'和'country'编写桶聚合的复合聚合。

如果需要进一步说明,请告诉我

错误清楚地表明experience字段不是nested类型。您需要将索引映射修改为 -

{
  "mappings": {
    "properties": {
      "experience": {
        "type": "nested"
      }
    }
  }
}

点击问题中给出的相同搜索查询,搜索结果为

"aggregations": {
    "nested_aggs": {
      "doc_count": 5,
      "by_brand": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "Other",
            "doc_count": 2,
            "by_company": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Other-Apartments and Residences",
                  "doc_count": 1
                },
                {
                  "key": "Other-Multi-Family",
                  "doc_count": 1
                }
              ]
            }
          },
          {
            "key": "Accor",
            "doc_count": 1,
            "by_company": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Accor LLC",
                  "doc_count": 1
                }
              ]
            }
          },
          {
            "key": "Other Lifestyle – Luxury",
            "doc_count": 1,
            "by_company": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Other Lifestyle – Luxury",
                  "doc_count": 1
                }
              ]
            }
          },
          {
            "key": "SO",
            "doc_count": 1,
            "by_company": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Accor LLC",
                  "doc_count": 1
                }
              ]
            }
          }
        ]
      }
    }
  }