Elasticsearch- 将类型和 ID 添加到 CreateIndexResponse
Elasticsearch- Add type and id to CreateIndexResponse
创建索引时可以使用:
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
,以及只有一个 title
字段的这一源对象的 id
?
编辑
当按照建议更改为添加两个文档时,我不断收到 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
和 prepareIndex
都创建了索引。但不允许设置所有必需的首选项,意思是:
- 分片编号
- 副本编号
- 索引名称
- 索引类型
- 新文档 ID。
-
如何做到这一点?
从您提供的代码来看,您似乎正在尝试使用客户端管理员 API 创建索引,以尝试同时创建索引和索引文档。 admin().indices() API 用于作为一个整体管理索引,而不是用于索引、创建、删除等文档 into/from 索引。
您可以使用代码创建索引,删除 setSource 部分。
为了实际索引文档,请参阅此处:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html
具体来说,这段代码:
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
prepareIndex() 的参数 1 和 2 分别是索引名称和文档类型,而可选的第三个参数用于提供 ID,这正是您要查找的内容。
我会怎么做:
IndicesExistsResponse res = client.admin().indices().prepareExists("twitter").get();
if (!res.isExists()) {
System.out.println("index doesn't exist, creating");
CreateIndexResponse createIndexRequestBuilder = client.admin().indices()
.prepareCreate("twitter")
.setSettings(
Settings.settingsBuilder()
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 2)
)
.get()
}
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
创建索引时可以使用:
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
,以及只有一个 title
字段的这一源对象的 id
?
编辑
当按照建议更改为添加两个文档时,我不断收到 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
和 prepareIndex
都创建了索引。但不允许设置所有必需的首选项,意思是:
- 分片编号
- 副本编号
- 索引名称
- 索引类型
- 新文档 ID。 -
如何做到这一点?
从您提供的代码来看,您似乎正在尝试使用客户端管理员 API 创建索引,以尝试同时创建索引和索引文档。 admin().indices() API 用于作为一个整体管理索引,而不是用于索引、创建、删除等文档 into/from 索引。
您可以使用代码创建索引,删除 setSource 部分。
为了实际索引文档,请参阅此处:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html
具体来说,这段代码:
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
prepareIndex() 的参数 1 和 2 分别是索引名称和文档类型,而可选的第三个参数用于提供 ID,这正是您要查找的内容。
我会怎么做:
IndicesExistsResponse res = client.admin().indices().prepareExists("twitter").get();
if (!res.isExists()) {
System.out.println("index doesn't exist, creating");
CreateIndexResponse createIndexRequestBuilder = client.admin().indices()
.prepareCreate("twitter")
.setSettings(
Settings.settingsBuilder()
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 2)
)
.get()
}
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();