查询以查找与查询中所有字段完全匹配的所有文档

时间:2020-01-28 09:32:10

标签: elasticsearch

我有一个简单的文档结构,如下所示。

{
    "did" : "1",
    "uid" : "user1",
    "mid" : "pc-linux1",
    "path" : "/tmp/path1" 
}

我需要查询elastic,它完全匹配所有字段

GET index2/_search
{
  "query": {
     "bool":{
      "must": [
        {
          "term" : { "uid" : "user1"}
        },
        {
          "term" : { "mid" : "pc-linux1"}
        },
        {
          "term" : { "did" : "1"}
        },
        {
          "term" : { "path" : "/tmp/path1"}
        }
      ]
    }
  }
}

匹配应该在没有任何形式的关键字“弹性”分析的情况下进行,以使“ / tmp / path1”作为一个完整的术语匹配。

我尝试使用自定义映射:

“索引”:false

不起作用。

PUT /index2?include_type_name=true
{
    "mappings" : {
      "_doc": {
      "properties" : {
        "did" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "index" : false,
              "ignore_above" : 256
            }
          }
        },
        "mid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "index" : false,
              "ignore_above" : 256
            }
          }
        },
        "path" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "index" : false,
              "ignore_above" : 256
            }
          }
        },
        "uid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "index" : false,
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

我使用的是elastic7.0,还有几篇文章建议使用

进行自定义映射

“索引”:“未分析”

在弹性7.0中未被接受为有效映射

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果您要匹配精确的字词,请尝试以下查询:

GET index2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "uid": "user1"
          }
        },
        {
          "match": {
            "mid": "pc-linux1"
          }
        },
        {
          "match": {
            "did": "1"
          }
        },
        {
          "match": {
            "path": "/tmp/path1"
          }
        }
      ]
    }
  }
}
相关问题