elasticsearch - 嵌套 bool 应查询不能与最小值匹配

标签 elasticsearch elastic-stack

我在下面的查询中要在名称字段或消息字段中获取搜索词"Schwarz"。语言必须为奥地利语,并且状态类型应等于提供的列表。我收到以下异常,但无法弄清原因:

QueryParsingException [[my_test_index] [_na]查询格式错误,start_object之后没有字段]; }]“,

{
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "bool": {
                                    "should": [
                                        {
                                            "term": {
                                                "name": "Schwarz"
                                            }
                                        },
                                        {
                                            "term": {
                                                "message": "Schwarz"
                                            }
                                        }
                                    ],
                                    "minimum_should_match": 1
                                }
                            },
                            {
                                "terms": {
                                    "status_type": [
                                        "1",
                                        "2",
                                        "3",
                                        "4",
                                        "5",
                                        "6",
                                        "7"
                                    ]
                                }
                            },
                            {
                                "term": {
                                    "language": "Austrian"
                                }
                            }
                        ]
                    }
                }
            }
        },
        "sort": [
            {
                "total": {
                    "order": "desc"
                }
            }
        ]
    }

这是没有筛选器仍然有效的查询:
{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "standard_analyzed_name": "Schwarz"
              }
            },
            {
              "match": {
                "standard_analyzed_message": "Schwarz"
              }
            }
          ],
          "must": [
            {
              "terms": {
                "buzzsumo_status_type": [
                  "1",
                  "2",
                  "3",
                  "4",
                  "5",
                  "6",
                  "7"
                ]
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total_interactions": {
        "order": "desc"
      }
    }
  ]
}

最佳答案

在经过过滤的查询中,您必须具有过滤器部分,此处未提供。我建议像这样重写它,即将termsterm部分移到filter部分:

{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "name": "Schwarz"
              }
            },
            {
              "term": {
                "message": "Schwarz"
              }
            }
          ],
          "minimum_should_match": 1
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "status_type": [
                  "1",
                  "2",
                  "3",
                  "4",
                  "5",
                  "6",
                  "7"
                ]
              }
            },
            {
              "term": {
                "language": "Austrian"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

一种替代方法是不使用filtered查询,而只是像这样编写它:
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "name": "Schwarz"
                }
              },
              {
                "term": {
                  "message": "Schwarz"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "terms": {
            "status_type": [
              "1",
              "2",
              "3",
              "4",
              "5",
              "6",
              "7"
            ]
          }
        },
        {
          "term": {
            "language": "Austrian"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "total": {
        "order": "desc"
      }
    }
  ]
}

关于elasticsearch - 嵌套 bool 应查询不能与最小值匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863592/

相关文章:

elasticsearch - 可以将弹性中的字段映射限制设置为 50k 吗?

java - Logstash stdout - 写入文件

python - python elasticsarch集成

elasticsearch - 由于空间限制,Elasticsearch无法在Windows中启动

testing - 通过文件初始化 Elasticsearch 数据库

elasticsearch - 增加内存大小elasticsearch

elasticsearch - Elasticsearch 查询组合键,例如在SQL中

javascript - Elasticsearch 日期数学问题,不确定范围

Elasticsearch:获取文档字段值中的子字符串?

java - Logstash 未将完整数据从 oracle 加载到 Elasticsearch