为什么这个查询会给我一个解析异常?如果我删除bool它似乎确实有效。但是我需要使用query_string进行bool。我怎样才能做到这一点?
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"terms": {
"status_type": [
"5"
]
}
}
]
}
},
"filter": {
"query_string": {
"fields": [
[
"name",
"message"
]
],
"query": "Arnold AND Schwarz"
}
}
}
},
"sort": [
{
"total_metrics": {
"order": "desc"
}
}
]
}
答案 0 :(得分:2)
您获得的解析异常应该告诉您类似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"
}
}
]
}
答案 1 :(得分:2)
您应该使用将任何查询包装到过滤器中的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"
}
}
]
}