简体   繁体   English

添加术语过滤器时的弹性搜索查询解析异常

[英]elastic search query parsing exception when adding term filter

I'm not quite sure why the term filter "term": {"language": "Austrian"} is causing an elastic search parse exception. 我不太确定为什么术语过滤器"term": {"language": "Austrian"}会导致弹性搜索解析异常。

The surprising thing is it works if I remove the query_string query. 令人惊讶的是,如果删除query_string查询,它会起作用。 Where would I put "term": {"language": "Austrian"} filter if it doesn't go there? 如果没有将"term": {"language": "Austrian"}过滤器放在哪里?

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "7"
                ]
              }
            }
          ]
        }
      },
      "filter": {
        "query": {
          "query_string": {
            "fields": [
              [
                "name",
                "message"
              ]
            ],
            "query": "Arnold AND Schwarz"
          }
        },
        "term": {                <-- Causes parse exception
          "language": "Austrian"
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

Inside your filter , you need a bool filter if you have more than one constraints, which is your case, since you have a query filter and a term filter . filter内部,如果您有多个约束,则需要bool过滤器 ,这是您的情况,因为您有query过滤器term过滤器 So the correct way of doing it is like this: 因此,正确的做法是这样的:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "7"
                ]
              }
            }
          ]
        }
      },
      "filter": {
        "bool": {               <---- add this
          "must": [             <---- and this
            {
              "query": {
                "query_string": {
                  "fields": [
                    [
                      "name",
                      "message"
                    ]
                  ],
                  "query": "Arnold AND Schwarz"
                }
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

However, if I may add something, I would rewrite your query a bit differently and move the query_string over to the query part and the status_type term over to the filter part, it would feel more "natural". 但是,如果我可以补充一下,我会有点不同重写查询和移动query_string到的query部分和status_type term到了filter的一部分,它会感觉更“自然”。 Also, in your query part you don't need a bool/must if you have only one constraint. 另外,在query部分中,如果只有一个约束,则不需要bool/must

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "fields": [
            [
              "name",
              "message"
            ]
          ],
          "query": "Arnold AND Schwarz"
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "7"
                ]
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM