4

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

{
  "tweet": {
    "_id": {
      "path": "post_id"
    }
  }
}

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

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

client.prepareIndex("index","type").setSource(MapA).execute().actionGet();

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

4

1 回答 1

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();

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

{
    "tweet" : {
        "_id" : {
            "path" : "post_id"
        }
    }
}

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

于 2014-07-26T03:35:09.637 回答