Elasticsearch Date字段的默认值 [英] Default value of Elasticsearch Date Field

查看:838
本文介绍了Elasticsearch Date字段的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法在ES中创建动态映射:

I'm creating a dynamic mapping in ES using:

{
  "template": "infobox*",
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "string_fields": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": {
              "type": "string",
              "index": "analyzed",
              "analyzer": "my_completion_analyzer",
              "fielddata": {
                "format": "disabled"
              },
              "fields": {
                "raw": {
                  "type": "string",
                  "index": "not_analyzed",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}

因此,每当我索引具有日期的文档时, d( birthYear ),它将自动创建一个日期为 的字段( birthYear )。 c $ c>类型。因此,每当我没有 birthYear 时,我都会发送一个空字符串'',这将引发异常 mapper_parsing_exception,无法解析[birthYear]

So, whenever I index a document having date field (birthYear), it automatically creates a field (birthYear) with date type. So, whenever I don't have a birthYear, I send an empty string '', which then raises an exception mapper_parsing_exception, failed to parse [birthYear].

有什么办法可以解决这个问题?我可以分配默认值吗?

Is there any way I can handle this? Can I assign a default value?

推荐答案

您可以添加 ignore_malformed:true 所有 date 字段,或在全局设置:

You either add ignore_malformed: true to all date fields or you set this globally:

date 字段

only date fields:

  "dynamic_templates": [
    {
      "string_fields": {
        "match": "*",
        "match_mapping_type": "string",
        "mapping": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "whitespace",
          "fielddata": {
            "format": "disabled"
          },
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed",
              "ignore_above": 256
            }
          }
        }
      }
    },
    {
      "date_fields": {
        "match": "*",
        "match_mapping_type": "date",
        "mapping": {
          "type": "date",
          "ignore_malformed": true
        }
      }
    }
  ]

全局设置

{
  "settings": {
    "index.mapping.ignore_malformed": true
  }, 
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "string_fields": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": {
              "type": "string",
              "index": "analyzed",
              "analyzer": "whitespace",
              "fielddata": {
                "format": "disabled"
              },
              "fields": {
                "raw": {
                  "type": "string",
                  "index": "not_analyzed",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}

这篇关于Elasticsearch Date字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆