弹性搜索:匹配查询在嵌套Bool过滤器中不起作用

时间:2015-08-08 10:33:23

标签: elasticsearch

我能够获取以下弹性搜索查询的数据:

{
  "query": {
    "filtered": {
      "query": [],
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "term": {
                      "gender": "malE"
                    }
                  },
                  {
                    "term": {
                      "sentiment": "positive"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

但是,如果我使用“匹配”查询 - 我收到400状态响应的错误消息

{
  "query": {
    "filtered": {
      "query": [],
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "gender": "malE"
                    }
                  },
                  {
                    "term": {
                      "sentiment": "positive"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

嵌套bool过滤器不支持匹配查询吗?

由于术语查询在字段的倒排索引中查找确切的术语,并且我想将性别数据作为case_insensitive字段进行查询 - 我应该尝试哪种方法?

索引的设置:

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "analyzer_keyword": {
            "tokenizer": "keyword",
            "filter": "lowercase"
          }
        }
      }
    }
  }
}

字段映射性别:

{"type":"string","analyzer":"analyzer_keyword"}

1 个答案:

答案 0 :(得分:1)

您收到错误400的原因是因为没有match过滤器,只有match queries,即使term queriesterm filters都存在。

您的查询可以像这样简单,即不需要filtered查询,只需将termmatch个查询放入bool/should

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "gender": "male"
          }
        },
        {
          "term": {
            "sentiment": "positive"
          }
        }
      ]
    }
  }
}
相关问题