【问题标题】:handle empty date in elasticsearch dynamically动态处理elasticsearch中的空日期
【发布时间】:2020-01-20 03:02:52
【问题描述】:

我有以下动态映射模板。

PUT my_index
{
  "mappings": {
   "dynamic_templates": [
     {
       "objects": {
         "match_mapping_type": "object",
         "mapping": {
           "type": "nested"
         }
       }
     }
   ],
   "dynamic_date_formats": ["yyyy-MM-dd" , "yyyy-MM-dd HH:mm:ss"]
 }
}

唯一的问题是当我有空日期时它会抛出错误。我只想忽略空日期。我的数据有多个日期字段,因此不想为每个日期字段进行映射。

以下是我得到的错误:

org.elasticsearch.hadoop.rest.EsHadoopRemoteException:非法参数异常:不同类型的映射器 [pb_bureau.applications.accounts.dateclosed],current_type [文本],merged_type [日期] {"index":{"_id":"02ade9b5-1ca5-4006-ab06-9c96439e7d02"}}

在我们插入的日期以下:空白字段是日期的空值

select date1, date2 from cbl_application_credit_report_account ;
    2014-11-14
    2018-03-31
2012-07-27  2012-07-23
    2015-11-30
2017-08-04  2016-05-13

下面是我正在应用的映射:

PUT my_index
{
  "mappings": {
   "dynamic_templates": [
     {
       "objects": {
         "match_mapping_type": "object",
         "mapping": {
           "type": "nested"
         }
       }
     },
     {
        "dates_ignore_malformed": {
          "path_match": "*",
          "match_mapping_type": "date",
          "mapping": {
            "format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss",
            "ignore_malformed": true
          }
        }
      }
   ],
   "dynamic_date_formats": ["yyyy-MM-dd" , "yyyy-MM-dd HH:mm:ss"]
 }
}

动态映射中是否有任何方法可以忽略空日期?

【问题讨论】:

  • 您的日期字段是否共享一致的命名?即它们是否都以Date_date 等结尾?

标签: azure elasticsearch kibana


【解决方案1】:

映射:

PUT my_index4
{
  "mappings": {
    "dynamic_templates": [
      {
        "objects": {
          "match_mapping_type": "object",
          "mapping": {
            "type": "nested"
          }
        }
      },
      {
        "dates_ignore_malformed": {
          "path_match": "*",
          "match_mapping_type": "date",
          "mapping": {
            "format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss" ---> date format on which to be applied ,
            "ignore_malformed": true ---> Ignores if field s malformed
          }
        }
      }
    ],
    "dynamic_date_formats": [
      "yyyy-MM-dd",
      "yyyy-MM-dd HH:mm:ss"
    ]
  }
}

数据:

POST my_index4/_doc
{
  "date":"2019-01-01 04:30:22",
   "Id":1
}

POST my_index4/_doc
{
  "name":2,
  "date":"2019-01-01"
}

POST my_index4/_doc
{
  "name":2,
  "date":""
}

查询:

GET my_index4/_search

结果:

"hits" : [
      {
        "_index" : "my_index4",
        "_type" : "_doc",
        "_id" : "NT5XSG0BbzgYofLxTDZ_",
        "_score" : 1.0,
        "_source" : {
          "date" : "2019-01-01 04:30:22",
          "Id" : 1
        }
      },
      {
        "_index" : "my_index4",
        "_type" : "_doc",
        "_id" : "Nj5XSG0BbzgYofLxUTaT",
        "_score" : 1.0,
        "_source" : {
          "name" : 2,
          "date" : "2019-01-01"
        }
      },
      {
        "_index" : "my_index4",
        "_type" : "_doc",
        "_id" : "Nz5XSG0BbzgYofLxWDYi",
        "_score" : 1.0,
        "_ignored" : [
          "date"
        ],
        "_source" : {
          "name" : 2,
          "date" : ""
        }
      }
    ]

【讨论】:

  • 感谢您的回复。我尝试了同样的事情。我遇到了同样的错误。因为问题是如果我在第一行得到空日期并且日期在第二行。它抛出一个错误。我已经用错误消息更新了我的问题。
  • 我无法复制它。 "mapping": { "format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss" 是否与 dynamic_date_formats 相同,并且 ignore_malformed 设置为 true。您能否检查新映射并发布您尝试输入的数据
  • 我已经更新了我的问题供您参考。数据就像某处日期是空的某处不是。可能是我错了,但 es 是根据列的第一个值来决定数据类型。
  • 我用你在数据之下的数据尝试了你发布的映射。它没有给我错误。您能否使用 GET my_index6/_mapping 检查映射并验证是否应用了最近的映射。发布 my_index6/_doc { "date":"2014-11-14", "date1":"2014-11-14" } 发布 my_index6/_doc { "date":"2018-03-31" } 发布 my_index6/_doc { "Id":3 }
  • 在输入第一条记录时创建字段映射,并进一步使用相同的记录。如果第一条记录为空,您将不得不按照@Val 所说的进行。字段名称中必须有模式(一些常见的文本),并且您需要在动态模板中使用匹配参数(使用模式来匹配字段名称)
猜你喜欢
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多