elasticsearch - 如何在Elastic Search中结合过滤器编写嵌套查询?

标签 elasticsearch kibana elastic-stack elasticsearch-5

我需要做一个嵌套查询,尝试在对象数组中搜索。除此之外,我还需要搜索其他参数。我正在为那些使用过滤器。如何有效组合过滤器和嵌套查询?抱歉,我不是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
}

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

最佳答案

在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个查询。

关于elasticsearch - 如何在Elastic Search中结合过滤器编写嵌套查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53068534/

相关文章:

Elasticsearch 相似度 discount_overlaps

elasticsearch - Kibana返回结果,其中特定字段是唯一的

regex - Lucene正则表达式中的单词边界

join - Elasticsearch /Kibana : Application-side joins

elasticsearch - 无法访问elastalert的警报部分内的数据

elasticsearch - JSON解析器的Logstash输出未发送到elasticsearch

java - SonarQube 已停止且未运行

elasticsearch - 从整数字段创建图表

elasticsearch - 使用Jhipster在kibana中未显示 Elasticsearch 索引

elasticsearch - 在logstash中使用grok模式解析我的json文件?