【问题标题】:elasticsearch bucket terms composite aggregationelasticsearch桶术语复合聚合
【发布时间】:2021-06-06 16:15:46
【问题描述】:

这是我的示例文档,我在 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"
    }
  ]
}

现在在“体验”键下的文档中,“品牌”是主组“公司”的子组。正如您在文档中看到的那样,“品牌”可能具有类似“其他”的条目,但由“公司”键分隔。类似的结构可以存在于文档内部和文档之间。我打算根据“品牌”和“公司”来汇总文档。我试过的查询是-

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"
        }
      }
    }
  }
}

我了解我的经验字段没有嵌套,这可能是导致此错误的原因。我对弹性搜索相对较新,因此在索引时很难使我的文档嵌套类型。我尝试通过映射使其嵌套然后填充文档,但错误仍然相同。 帮帮我

  1. 在索引时将所需字段嵌套。
  2. 编写复合聚合,该聚合基于“品牌”和“国家/地区”进行存储桶聚合。

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

【问题讨论】:

    标签: elasticsearch kibana elasticsearch-aggregation


    【解决方案1】:

    错误清楚地表明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
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    

    【讨论】:

    • 您好,我知道我必须将我的体验文档设为嵌套类型,但不知道怎么做?我已经尝试通过最初提供嵌套在映射中的类型然后将我的文档索引到索引来使其嵌套,而我所做的另一个尝试是最初索引所有文档然后执行“PUT user_data/_mapping/ {“properties”:{“experience”: { { “属性”:{ “类型”:“嵌套”} } } } }”。首先是成功,但查询错误保持不变。第二个直接报错。
    • @RicheshChouksey 在第一种方法中运行搜索查询时遇到什么错误?
    • 嗨@ESCoder,您的上述建议已解决。我在映射之前缺少 {}。谢谢
    猜你喜欢
    • 2021-06-06
    • 2014-07-09
    • 1970-01-01
    • 2015-11-06
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2020-10-30
    相关资源
    最近更新 更多