ElasticSearch 6.4:未存储的字段

时间:2018-11-29 14:31:35

标签: elasticsearch

在ElasticSearch 6.4中,我有一个数据库,在其中映射了几个字段并指定了几个存储字段。但是,当我运行查询时,这些字段位于_source中,而不是stored_fields中。我在映射中错过了什么吗?

这是一个重现该问题的简单示例:

PUT http://localhost:9200/testdb

然后

PUT http://localhost:9200/testdb/_mapping/_doc

随身

{
    "properties": {
        "field1_indexed": {
            "type": "text"
        },
        "field2_not_indexed": {
            "type": "text",
            "index": false,
            "store": true
        },
        "field3_stored": {
            "type": "text",
            "store": true
        }
    }
}

PUT http://localhost:9200/testdb/_doc/1

随身

{
    "field1_indexed": "field 1",
    "field2_not_indexed": "field 2",
    "field3_stored": "field 3"
}

现在请求http://localhost:9200/testdb/_search?_source=false并将其取回:

{"took":1,"timed_out":false,"_shards": 
{"total":5,"successful":5,"skipped":0,"failed":0},"hits": 
{"total":1,"max_score":1.0,"hits": 
[{"_index":"testdb","_type":"_doc","_id":"1","_score":1.0}]}}

请注意,尽管“ field2_not_indexed”和“ field3_stored”在映射中显式声明store = true,但没有“ stored_fields”元素。如果我不关闭_source,则所有三个字段都将在_source元素中输出。

1 个答案:

答案 0 :(得分:0)

阅读搜索中的文档后,您发现必须明确告知搜索查询要存储的字段。因此,您需要在URL http://localhost:9200/testdb/_search?stored_fields= *或请求正文中指定它。

{
    "stored_fields": ["*"]
}