如何在elasticsearch 2.0中设置_id

gil*_*lyb 9 elasticsearch elasticsearch-2.0

由于结构path上的_id一个特定的映射的字段被弃用(如文档中注意到这里),

如何_id在elasticsearch 2.0中为特定文档设置字段?

(在我的具体用例中,我想用我自己的id索引所有文档.我知道它们都是唯一的)

Val*_*Val 18

_id 被弃用只是意味着您必须明确指定ID,并且ES不会让您第一次解析文档只是为了检索您指定为id字段的字段.

因此,只要您明确指定id,所有当前索引文档的方法仍然有效:

curl -XPUT localhost:9200/index/type/your_id -d '{"field1": "value1"}'
                                        ^
                                        |
                                 your id goes here
Run Code Online (Sandbox Code Playgroud)

或者在批量查询中

curl -XPOST localhost:9200/_bulk -d '
{"index": {"_index": "index", "_type": "type", "_id": "your_id"}}
{"field1": "value1"}                                     ^
'                                                        |
                                                 your id goes here
Run Code Online (Sandbox Code Playgroud)