[英]How to get nested types in Elasticsearch
I have the following document: 我有以下文件:
{
"_index" : "testdb",
"_type" : "artWork",
"_id" : "0",
"_version" : 4,
"found" : true,
"_source":{"uuid":0,
"StatusHistoryList":[
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"ACTIVE"
},
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"INACTIVE"
}
]
}
and here is the mapping of the document: 这是文档的映射:
{
"testdb" : {
"mappings" : {
"artWork" : {
"properties" : {
"StatusHistoryList" : {
"type" : "nested",
"properties" : {
"ArtWorkDate" : {
"type" : "string",
"store" : true
},
"ArtworkStatus" : {
"type" : "string",
"store" : true
}
}
},
"uuid" : {
"type" : "integer",
"store" : true
}
}
}
}
}
}
Now I want to access the values of StatusHistoryList
. 现在,我想访问
StatusHistoryList
的值。 I got null values if I do it like this: 如果这样做,我将得到空值:
val get = client.prepareGet("testdb", "artWork", Id.toString()).setOperationThreaded(false)
.setFields("uuid",,"StatusHistoryList.ArtworkStatus","StatusHistoryList.ArtWorkDate","_source")
.execute()
.actionGet()
var artworkStatusList= get.getField("StatusHistoryList.ArtworkStatus").getValues.toArray()
var artWorkDateList= get.getField("StatusHistoryList.ArtWorkDate").getValues.toArray()
then I got null values from the code but my document contains the values then I found this question so after that i tried to do it like this 然后我从代码中得到了空值,但是我的文档包含了这些值,然后我发现了这个问题,所以在此之后我尝试这样做
var smap = get.getSource.get("StatusHistoryList").asInstanceOf[Map[String,Object]]
but then a ClassCastException
is thrown 但是然后
ClassCastException
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
Please help me how can I get the values of StatusHistoryList
's ArtworkStatus
and ArtWorkDate
values please guide me I will be very thankfull to you. 请帮助我如何获取
StatusHistoryList
的ArtworkStatus
和ArtWorkDate
值,请指导我,我将非常感谢您。
You have almost derived the solution. 您几乎已经得出了解决方案。 The GET request has retrieved the response but the problem is in parsing the response.
GET请求已检索到响应,但问题在于解析响应。
Let's see the problem. 让我们看看问题所在。 Below is the document that the elastic search returns as source
以下是弹性搜索返回的文件来源
{
"uuid":0,
"StatusHistoryList":[
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"ACTIVE"
},
{
"ArtWorkDate":"2015-08-28T15:52:03.030+05:00",
"ArtworkStatus":"INACTIVE"
}
]
}
When we do get.getSource.get("StatusHistoryList")
it returns List ArtWork objects and not a Map. 当我们执行
get.getSource.get("StatusHistoryList")
它将返回List get.getSource.get("StatusHistoryList")
对象,而不是Map。 That is the reason for the classCastException
exception. 这就是
classCastException
异常的原因。
So if you cast the response to list of objects your problem will be solved. 因此,如果将响应转换为对象列表,则将解决问题。
But this would not be an ideal solution. 但这不是理想的解决方案。 Some of the libraries like Jackson-Faterxml does the job for you.
一些库(例如Jackson-Faterxml)可以为您完成这项工作。 Using the fasterxml library you can bind the json to equivalent POJO Object.
使用fasterxml库,您可以将json绑定到等效的POJO对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.