在弹性搜索中使用query_string查询与bool导致解析异常 [英] using query_string query with bool in elastic search causing parsing exception

查看:500
本文介绍了在弹性搜索中使用query_string查询与bool导致解析异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个查询给我一个解析异常?如果我删除bool它似乎工作。但是我需要使用query_string的bo​​ol。如何使这项工作?

  {
查询:{
filtered:{
query:{
bool:{
must:[
{
terms:{
status_type
5
]
}
}
]
}
},
过滤器:{
query_string:{
fields:[
[
name,
message
]
],
查询:阿诺德和施瓦茨
}
}
}
},
sort:[
{
total_metrics:{
order:desc
}
}
]
}


解决方案

您应该使用查询过滤器,将任何查询包装到过滤器中。否则您将得到解析错误没有为[query_string]注册过滤器



您需要更改过滤器部分到:

 filter:{
query:{//< - 将查询作为过滤器
query_string:{
fields:[
[
name,
消息
]
],
查询:阿诺德和施瓦茨
}
}
}

@Edit:由于我看到人们可能会遇到问题,注意到我只粘贴了整个查询的更改部分,包括过滤器部分(不是整个过滤)修改后的整个事情:

  {
query:{
filtered:{
query:{
bool:{
必须:[
{
条款:{
status_type:[
5
]
}
}
]
}
},
filter :{
query:{//< - 唯一的区别!
query_string:{
fields:[
[
name,
message
]
],
查询:阿诺德和施瓦茨
}
}
}
}
},
sort:[
{
total_metrics:{
order:desc
}
}
]
}


Why is this query giving me a parsing exception? If I remove the bool it does seem to work. But I need the bool there with the query_string. How can I make this work?

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

解决方案

You should use the query filter which wraps any query into a filter. Otherwise you will get the parse error you get No filter registered for [query_string].

You need to change your filter part to:

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

@Edit: since I see people might have problems noticing that I only pasted the changed part of the whole query including the filter part (not the whole filtered) here's the whole thing after modification:

{
    "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"
            }
        }
    ]
}

这篇关于在弹性搜索中使用query_string查询与bool导致解析异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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