简体   繁体   English

如何在Elasticsearch中检索嵌套的文档和数组值

[英]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 我想访问/检索ArtShare的值及其属性和StatusHistoryList数组的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 请指导我如何获取这些值

Yeah Actually the problem is that you have mentioned both "ArtShare" and "ArtShare.TotalArtShares" in the fields array. 是的,实际上问题是您在字段数组中同时提到了“ ArtShare”和“ ArtShare.TotalArtShares”。 So it throws exception as you have already retrieved complete ArtShare object. 因此,如果您已经检索了完整的ArtShare对象,它将引发异常。

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 : 如果要完整的“ ArtShare”对象,则只需编写:

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`  
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM