Elasticsearch Java Api:将字段设置为 _id

use*_*851 4 elasticsearch elasticsearch-java-api

我想问一下唯一字段_id是否由文档中的某个字段分配。我看到 Rest,它可以通过path以下方式实现:

{
  "tweet": {
    "_id": {
      "path": "post_id"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我想用java API来做,有什么办法可以实现吗?

Map<String, Object> MapA= new HashMap<String, Object>();
MapA=MapProcessor(MapA);

client.prepareIndex("index","type").setSource(MapA).execute().actionGet();
Run Code Online (Sandbox Code Playgroud)

如何修改我的代码以分配 Map 中的某些字段成为_id这种类型?

pro*_*mer 5

只需在索引时提供 id,就像这样

Map<String, Object> MapA= new HashMap<String, Object>();
MapA=MapProcessor(MapA);

client.prepareIndex("index","type",MapA.get("post_id")).setSource(MapA).execute().actionGet();
Run Code Online (Sandbox Code Playgroud)

如果您不想这样做,请将其添加为映射。

{
    "tweet" : {
        "_id" : {
            "path" : "post_id"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果将 post_id 字段添加到elasticsearch,那么它也会变成“_id”。