While creating index one can use the:
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();
However how can I setup the index type
, and the id
of this one source object with only one title
field?
EDIT
When changed as recommended to add two documents I keep getting the IndexAlreadyExistsException
error:
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();
It seems that the prepareCreate
and prepareIndex
both create indexes. But not allow to setup all required preferences meaning:
How this can be done?
From the code you have given, it appears you are trying to use the client admin API for creating indexes to try and create an index AND index a document at the same time. The admin().indices() API is for administrating indexes as a whole, not for indexing, creating, deleting, etc documents into/from indexes.
You can create your index using your code, ripping out the setSource portion.
In order to actually index a document, see here: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html
Specifically, this chunk of code:
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
Arguments 1 and 2 of prepareIndex() are the index name and document type respectively, while the optional 3rd argument is for supplying an ID, which is exactly what you are looking for.
How I would do it is this:
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();