嵌套bool应查询不使用最小匹配

时间:2015-08-06 18:48:24

标签: 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"
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

在过滤后的查询中,您必须有一个过滤器部件,此处不存在。我建议像这样重写它,即将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"
      }
    }
  ]
}
相关问题