Elasticsearch:如何添加“嵌套查询"进入(嵌套)过滤器 [英] Elasticsearch: how to add "nested query" into (nested) filter

查看:121
本文介绍了Elasticsearch:如何添加“嵌套查询"进入(嵌套)过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构:

  • 人员:键入对象.

  • Person: type object.

Person.Cars:类型为嵌套.

Person.Cars: type nested.

Person.Cars.Radios:键入嵌套.

Person.Cars.Radios: type nested.

我想编写一个查询,以查找所有符合以下条件的人:

I want to write a query that should find all person with:

  • 汽车X
  • 汽车年限从40到100
  • Y车上的收音机
 {
      "query": {
        "nested": {
          "path": "person.cars",
          "query": {
            "bool": {
              "filter": [
                {
                  "match": {
                    "person.cars.id": {
                      "query": "X"
                    }
                  }
                },
                {
                  "range": {
                    "person.cars.year": {
                      "from": 40,
                      "to": 100
                    }
                  }
                }
              ]
            },
            "nested": {
              "path": "person.cars.radios",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "match": {
                        "person.cars.radios.id": {
                          "query": "Y"
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }

我的答复:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 26,
                "col": 5
            }
        ],
        "type": "parsing_exception",
        "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 26,
        "col": 5
    },
    "status": 400
}

如何编写这样的查询?我知道我必须移动第二个嵌套部分,但是我不知道如何操作.

How to write query like this? I know that I have to move my second nested part, but I don't know how.

推荐答案

我建议您将第二个嵌套查询添加为第一个布尔过滤器的一部分.

I suggest you to add, your second nested query as a part of your first bool-filter.

 {
  "query": {
    "nested": {
      "path": "person.cars",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "person.cars.id": {
                  "query": "X"
                }
              }
            },
            {
              "range": {
                "person.cars.year": {
                  "from": 40,
                  "to": 100
                }
              }
            },
            {
              "nested": {
                "path": "person.cars.radios",
                "query": {
                  "bool": {
                    "filter": [
                      {
                        "match": {
                          "person.cars.radios.id": {
                            "query": "Y"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

这篇关于Elasticsearch:如何添加“嵌套查询"进入(嵌套)过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆