2017-10-21 72 views
1

當我創建餅圖時,我想用"Others"替換myfield(即等於"")字段的所有空值。我怎麼能在Kibana做到這一點?如何用Elasticsearch中的「其他」替換所有空/空值?

如果在Kibana中無法做到,那我該如何使用Elasticsearch做到這一點。

enter image description here

UPDATE:

我執行該查詢,並沒有給我任何錯誤:

GET myindex/entry/_update_by_query 
{ 
    "query":{ 
    "term": { 
     "myfield.keyword": { 
     "value": "" 
     } 
    } 
    }, 
    "script":{ 
    "inline": "ctx._source.myfield = 'Other'", 
    "lang": "painless" 
    } 
} 

我得到這樣的輸出:

{ 
    "took": 5, 
    "timed_out": false, 
    "total": 0, 
    "updated": 0, 
    "deleted": 0, 
    "batches": 0, 
    "version_conflicts": 0, 
    "noops": 0, 
    "retries": { 
    "bulk": 0, 
    "search": 0 
    }, 
    "throttled_millis": 0, 
    "requests_per_second": -1, 
    "throttled_until_millis": 0, 
    "failures": [] 
} 

但是當我檢查的值,我再次獲得值"",而不是Other

GET myindex/_search? 
{ 
"size":0, 
    "aggs": { 
    "months": { 
    "terms" : { 
     "field": "myfield" 
    } 
    } 
    } 
} 

這是我的索引映射:

PUT /myindex 
{ 
    "mappings": { 
     "entry": { 
     "_all": { 
     "enabled": false 
     }, 
     "properties": { 
      "Id": { 
      "type":"keyword" 
      }, 
      "Country": { 
      "type":"keyword" 
      }, 
      "myfield": { 
      "type":"keyword" 
      }, 
      "Year": { 
      "type":"integer" 
      }, 
      "Counter": { 
      "type":"integer" 
      } 
     } 
     } 
    } 
} 

回答

1

據我所知,這不能在Kibana年底完成。您可以使用Elasticsearch索引中的「其他」來更新所有空白字段文檔。

要更新Elasticsearch結束,您可以使用Update By Query API。以下是更新代碼/查詢。

GET myindex/entry/_update_by_query 
{ 
    "query":{ 
    "term": { 
     "myfield": { 
     "value": "" 
     } 
    } 
    }, 
    "script":{ 
    "inline": "ctx._source.myfield = 'Other'", 
    "lang": "painless" 
    } 
} 
+0

謝謝。什麼是'ctx'和'_source'? – Dinosaurius

+0

我得到'parse_exception':'reason「:」期望的[inline],[file]或[stored]字段之一,但沒有找到「 – Dinosaurius

+0

您正在使用哪個ES版本? –

0

您可以在Kibana中嘗試這種無痛腳本,它將在運行時爲您的索引創建一個新的字符串類型腳本字段。

選擇:管理 - > name_of_your_index - >腳本字段 - >添加腳本字段。

下一步:爲腳本命名並將其類型設置爲「字符串」。

然後:

def source = doc['name_of_the_field'].value; 
if (source != null) { return source; } 
else { return "Other";} 

然後在你的圖表中使用這個新的領域。