无法运行elasticsearch嵌套聚合查询

nhjlsmyf  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(479)

我正在尝试创建一个查询,该查询聚合了3个不同字段的总和,还匹配了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
}

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

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

我还尝试了ElasticSearch文档中的示例和下面val中的示例。他们都给出了相同的错误。

nbewdwxp

nbewdwxp1#

这个 sum 聚合是不能有子聚合的度量聚合。。。所以你不能做sum->sum->sum。
如果你需要3个不同的总和,你可以这样做:

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

相关问题