在弹性搜索中使用 query_string 查询和 bool 导致解析异常

using query_string query with bool in elastic search causing parsing exception

为什么这个查询给我一个解析异常?如果我删除这本书,它似乎确实有效。但我需要那本书 query_string。我怎样才能使这项工作?

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "status_type": [
                                    "5"
                                ]
                            }
                        }
                    ]
                }
            },
            "filter": {
                "query_string": {
                    "fields": [
                        [
                            "name",
                            "message"
                        ]
                    ],
                    "query": "Arnold AND Schwarz"
                }
            }
        }
    },
    "sort": [
        {
            "total_metrics": {
                "order": "desc"
            }
        }
    ]
}

你得到的解析异常应该告诉你类似 No filter registered for [query_string]

实际上,没有 query_string 过滤器,但是有一个 query_string 查询。因此,如果您交换过滤器和查询,它将起作用:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {         <--- move query_string in the query part
          "fields": [
            [
              "accountIdentifier",
              "accountName"
            ]
          ],
          "query": "Arnold AND Schwarz"
        }
      },
      "filter": {
        "bool": {                 <--- move the bool in the filter part
          "must": [
            {
              "terms": {
                "quantity": [
                  "5"
                ]
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total_metrics": {
        "order": "desc"
      }
    }
  ]
}

您应该使用 query filter 将任何查询包装到过滤器中。否则你会得到 No filter registered for [query_string].

的解析错误

您需要将 filter 部分更改为:

"filter": {
  "query": { // <- wraps a query as a filter
    "query_string": {
      "fields": [
        [
          "name",
          "message"
        ]
      ],
      "query": "Arnold AND Schwarz"
    }
  }
}

@Edit:因为我看到人们可能有问题注意到我只粘贴了整个查询的更改部分,包括 filter 部分(而不是整个 filtered),这是之后的全部内容修改:

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "status_type": [
                                    "5"
                                ]
                            }
                        }
                    ]
                }
            },
            "filter": {
                "query": { // <- the only difference!
                    "query_string": {
                        "fields": [
                            [
                                "name",
                                "message"
                            ]
                        ],
                        "query": "Arnold AND Schwarz"
                    }
                }
            }
        }
    },
    "sort": [
        {
            "total_metrics": {
                "order": "desc"
            }
        }
    ]
}