sarah w
sarah w

Reputation: 3475

how to retrive nested document and arrays values in elasticsearch

I have a following document

{
  "_index" : "Testdb",
  "_type" : "artWork",
  "_id" : "0",
  "_version" : 1,
  "found" : true,
  "_source":{"uuid":0,"ArtShare":{"TotalArtShares":0,"pricePerShare":0,"ArtworkUuid":12,"AvailableShares":0,"SoldShares":0},"StatusHistoryList":[{"ArtWorkDate":"2015-08-26T13:20:17.725+05:00","ArtworkStatus":"ACTIVE"}]}
}

i want to access/retrieve the value of ArtShare and its attributes and values of array StatusHistoryList

i am doing like this

val get=client.prepareGet("Testdb","artWork",Id.toString())
        .setOperationThreaded(false)
        .setFields("uuid","ArtShare","StatusHistoryList"
            )
        .execute()
        .actionGet()
        if(get.isExists())
        {
        uuid=get.getField("uuid").getValue.toString().toInt
       //how to fetch `artShare` whole nested document and array elements `StatusHistoryListof`  
     }

UPDATE if i do this

 val get=client.prepareGet("Testdb","artWork",Id.toString())
            .setOperationThreaded(false)
            .setFields("uuid","ArtShare","StatusHistoryList"
                ,"_source","ArtShare.TotalArtShares")
            .execute()
            .actionGet()
            if(get.isExists())
            {
            uuid=get.getField("uuid").getValue.toString().toInt
           var totalShares= get.getField("ArtShare.TotalArtShares").getValue.toString().toInt 
         }

then following exception thrown

org.elasticsearch.ElasticsearchIllegalArgumentException: field [ArtShare] isn't a leaf field
    at org.elasticsearch.index.get.ShardGetService.innerGetLoadFromStoredFields(ShardGetService.java:368)
    at org.elasticsearch.index.get.ShardGetService.innerGet(ShardGetService.java:210)
    at org.elasticsearch.index.get.ShardGetService.get(ShardGetService.java:104)
    at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:104)
    at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:44)
    at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:297)
    at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:280)
    at org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.doRun(MessageChannelHandler.java:279)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:36)

please guide me how to fetch these values

Upvotes: 0

Views: 1114

Answers (1)

deepakchauhan
deepakchauhan

Reputation: 290

Yeah Actually the problem is that you have mentioned both "ArtShare" and "ArtShare.TotalArtShares" in the fields array. So it throws exception as you have already retrieved complete ArtShare object.

So please mention the fields that you want, If you want specified nested values then no need to access complete parent object.

Try this:

val get=client.prepareGet("Testdb","artWork",Id.toString())
        .setOperationThreaded(false)
        .setFields("uuid","StatusHistoryList",
            "ArtShare.TotalArtShares")
        .execute()
        .actionGet()
        if(get.isExists())
        {
        uuid=get.getField("uuid").getValue.toString().toInt
       var totalShares= get.getField("ArtShare.TotalArtShares" 
     }

And if you want complete "ArtShare" object then simply write :

val get=client.prepareGet("Testdb","artWork",Id.toString())
    .setOperationThreaded(false)
    .setFields("uuid","ArtShare","StatusHistoryList"
        )
    .execute()
    .actionGet()
    if(get.isExists())
    {
    uuid=get.getField("uuid").getValue.toString().toInt
   //how to fetch `artShare` whole nested document and array elements `StatusHistoryListof`  
 }

Upvotes: 2

Related Questions