嵌套的 bool 应该查询不与最小值一起工作应该匹配

Nested bool should query not working with the minimum should match

我有以下查询,我想在名称字段或消息字段中获取搜索词 "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"
      }
    }
  ]
}