elasticsearch - 如何在ElasticSearch中进行完全匹配查询?

标签 elasticsearch

我想对ElasticSearch索引进行完全匹配查询,
我有以下数据-

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.21110919,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.21110919,
        "_source" : {
          "id" : 1,
          "name" : "test"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.160443,
        "_source" : {
          "id" : 2,
          "name" : "test two"
        }
      }
    ]
  }
}

我想查询name字段,
我正在尝试搜索名称test
但这给了我两个文件。

预期结果是唯一的文件1
映射如下-
{
  "test" : {
    "mappings" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

我尝试了以下操作-
GET /test/_search
{
  "query": {
    "bool": {
      "must": {
        "term" : { 
          "name": "test"
        }
      }
    }
  }
}


GET /test/_search
{
  "query": {
    "match": {
      "name": "test"
    }
  }
}

最佳答案

除了指向我在评论中提供的答案的链接之外,建议您将名称字段定义为:

{
   "name":{
      "type": "text",
      "fields":{
         "keyword":{
            "type": "keyword"
         }
      }
   }
}

然后在需要完全匹配(区分大小写)时查询name.keyword字段,如果想要部分匹配(例如仅搜索名字),则查询name

关于elasticsearch - 如何在ElasticSearch中进行完全匹配查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60751711/

相关文章:

datetime - Elasticsearch不想接受null为日期

elasticsearch - Elasticsearch-强制字段仅索引,避免存储

elasticsearch - 将字段设置为在Elasticsearch 1.7的索引下的所有( future )类型中为not_analysed

c# - 如何在 NEST 中禁用驼峰式 Elasticsearch 字段名称?

mysql - Elasticsearch如何匹配一个二维数组?

elasticsearch - Elasticsearch文字匹配百分比

elasticsearch - Microsoft Power BI 和 ElasticSearch

elasticsearch - 如何按对象的子对象字段对对象进行排序?

php - 每个实体一个查询

directory - 我如何找到 ElasticSearch 在哪里安装我的插件?