我有下面的映射,我想访问imageMap的属性而不是所有集合。
"imageMap": {
"properties": {
"item1": {
"type": "long"
},
"item2": {
"type": "string"
},
"item3": {
"type": "string"
}
}
}
以下是样本数据
imageMap": [
{
"item1": 20893,
"item2": "ImageThumbnail_100_By_44",
"item3": "/9j/4AAQSkZJRg"
},
{
"item1": 20893,
"item2": "ImageThumbnail_400_By_244",
"item3": "/9j/4AAQSkZJRgABAQEAYABgAAD/2w"
}
]
以下是我的查询无效。任何帮助表示赞赏。提前谢谢。
更新
{
"_source": {
"include": [
"imageMap"
]
},
"query": {
"bool": {
"must": [
{
"term": {
"imageMap.item1": {
"value": 20893
}
},
"term": {
"imageMap.item2": {
"value": "imagethumbnail_100_by_44"
}
}
}
]
}
}
}
预期结果仅低于imageMap的单个元素,但我得到数组:
"_source": {
"imageMap": [
{
"item2": "ImageThumbnail_100_By_44",
"item1": 20893,
"item3": "/9j/4AAQSkZJRgABAQ"
}
]
}
答案 0 :(得分:1)
您的查询无效,因为您使用的term query
在您的搜索字符串上没有analysis
。由于您未在映射中指定任何分析器 ImageThumbnail_100_By_44 存储为 imagethumbnail_100_by_44 ,因为它由standard analyzer分析
根据您的要求,您可以将item2
映射为"index : not_analyzed"
,您的查询也可以正常使用,或者您可以使用match query
进行分析。
{
"_source": {
"include": [
"imageMap"
]
},
"query": {
"bool": {
"must": [
{
"match": {
"imageMap.item2": {
"query": "ImageThumbnail_100_By_44"
}
}
}
]
}
}
}
请仔细阅读this document以更好地了解分析过程
答案 1 :(得分:1)
如果您只想从imageMap
数组中获取单个元素,则需要将imageMap
映射为nested
object,如下所示:
"imageMap": {
"type": "nested", <--- add this
"properties": {
"item1": {
"type": "long"
},
"item2": {
"type": "string"
},
"item3": {
"type": "string"
}
}
}
然后你需要擦除索引并使用这个新映射从头开始重新构建它。
最后,您将只能使用nested
inner_hits
query检索特定元素:
{
"_source": false,
"query" : {
"nested" : {
"path" : "imageMap",
"query" : {
"match" : {"imageMap.item2" : "ImageThumbnail_100_By_44"}
},
"inner_hits" : {}
}
}
}