ElasticSearch中keyword和text的区别


keyword:存储数据时候,该类型的字段不会分词建立索引
text:存储数据时候,会自动分词,并生成索引
数据准备

PUT /test
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "desc":{
        "type": "text"
      }
    }
  }
}

POST /test/_doc
{
  "name":"梦露",
  "desc":"性感魅力"
}

POST /test/_doc
{
  "name":"奥黛丽·赫本",
  "desc":"光芒四射"
}

模糊查询keyword类型字段

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

运行结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

模糊查询text类型字段

POST /test/_search
{
  "query": {
    "match": {
      "desc": "芒"
    } 
  }
}

运行结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "JASwgHQByM5jgHs7lImo",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "奥黛丽·赫本",
          "desc" : "光芒四射"
        }
      }
    ]
  }
}

精确查询keyword类型字段

POST /test/_search
{
  "query": {
    "term": {
      "name": "梦露"
    } 
  }
}

运行结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "HgSwgHQByM5jgHs7XYkt",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "梦露",
          "desc" : "性感魅力"
        }
      }
    ]
  }
}

精确查询text类型字段

POST /test/_search
{
  "query": {
    "term": {
      "desc": "光芒四射"
    } 
  }
}

查询结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值