Java High level Rest Client(5.6), getting succesful shard info count


(Neha Goswami) #1

Hi i am working on migration from Transport client to restClient:

M stuck here:

using transport client this was done as:

XContentBuilder builder = jsonBuilder().startObject().field("ProcessName", processName).field(StrConstants.Status, status).endObject();

IndexRequest indexRequest = new IndexRequest(index, type).id(this.statId).source(builder);
UpdateResponse resp = this.client.update(new UpdateRequest(index, type,id).doc(builder).upsert(indexRequest)).actionGet(TimeValue.timeValueSeconds(this.timeoutSecs));

& now using RestClient we can do it AS:
HttpEntity entity = new NStringEntity(
"{\n" +
" "ProcessName" : ""+processName+"",\n" +
" "Status" : ""+status+"",\n" +
" "StatLastModified" : ""+new Date()+""\n" +
"}", ContentType.APPLICATION_JSON);

			ResponseListener responseListener = new ResponseListener() {
			    @Override
			    public void onSuccess(Response response){
			    	try {
						logger.info("   Written Status to ES >>>  " + EntityUtils.toString(response.getEntity()));
					} catch (ParseException | IOException e) {
						logger.error("Exception occurred "+e);
					}
			    }

			    @Override
			    public void onFailure(Exception exception) {
			    	logger.info(" NOT Written to ES...."+exception);
			    }
			};

			Map<String, String> params = Collections.emptyMap();
			restClient.performRequestAsync("PUT", getEndPoint(index, type, id), params, entity, responseListener);

Now after this in code. we had a retry logic which needed the succesful shard info :
int success = resp.getShardInfo().getSuccessful(); ------> Now to do this using RestClient Response
In rest client response we only get json response using :

EntityUtils.toString(response.getEntity())

How can i get the succesful shard info count here??


(Imteyaz Ahmad) #2

This can help you...

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/java-rest-high-document-index.html


(Neha Goswami) #3

I had gone through this link, confusioin here is that link says to get output from IndexResponse like below:

Synchronous Execution

IndexResponse indexResponse = client.index(request);

But, indexResponse is fetched using client object...this is not restClient Object right??

When we are migrating to restClient, we should move everything to restClient right???

From restClient way of getting index response is : :
Response response= restClient.performRequestAsync("PUT", getEndPoint(index, type, id), params, entity, responseListener);

Now from this response i am not able to fetch successful shard info:

Pls guide me if im on right direction


(Imteyaz Ahmad) #4
      @Override
      public void onResponse(IndexResponse indexResponse) {
        // This is where you can get the Shard Infos
       ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
        System.out.println(shardInfo.getSuccessful());
      }

      @Override
      public void onFailure(Exception e) {

      }
    };

(Neha Goswami) #5

Thanks.


(Imteyaz Ahmad) #6

@Neha_goswami did it solve the problem?? if yes please mark it as solution.


(Neha Goswami) #7

Yes actually i was not getting the correct high level rest client instance.
Updating correct dependency in pom.xml solved my problem


(system) #8

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.