如何在 Elasticsearch 中用 "Others" 替换所有 empty/blank 值?

How to substitute all empty/blank values by "Others" in Elasticsearch?

创建饼图时,我想用 "Others" 替换字段 myfield(即等于 "")的所有空值。我如何在 Kibana 中执行此操作?

如果在 Kibana 中做不到,那么我如何使用 Elasticsearch 来做。

更新:

我执行了这个查询,它没有给我任何错误:

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": []
}

但是当我检查 myfield 的值时,我再次得到值 "",而不是 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"
          }
        }
      }
    }
}

据我所知,这不能在 Kibana 端完成。您可以在 Elasticsearch 索引中使用 "Others" 更新所有空字段文档。

要在Elasticsearch端更新,可以使用Update By Query API。以下是更新 code/query.

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

您可以在 Kibana 中尝试这种无痛脚本,它会在运行时为您的索引创建一个新的字符串类型脚本化字段。

Select:管理 -> name_of_your_index -> 脚本字段 -> 添加脚本字段。

下一步:为脚本命名并将其类型设置为"string"。

然后:

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

然后在您的可视化中使用这个新字段。