值为“0”和“1”的 Elasticsearch 映射布尔值

Elasticsearch mapping boolean with value "0" and "1"

ElasticSearch 版本 7.13

索引已经存在,我想用映射重建索引,该字段是一个布尔值。但是当我试图重新索引时,该字段有 "1""0" (字符串)。

如果 field = "1" 设置 true(0 相同,但为假),我如何评估?

我读过 runtime,但不知道它是如何工作的。

我的映射

{
 mappings:{
  "OPTIONS": {
     "type": "nested",
     "properties":{
        "COMBINABLE": {
          "type": "boolean"
        }
     }
  }
 }
}

和文件

{
  "options": [
    {
      "COMBINABLE": "0"
    }
  ]
}

您可以考虑使用 pipeline ingestion 将您的数字转换为布尔值,您可以这样做:

POST _ingest/pipeline/_simulate

{
    "pipeline": {
    "description": "convert to boolean",
    "processors": [
      {
        "script": {
          "source": "def options = ctx.options;def pairs = new ArrayList();for (def pair : options) {def k = false;if (pair[\"COMBINABLE\"] == \"1\" || pair[\"COMBINABLE\"] == 1) {k = true;}pair[\"COMBINABLE\"] = k;}ctx.options = options;"
        }
      }
    ]
  },
    "docs": [
        {
            "_source": {
                "options": [
                    {
                        "COMBINABLE": 1
                    }
                ]
            }
        }
    ]
}

上面的无痛脚本非常简单:

def options = ctx.options;
def pairs = new ArrayList();
for (def pair : options) {
  def k = false;
  if (pair["COMBINABLE"] == "1" || pair["COMBINABLE"] == 1) {
    k = true;
  }
  pair["COMBINABLE"] = k;
}
ctx.options = options;

它简单地遍历你在 options 下的所有选项,然后如果 COMBINABLE1"1",它将转换为 true,否则,它将是 false。您可以将管道设置为默认摄取,see here