High-Level Rest Client | Response Issue

Ciao,

I am using High-level rest client to connect with Elasticsearch service and I am successfully able to insert the data in Elasticsearch.

When I am inserting the data for the first time, it is showing 'created' in logs, but when I receive the IndexResponse, it says 'updated', instead of 'created'.

Here is the block of code:

case class User(id: String, name: String, age: Int)


def insert(doc: User): Boolean = {
    val request = new IndexRequest(index_name, type_name, doc.id)
    val jsonString = Json.stringify(Json.toJson(doc))
    request.source(jsonString, XContentType.JSON)
    val indexResponse = client.index(request)

    LOGGER.info("Received Index Response" + indexResponse)
    LOGGER.info("Index Response Result -- " + indexResponse.getResult)

    if (indexResponse.getResult.equals(DocWriteResponse.Result.CREATED)) {
      LOGGER.info("User data inserted!")
      true
    } else if (indexResponse.getResult.equals(DocWriteResponse.Result.UPDATED)) {
      LOGGER.info(s"User for id ${doc.id} already exists, hence it's details are updated!")
      true
    } else {
      LOGGER.info("User data not inserted!")
      false
    }
  }  

Here are the Logs for the same:

12:46:06.963 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 >> "{"id":"10","name":"User-10","age":18}"
12:46:06.964 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalIODispatch - http-outgoing-0 [ACTIVE] Request ready
12:46:06.964 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 127.0.0.1:55826<->127.0.0.1:9200[ACTIVE][r:w]: Event cleared [w]
12:46:07.061 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 127.0.0.1:55826<->127.0.0.1:9200[ACTIVE][r:r]: 282 bytes read
12:46:07.062 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 201 Created[\r][\n]"
12:46:07.062 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 << "Location: /user-index/user/10[\r][\n]"
12:46:07.062 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 << "content-type: application/json; charset=UTF-8[\r][\n]"
12:46:07.062 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 << "content-length: 159[\r][\n]"
12:46:07.062 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
12:46:07.062 [I/O dispatcher 1] DEBUG org.apache.http.wire - http-outgoing-0 << "{"_index":"user-index","_type":"user","_id":"10","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":2}"
12:46:07.076 [I/O dispatcher 1] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 201 Created
12:46:07.076 [I/O dispatcher 1] DEBUG org.apache.http.headers - http-outgoing-0 << Location: /user-index/user/10
12:46:07.076 [I/O dispatcher 1] DEBUG org.apache.http.headers - http-outgoing-0 << content-type: application/json; charset=UTF-8
12:46:07.076 [I/O dispatcher 1] DEBUG org.apache.http.headers - http-outgoing-0 << content-length: 159
12:46:07.076 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalIODispatch - http-outgoing-0 [ACTIVE(159)] Response received
12:46:07.077 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.MainClientExec - [exchange: 1] Response received HTTP/1.1 201 Created
12:46:07.088 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalIODispatch - http-outgoing-0 [ACTIVE(159)] Input ready
12:46:07.088 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.MainClientExec - [exchange: 1] Consume content
12:46:07.091 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalHttpAsyncClient - [exchange: 1] Connection can be kept alive indefinitely
12:46:07.095 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.MainClientExec - [exchange: 1] Response processed
12:46:07.095 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalHttpAsyncClient - [exchange: 1] releasing connection
12:46:07.096 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 127.0.0.1:55826<->127.0.0.1:9200[ACTIVE][r:r]: Remove attribute http.nio.exchange-handler
12:46:07.096 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Releasing connection: [id: http-outgoing-0][route: {}->http://localhost:9200][total kept alive: 0; route allocated: 1 of 5; total allocated: 1 of 10]
12:46:07.097 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection [id: http-outgoing-0][route: {}->http://localhost:9200] can be kept alive indefinitely
12:46:07.097 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 127.0.0.1:55826<->127.0.0.1:9200[ACTIVE][r:r]: Set timeout 0
12:46:07.098 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection released: [id: http-outgoing-0][route: {}->http://localhost:9200][total kept alive: 1; route allocated: 1 of 5; total allocated: 1 of 10]
12:46:07.106 [I/O dispatcher 1] DEBUG org.elasticsearch.client.RestClient - request [PUT http://localhost:9200/user-index/user/10?timeout=1m] returned [HTTP/1.1 201 Created]
12:46:07.107 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalIODispatch - http-outgoing-0 [ACTIVE] [content length: 159; pos: 159; completed: true]
12:46:07.271 [run-main-0] INFO com.knoldus.elasticsearch.service.EsOperations - Received Index ResponseIndexResponse[index=user-index,type=user,id=10,version=1,result=updated,shards={"total":2,"successful":1,"failed":0}]
12:46:07.272 [run-main-0] INFO com.knoldus.elasticsearch.service.EsOperations - Index Response Result -- UPDATED
12:46:07.272 [run-main-0] INFO com.knoldus.elasticsearch.service.EsOperations - User for id 10 already exists, hence it's details are updated!

Same is happening with Delete operation. When deleting an existing user, user is deleted from elasticsearch (verified this by visualising in Kibana), but it gives 'not_found' at last in DeleteResponse, instead of 'deleted'.

Can you help me out with this?

Hi @Divya_Dua,
I tried to reproduce this with no luck. Which client version are you on, and which Elasticsearch is it sending requests to?

Thanks
Luca

1 Like

Hey @javanna,

That seems to be Elasticsearch version issue. I have changed it and now it is working fine.

Thank You.

Good to know @Divya_Dua, would you mind letting us know which versions you were on, out of curiosity?

Thanks
Luca

Hi @javanna,

Earlier I was using Elasticsearch 6.0.1 on local and was using dependency version 5.6.0 for both Elasticsearch and high-level rest client in the project.

But now, I have switched to the latest version 6.2.4 for both dependencies and local Elasticsearch.

Thanks,
Divya Dua

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