Hi -
I am using TransportClient to connect to 3 nodes on my cluster. All nodes
are identical to each other.
Using Java API -
-
How do I create a new Index with a given type?
-
Once I create the index, I need to make sure that, it is up to date by
keeping with the transactions [CRUD]. I am using the following snippet to
add/update the documents into the index.bulkRequest.add(client.prepareIndex("supplier", type, id) .setSource(jsonBuilder() .startObject() .field("name", name) .field("revision", revision) .field("description", description) .field("current", current) .field("kindof", kindof) .endObject() ) );
-
Using the lines below to delete documents when required
- bulkRequest.add(client.prepareDelete("supplier", type, id));
-
Once in a day, I need to make sure that the index is in sync with my
data in the database. To do this verification, I am planning to get all the
records from the db, and check whether they are available in the index.
Code snippet below.. -
SearchRequestBuilder searchBuilder =
client.prepareSearch("supplier").setTypes(type);
QueryBuilder qb = QueryBuilders.matchAllQuery();-
SearchResponse scrollResp =
searchBuilder.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(100))
.setQuery(qb)
.setSize(50).execute().actionGet();
while (true) {
scrollResp =
client.prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(100))
.execute()
.actionGet();for (SearchHit hit : scrollResp.getHits()) { list.add(hit.id()); names.add(hit.sourceAsMap().get("name")); } //Break condition: No hits are returned if (scrollResp.hits().hits().length == 0) { break; } }
-
If there is mismatch, I need to rebuild the index from scratch. To
do that I am currently deleting the mapping "type" from the "supplier"
index. -
DeleteMappingRequest req = new DeleteMappingRequest("supplier");
req.type(type);
client.admin().indices().deleteMapping(req).actionGet();
-
-
Now I need to rebuild the index.. with type=Type above. How do I do
that?
Am I doing it the right way? Would there be any issues with my design? Is
deleting the mapping and creating the index with the deleted mapping again
causes any trouble?
Thank you,
Mxims
--