Elasticsearch must_not bool查询不起作用

时间:2018-04-09 22:45:33

标签: elasticsearch elasticsearch-6

我有一个带有filtermust_not子句的bool查询。但是当我执行查询时,我得到的结果包含must_not子句中指定的值。知道我做错了吗?

查询

POST /test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "partnerId": {
              "value": 12345
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "stage": {
              "value": "REJECTED"
            }
          }
        }
      ],
      "adjust_pure_negative": true
    }
  }
}

响应

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "45678910",
        "_score": 0,
        "_source": {
          "id": 45678910,
          "partnerId": 12345,
          "stage": "REJECTED"
        }
      }
    ]
  }
}

映射

"mappings": {
      "doc": {
        "properties": {
          "id": {
            "type": "long"
          },
          "partnerId": {
            "type": "long"
          },
          "stage": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }

请注意,在我的结果中,即使我已将REJECTED条款包括在内,我仍然会获得must_not阶段。

Elasticsearch版本:6.2.2

1 个答案:

答案 0 :(得分:-1)

如果您正在使用字符串,则应该:

  • 对行字符串使用匹配
  • 将术语与字符串
  • 中的关键字一起使用

您在官方文档中拥有所需的一切

MatchTerm

使用匹配:

GET /_search
{
"query": {
    "match" : {
        "message" : "this is a test"
        }
    }
}

使用术语:

POST _search
{
"query": {
    "term" : { "user" : "Kimchy" } 
    }
}

我希望我能帮到你