gpt4 book ai didi

lucene - 添加术语过滤器时的 Elasticsearch 查询解析异常

转载 作者:行者123 更新时间:2023-12-03 02:01:42 25 4
gpt4 key购买 nike

我不太确定为什么过滤器"term": {"language": "Austrian"}一词会导致 Elasticsearch 解析异常。

令人惊讶的是,如果删除了query_string查询,它会起作用。
如果不将"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"
}
}
]
}

最佳答案

filter内部,如果您有多个约束,则需要 bool filter,这是您的情况,因为您有 query filter term filter。因此,正确的做法是这样的:

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

但是,如果我可以添加一些内容,我将以某种不同的方式重写查询,然后将 query_string移至 query部分,将 status_type term移至 filter部分,这会感觉更“自然”。另外,如果只有一个约束,则在 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"
}
}
]
}

关于lucene - 添加术语过滤器时的 Elasticsearch 查询解析异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com