我在某些文档中嵌套了数据,而在某些文档中则没有。假设下面是映射。
{
"mappings": {
"item": {
"properties": {
"id": {
"type": "string"
},
"nestedType": {
"type": "nested",
"properties": {
"item1": {
"type": "long"
},
"item2": {
"type": "string"
}
}
}
}
}
}
}
我想基于id进行查询,并希望嵌套元素包含在我的响应中,其中item1 =1234。但我不想过滤响应。如果item1!= 1234或item1不存在。
实际上,我不希望嵌套查询影响我的点击结果。但如果匹配项没有发现内部匹配,则包括内部匹配。
最佳答案
您可以尝试以下查询。仅当存在inner_hits
时,它将获取结果。
{
"query": {
"bool": {
"must": [
{
"term": {
"id": "idValue"
}
}
],
"should": [
{
"nested": {
"path": "nestedType",
"query": {
"match": {
"item2": "item2Value"
}
},
"inner_hits": {}
}
}
]
}
}
}
关于elasticsearch - ElasticSearch查询将嵌套数据作为响应,但如果不存在嵌套字段,则不过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123129/