如何获取弹性搜索中的嵌套类型 [英] How to get nested types in Elasticsearch
问题描述
我有以下文件:
{
"_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"
}
]
}
这里是映射文件:
{
"testdb" : {
"mappings" : {
"artWork" : {
"properties" : {
"StatusHistoryList" : {
"type" : "nested",
"properties" : {
"ArtWorkDate" : {
"type" : "string",
"store" : true
},
"ArtworkStatus" : {
"type" : "string",
"store" : true
}
}
},
"uuid" : {
"type" : "integer",
"store" : true
}
}
}
}
}
}
现在我想访问 StatusHistoryList
。如果我这样做,我得到空值:
Now I want to access the values of 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]]
但是一个 ClassCastException
抛出
but then a ClassCastException
is thrown
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
请帮助我如何获取 StatusHistoryList
的 ArtworkStatus
和 ArtWorkDate
值请指导我,我将非常感谢你。 >
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.
推荐答案
你几乎派生了解决方案。 GET请求已经检索到响应,但问题在于解析响应。
You have almost derived the solution. The GET request has retrieved the response but the problem is in parsing the response.
让我们看看问题。以下是弹性搜索作为来源返回的文档
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"
}
]
}
当我们做 get.getSource.get StatusHistoryList)
它返回列表ArtWork对象而不是Map。这就是 classCastException
异常的原因。
When we do get.getSource.get("StatusHistoryList")
it returns List ArtWork objects and not a Map. That is the reason for the classCastException
exception.
所以如果你把响应放在对象列表上,你的问题会要解决。
So if you cast the response to list of objects your problem will be solved.
但这不是一个理想的解决方案。一些图书馆,如 Jackson-Faterxml 为您提供的工作。使用rapidxml库可以将json绑定到等效的POJO对象。
But this would not be an ideal solution. Some of the libraries like Jackson-Faterxml does the job for you. Using the fasterxml library you can bind the json to equivalent POJO Object.
这篇关于如何获取弹性搜索中的嵌套类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!