我无法弄清楚如何在复杂的嵌套对象上包装查询的“查询”语句。
我已将概念简化为以下内容 -
我有一个索引,如条目
{
"_index": "my_index",
"_type": "my_type",
"_id": "5",
"_source": {
"group": "student",
"user": [
{
"first": "Hubert",
"last": "Rock",
"grade": "B",
"address": "12 Hunting St"
}
]
}
}
'user'是嵌套对象的位置。现在我想进行搜索以识别名字为“Hubert”但在“等级”和“地址”字段中都没有条目的条目。
我可以单独做 - (获取所有'休伯特')
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Hubert" }}
]
}
}
}
}
}
(获取所有没有“成绩”和“地址”值的条目)
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must_not": [
{
"exists" : {
"field":"user.grade"
}
},
{
"exists" : {
"field":"user.address"
}
}
]
}
}
}
}
}
但我真的不知道如何将它们结合起来。有任何想法吗?
您只需要在单个bool查询下组合must
和must_not
子句,如下所示:
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Hubert"
}
}
],
"must_not": [
{
"exists": {
"field": "user.grade"
}
},
{
"exists": {
"field": "user.address"
}
}
]
}
}
}
}
}