【问题标题】:Force string type when indexing unknown field索引未知字段时强制字符串类型
【发布时间】:2017-09-04 15:06:42
【问题描述】:

我正在尝试索引我不知道我将其声明为对象的文档部分的文档。我想要实现的是“告诉”elasticsearch 来索引我在这个对象中作为字符串给出的任何字段,换句话说,任何要映射和存储为字符串类型的整数、长整数、date 字段,例如

假设我们有以下文档要索引

{
  "foo":"bar",
  "custom_object":{
    "a_name":"jim",
    "a_date":"2016-3-31"
  }
}

我确实知道我在custom_object中的字段名称是什么,该文档log的映射如下:

"mappings": {
       'log': {
          'properties': {
              'foo': {
                  'type': 'string',
                  'index': 'not_analyzed'
              },
              'custom_object': {
                  'type': 'object'
              }
       }
 }

我怎么知道我给 custom_object 内的文档的任何内容都将映射为字符串?这主要与 date 值有关,我是否面临这个问题。我不想用 dynamic=falseenabled =false 忽略它(取自documentation)。有什么想法吗?

【问题讨论】:

    标签: java elasticsearch nosql


    【解决方案1】:

    您可以使用dynamic templates 表示名称custom_object 内的所有字段都必须映射为string

    定义映射时,可以如下设置

         "dynamic_templates":[
            {
               "custom_object_template":{
                  "path_match":"custom_object.*",
                  "mapping":{
                     "type": "string"
                  }
               }
            }
    

    【讨论】:

      【解决方案2】:

      当 Elasticsearch 遇到一个新的字符串字段时,它会检查是否 该字符串包含一个可识别的日期,例如 2014-01-01。如果看起来 像日期一样,该字段被添加为日期类型。否则,它被添加 作为类型字符串。

      您可以在 elasticsearch 中自定义dynamic mapping 以满足您的需求。

      可以通过在根对象上将date_detection 设置为false 来关闭日期检测:

      PUT /my_index
      {
          "mappings": {
              "my_type": {
                  "date_detection": false
              }
          }
      }
      

      有了这个映射,string 将永远是string。如果您需要date 字段,则必须手动添加。

      【讨论】:

        猜你喜欢
        • 2015-09-01
        • 1970-01-01
        • 2011-10-06
        • 2013-05-30
        • 1970-01-01
        • 2016-07-22
        • 1970-01-01
        • 2019-05-22
        • 1970-01-01
        相关资源
        最近更新 更多