我有以下文件

{
  "_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"}]}
}

我想访问/检索ArtShare的值及其属性和数组StatusHistoryList的值

我正在这样
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`
     }

更新
如果我这样做
 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
         }

然后抛出异常
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)

请指导我如何获取这些值

最佳答案

是的,实际上问题是您在字段数组中同时提到了“ArtShare”和“ArtShare.TotalArtShares”。因此,如果您已经检索了完整的ArtShare对象,它将引发异常。

因此,请提及所需的字段。如果需要指定的嵌套值,则无需访问完整的父对象。

试试这个:

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

如果您想要完整的“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`
 }

10-06 05:42