Elasticsearch:将结果限制为完全匹配的文档

Elasticsearch: restrict result to documents with exact match

目前我试图通过以下查询限制 Elasticsearch (5.4) 的结果:

{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "apache log Linux",
          "type": "most_fields",
          "fields": [
            "message",
            "type"
          ]
        }
      },
      "filter": {
        "term": {
          "client": "test"
        }
      }
    }
  }
}

此 returns 每个包含 "apache""log" 或 "linux" 的文档。我想将结果限制为具有字段 [=26] 的文档=] 与 exact 指定值,本例:"test"。但是,此查询 returns 所有包含 "test" 作为值的文档。A带有 "client": "test client" 的文档也将被返回。 我想限制准确,所以只应返回带有 "client": "test" 的文档,而不应返回 "client": "test client".

在测试了一堆不同的查询和大量搜索之后,我找不到解决我的问题的方法。我错过了什么?

在您的索引上设置一个映射,指定您的 client 字段是 keyword datatype

映射请求可能看起来像

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "client": {
          "type":  "keyword"
        }
      }
    }
  }
}

只需使用 client 字段的 keyword 部分,因为这是 5.x 并且 默认情况下 keyword 已经存在:

  "filter": {
    "term": {
      "client.keyword": "test"
    }
  }