【问题标题】:Elasticsearch how to count specific values from multiple fields across all documents in index?Elasticsearch如何计算索引中所有文档的多个字段中的特定值?
【发布时间】:2021-06-08 14:22:16
【问题描述】:

我的 ES 指数中有 WRC 排名。每个驱动程序都有自己的文档。

此索引的映射:

`{
  "properties" : {
    "Argentina pts" : {
      "type" : "long"
    },
    "Argentina result" : {
      "type" : "long"
    },
    "Chile pts" : {
      "type" : "long"
    },
    "Chile result" : {
      "type" : "long"
    },
    "Driver" : {
      "type" : "keyword"
    }
    "Nationality" : {
      "type" : "keyword"
    }
    "Team" : {
      "type" : "keyword"
    },
    "Total" : {
      "type" : "long"
    },
    "Position" : {
      "type" : "long"
    }`

示例文档:

"_source" : {
      "Argentina pts" : 30,
      "Argentina result" : 1,
      "Chile pts" : 30,
      "Chile result" : 1,
      "Driver" : "O. TANAK",
      "Nationality" : "Estonia",
      "Team" : "Toyota",
      "Total" : 263,
      "Position" : 1,

    }

问题

如何根据result 字段计算车手获胜次数?我尝试了不同的查询和/或聚合,但我得到的最接近的结果是赛季至少赢了一场的车手(我为此使用了multi_match)。我没有在 Stack 或 Elastic 文档中找到答案。我不知道如何通过多个字段“垂直”搜索文档并对选定字段进行聚合。

编辑

我只想计算每个车手的 result 等于 1 的字段。例如,如果车手在摩纳哥获胜 (Monaco result=1),查询/脚本将增加他的获胜计数器。

示例响应

{
"Driver" : "a",
"Win count": 3
},
{
"Driver" : "b",
"Win count": 2
},
{
"Driver" : "c",
"Win count": 1
}

【问题讨论】:

  • 你能给我一个你想要什么的例子吗?只是为了清楚。
  • @nimeresam 添加了一个示例
  • 什么是胜利?只是 position 字段等于 1?或者如果Nationality='Chile',你是否需要Chile result=1?
  • @RonSerruya 对不起,我应该提一下。你的意思是对的。例如,Win 是 Chile result = 1,仅此而已。

标签: elasticsearch


【解决方案1】:

因此,鉴于“胜利”的定义是 <Driver nationality> result == 1,您可以使用此查询

{
  "query": {
    "script": {
      "script": "def nat = doc['Nationality'].value; return doc[nat + ' result'].value == 1"
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2018-08-26
    • 2021-07-25
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多