ElasticSearch:存在模板时将文档插入 ElasticSearch 索引的奇怪问题

ElasticSearch: Weird problem in inserting documents to ElasticSearch indices when a template is present

我正在尝试在 ElasticSearch 中进行一些测试。我可以根据需要填充所有内容,但是每当我尝试放置我们项目的默认模板然后插入时,索引中都不会摄取数据(尽管 http 调用是成功的)。

经过检查,我发现即使使用elasticSearch 的默认模板,我也无法插入一个简单的文档。 例如从 ES 的文档中插入模板:

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "host_name": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date",
        "format": "EEE MMM dd HH:mm:ss Z yyyy"
      }
    }
  }
}

然后通过

index = "bark" 中插入文档
PUT http://localhost:9200/bark/_doc/11232 HTTP/1.1
User-Agent: Fiddler
Host: localhost:9200
Content-Length: 21
Content-Type: application/json

{"host_name": "generic_name"}

在索引中添加一个文档,但没有关于 host_name 的数据。 只需将索引名称更改为此模板不适用的内容(例如 index = dark),就会添加一个包含 host_name 数据的文档。 显示要复制的索引数据:

(当 index=bark

{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"bark","_type":"_doc","_id":"11232","_score":1.0}]}}

(当 index=dark

{"took":6,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"dark","_type":"_doc","_id":"11232","_score":1.0,"_source":{"host_name":"generic_name"}}]}}

注意到 _source":{"host_name":"generic_name"} 字段在前者中不存在了吗?

为此可以做什么?如果有人遇到过此问题或知道解决方法,请提供帮助。

您需要从映射中删除它

"_source": {
  "enabled": false
},

此设置的效果是源文档未存储在 _source 字段中。这可能不是你想要的。