在创建索引时,可以使用:
CreateIndexResponse createIndexRequestBuilder = client().admin().indices()
.prepareCreate(INDEX_NAME)
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("title", "My Title 1")
.endObject()
)
.setSettings(
Settings.settingsBuilder()
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 2)
)
.execute()
.actionGet();
但是,如何设置索引type
,以及id
只有一个title
字段的这个源对象的?
编辑
当按照建议进行更改以添加两个文档时,我不断收到IndexAlreadyExistsException
错误消息:
CreateIndexResponse createIndexRequestBuilder = client().admin().indices()
.prepareCreate(INDEX_NAME)
.setSettings(
Settings.settingsBuilder()
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 2)
)
.execute()
.actionGet();
IndexResponse response1 = client().prepareIndex(INDEX_NAME, BOOK_TYPE_NAME, "id1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("title", "Clean COde")
.endObject()
)
.setRouting("route1")
.get();
IndexResponse response2 = client().prepareIndex(INDEX_NAME, BOOK_TYPE_NAME, "id2")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("title", "Learn Scala")
.endObject()
)
.setRouting("route2")
.get();
似乎prepareCreate
andprepareIndex
都创建了索引。但不允许设置所有必需的首选项含义:
- 分片NR
- 副本编号
- 索引名称
- 索引类型
- 新文档 ID。 -
如何做到这一点?