【问题标题】:Unable to run elastic search nested aggregate query无法运行弹性搜索嵌套聚合查询
【发布时间】:2020-11-05 22:57:38
【问题描述】:

我正在尝试创建一个聚合 3 个不同字段的总和并匹配三个不同条件的查询。我不明白错误消息在说什么。

下面的查询给出了这个特定的错误消息:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "Unknown key for a VALUE_NUMBER in [Type].",
        "line": 1,
        "col": 9
      }
    ],
    "type": "parsing_exception",
    "reason": "Unknown key for a VALUE_NUMBER in [Type].",
    "line": 1,
    "col": 9
  }
}

我的查询如下所示:

 {
  "aggs": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "data.entity.productId": "45c29143b3bb4073a9fd325106784ce2"
              }
            },
            {
              "term": {
                "data.entity.locationId": "c5f45ffc4fd94dcb926f96f1d5b9d835"
              }
            },
            {
              "term": {
                "type.keyword": "StockLocationActivityAggregate"
              }
            }
          ]
        }
      }
    },
    "aggs": {
      "directStock": {
        "sum": { "field": "data.entity.inStock" },
        "aggs": {
          "directOutgoing": {
            "sum": { "field": "data.entity.outgoing" },
            "aggs": {
              "directIncoming": { "sum": { "field": "data.entity.incoming" } }
            }
          }
        }
      }
    }
  },
  "size": 0
}

更新 我正在使用以下索引图

{
  "mapping": {
    "_doc": {
      "properties": {
        "active": {
          "type": "boolean"
        },
        "data": {
          "properties": {
            "entity": {
              "properties": {
                "activityDate": {
                  "type": "date"
                },
                "creationDate": {
                  "type": "date"
                },
                "deleted": {
                  "type": "boolean"
                },
                "hash": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "id": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "inStock": {
                  "type": "float"
                },
                "incoming": {
                  "type": "float"
                },
                "locationId": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "modifiedOn": {
                  "type": "date"
                },
                "modifier": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "orderId": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "orderItemId": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "outgoing": {
                  "type": "float"
                },
                "productId": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "reservationDate": {
                  "type": "date"
                },
                "version": {
                  "type": "long"
                }
              }
            },
            "hash": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "modifiedOn": {
              "type": "date"
            },
            "modifier": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "tenantIdentifier": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "version": {
              "type": "long"
            }
          }
        },
        "deleted": {
          "type": "boolean"
        },
        "id": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "tenantId": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "type": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "version": {
          "type": "long"
        }
      }
    }
  }
}

我还尝试了弹性搜索文档中的示例和下面 Val 中的示例。他们都给出了相同的错误。

【问题讨论】:

  • 能否也分享一下您的索引映射?
  • 我已经添加了索引映射。

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

sum 聚合是一个不能有子聚合的度量聚合...所以你不能做 sum -> sum -> sum。

如果您需要 3 个不同的总和,您可以这样做:

{
  ...
  "aggs": {
    "directIncoming": {
      "sum": {
        "field": "data.entity.incoming"
      }
    },
    "directStock": {
      "sum": {
        "field": "data.entity.inStock"
      }
    },
    "directOutgoing": {
      "sum": {
        "field": "data.entity.outgoing"
      }
    }
  }
}

【讨论】:

  • 感谢您的建议,但是使用这个确切的查询(包括我的过滤器)会得到相同的结果。
  • 你用的是哪个版本的ES?
猜你喜欢
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 2015-09-24
  • 2017-06-11
  • 2020-07-26
  • 1970-01-01
相关资源
最近更新 更多