【问题标题】:Is there a way to know datatype of key in Elasticsearch?有没有办法知道 Elasticsearch 中键的数据类型?
【发布时间】:2020-10-15 11:48:09
【问题描述】:

我遇到了诸如密钥数据类型改变之类的问题。在创建索引时,我有嵌套的数据类型,但由于某种原因,它被更改为对象。我通过无痛脚本进行 CRUD 操作,但这似乎很好。

弹性版本 7.3.0

初始模板:

"settings": {
  "number_of_shards": 1,
},
"mappings" : {
  "properties": {
    "deleted_at": { "type": "date" },
    "updated_at": { "type": "date" },
    "id": { "type": "integer" },
    "user_id": { "type": "integer" },

    ... some more keys

    "user_tags": {
      "type": "nested"
    },
    "user_files": {
      "type": "nested"
    },
  }
}

一些批量插入/更新后的映射

"mappings" : {
  "properties": {
    "deleted_at": { "type": "date" },
    "updated_at": { "type": "date" },
    "id": { "type": "integer" },
    "user_id": { "type": "integer" },
    "user_tags": {
      "properties": {
        ...some properties
      }
    },
    "user_files": {
      "properties": {
        ...some properties
      }
    },
  }
}

我必须重新索引才能解决此问题,但这种情况经常发生。还有有什么方法可以知道键的数据类型是嵌套的还是对象的?

提前致谢。

【问题讨论】:

    标签: elasticsearch querydsl elasticsearch-painless


    【解决方案1】:

    通过设置"dynamic": "strict",您的映射不会改变,不合适的文档也不会被插入。要解决此问题,您需要定义要位于嵌套字段内的所有字段。例如:

    {
    "user_tags": {
                "type": "nested",
                "properties": {
                  "code": {
                    "type": "keyword",
                    "store": true
                  },
                  "score": {
                    "type": "float",
                    "store": true
                  }
                  
                }
              }
    }
    

    如果你只想存储一个列表,你可以使用如下映射:

    {
        "user_tags": {
                "type": "keyword",
                "store": true
              }
    }
    

    在第二个映射中,您可以使用此结果存储 user_tags ["tag1", "tag2", ...]

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2010-12-24
      • 2016-11-09
      相关资源
      最近更新 更多