簡體   English   中英

Elasticsearch 如何計算索引中所有文檔中多個字段的特定值?

[英]Elasticsearch how to count specific values from multiple fields across all documents in index?

我的 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 文檔中找到答案。 我不知道如何通過多個字段“垂直”搜索文檔並對選定字段進行聚合。

編輯

我只想為每個車手計算等於 1 的result字段。例如,如果車手在摩納哥獲勝( Monaco result =1),查詢/腳本將增加他的獲勝計數器。

示例響應

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

因此,鑒於“獲勝”的定義是<Driver nationality> result == 1,您可以使用此查詢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM