我有这样的文档结构。对于下面两个文档,我们嵌套了称为交互信息的文档。我只需要获取具有标题持续时间且其值大于60的文档
这里的值字段是关键字,而不是整数。我知道只有整数范围查询会被执行。有没有可能找到持续时间大于60的文档(无痛查询或脚本查询)。就像将值Field转换为Integer然后搜索文档一样。
{
"key": "f07ff9ba-36e4-482a-9c1c-d888e89f926e",
"interactionInfo": [
{
"title": "duration",
"value": "11"
},
{
"title": "timetaken",
"value": "9"
},
{
"title": "talk_time",
"value": "145"
}
]
},
{
"key": "f07ff9ba-36e4-482a-9c1c-d888e89f926e",
"interactionInfo": [
{
"title": "duration",
"value": "120"
},
{
"title": "timetaken",
"value": "9"
},
{
"title": "talk_time",
"value": "60"
}
]
}
最佳答案
我添加了脚本来获取interactInfo.value>“somevalue”。脚本很慢,最好在索引时间解决此问题并使用范围查询。
指数:
{
"index15" : {
"mappings" : {
"properties" : {
"interactionInfo" : {
"type" : "nested",
"properties" : {
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"value" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"key" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
查询:
{
"query": {
"nested": {
"path": "interactionInfo",
"query": {
"bool": {
"must": [
{
"term": {
"interactionInfo.title.keyword": {
"value": "duration"
}
}
},
{
"script": {
"script": {
"source":"def val=Integer.parseInt(doc['interactionInfo.value.keyword'].value); if(val>params.value) return true; else return false;",
"params": {
"value":10
}
}
}
}
]
}
},
"inner_hits": {}
}
}
}
关于elasticsearch - 在嵌套文档上查询关键字类型的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61586498/