首页 > 解决方案 > 嵌套字段上的 Elasticsearch 桶排序

问题描述

我对 Elastic 7.1.0 上的嵌套字段的存储桶排序有问题:我的索引具有以下映射:

 {
  "mapping": {
    "dynamic": "strict",
    "properties": {
      "created_at_timestamp": {
        "type": "date"
      },
      "url": {
        "type": "keyword",
      },
      "title": {
        "type": "keyword",
      },
      "entities": {
        "type": "nested",
        "properties": {
          "counter": {
            "type": "long"
          },
          "metric": {
            "type": "long"
          },
          "id": {
            "type": "long"
          },
          "relevance": {
            "type": "float"
          },
          "weighted_metric": {
            "type": "float"
          }
        }
      }
    }
  }
}

我需要通过“weighted_metric”对这些文档进行排序,并针对特定的实体 ID 进行过滤。我写了这个查询:

GET my_index/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "entities",
            "query": {
              "term": {
                "entities.id": "27374"
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "title": {
              "value": ""
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "by_url_and_title": {
      "composite": {
        "sources": [
          {
            "final_url": {
              "terms": {
                "field": "final_url"
              }
            }
          },
          {
            "title": {
              "terms": {
                "field": "title"
              }
            }
          }
        ]
      },
      "aggs": {
        "sum_metric": {
          "nested": {
            "path": "entities"
          },
          "aggs": {
            "weightedmetric": {
              "filters": {
                "filters": {
                  "new": {
                    "bool": {
                      "should": [
                        {
                          "term": {
                            "entities.id": "27374"
                          }
                        }
                      ]
                    }
                  }
                }
              },
              "aggs": {
                "wmetric": {
                  "sum": {
                    "field": "entities.weighted_metric"
                  }
                }
              }
            },
            "w_sort": {
              "bucket_sort": {
                "sort": [
                  {
                    "weightedmetric.wmetric": {
                      "order": "desc"
                    }
                  }
                ],
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

我有这个错误:

{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "org.elasticsearch.search.aggregations.bucket.nested.InternalNested cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"
    }
  },
  "status": 503
}

如果我不尝试订购水桶,一切正常。有人可以帮我解决这个问题吗?我需要按 weighted_metric 订购桶。谢谢

标签: elasticsearchnested

解决方案


推荐阅读