【问题标题】:How to write a nested query in combination with filters in Elastic Search?如何结合 Elastic Search 中的过滤器编写嵌套查询?
【发布时间】:2019-04-03 17:52:11
【问题描述】:

我需要做一个嵌套查询,尝试在对象数组中进行搜索。除此之外,我还有其他需要搜索的参数。我正在使用过滤器。如何有效地组合过滤器和嵌套查询?抱歉,我不是 ES 专家。

这是我尝试在 Kibana 中执行的查询:

GET test-index/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isPublic": true
              }
            },
            {
              "term": {
                "isDeleted": false
              }
            }
          ]
        },
        "nested": {
          "path": "data.location.countries",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "data.location.countries.name": "United States"
                  }
                },
                {
                  "range": {
                    "data.location.countries.weight": {
                      "gt": 30
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "size": "60",
      "from": 0,
      "sort": [
        {
          "followers": {
            "order": "desc"
          }
        }
      ]
    }

它返回一个错误:

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

有人可以对此有所了解吗?

【问题讨论】:

    标签: elasticsearch kibana elastic-stack elasticsearch-5


    【解决方案1】:

    在 bool.filter 中移动嵌套查询:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isPublic": true
              }
            },
            {
              "term": {
                "isDeleted": false
              }
            },
            {
              "nested": {
                "path": "data.location.countries",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "data.location.countries.name": "United States"
                        }
                      },
                      {
                        "range": {
                          "data.location.countries.weight": {
                            "gt": 30
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "size": "60",
      "from": 0,
      "sort": [
        {
          "followers": {
            "order": "desc"
          }
        }
      ]
    }
    

    您不能在 compound query afaik 之外使用超过 1 个查询。

    【讨论】:

      猜你喜欢
      • 2016-02-10
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多