ElasticSearch中text和keyword类型的区别 - Go语言中文社区

ElasticSearch中text和keyword类型的区别


    在ES的2.X版本中,对于字符类型的数据,我们都使用string类型作为映射,然后再设置它的分词,例如我们新建了一个名为  zk_test的索引,类型为 info,为其中字符类型的name字段设置索引,设置不分词,Kibanan中操作如下

PUT /zk_test/info/_mapping
{
  "info":{
    "properties":{
      "name":{"type":"string","index":"not_analyzed"},
      "age":{"type":"integer"}
    }
  }
}

    但是在ES5.6.8中这样操作,虽然操作会成功,但是ES会提醒你,string类型已经过期,请使用  text或keyword类型设置name字段。

text和keyword这两个类型,是在5以后的版本中出现的。官网中,对这两个数据类型,这样描述

Text datatype:
A field to index full-text values, such as the body of an email or the description of a product. These fields are analyzed, that is they are passed through an analyzer to convert the string into a list of individual terms before being indexed. The analysis process allows Elasticsearch to search for individual words within each full text field. Text fields are not used for sorting and seldom used for aggregations (although the significant text aggregation is a notable exception).
If you need to index structured content such as email addresses, hostnames, status codes, or tags, it is likely that you should rather use a keyword field.
Keyword datatype
A field to index structured content such as email addresses, hostnames, status codes, zip codes or tags.
They are typically used for filtering (Find me all blog posts where status is published), for sorting, and for aggregations. Keyword fields are only searchable by their exact value.
If you need to index full text content such as email bodies or product descriptions, it is likely that you should rather use a text field.

官方文档链接:text      keyword

    原来,es从2.X版本一下子跳到了5.X版本,将string类型变为了过期类型,取而代之的是text和keyword数据类型,一直到现在最新的6以上版本。接下来就看看这两个字段的区别。
    
     按照官方文档的阐述,text类型的数据被用来索引长文本,例如电子邮件主体部分或者一款产品的介绍,这些文本会被分析,在建立索引文档之前会被分词器进行分词,转化为词组。经过分词机制之后es允许检索到该文本切分而成的词语,但是text类型的数据不能用来过滤、排序和聚合等操作。
    keyword类型的数据可以满足电子邮箱地址、主机名、状态码、邮政编码和标签等数据的要求,不进行分词,常常被用来过滤、排序和聚合。

    综上,可以发现text类型在存储数据的时候会默认进行分词,并生成索引。而keyword存储数据的时候,不会分词建立索引,显然,这样划分数据更加节省内存。

    这样,我们映射了某一个字段为keyword类型之后,就不用设置任何有关分词器的事情了,该类型就是默认不分词的文本数据类型。而对于text类型,我们还可以设置它的分词类型,如下:

PUT /zk_test/info/_mapping
{
  "info":{
    "properties":{
      "address":{"type":"text","analyzer":"standard"}
    }
  }
}

analyzer 还有中文分词 chinese,英文分词 english 等参数

    另外,我们在像之前2.X版本中一样设置分词使用"index":"not_analyzed"配置时,会有提醒,提示"index"参数只有false和true两个值。
在5以上的版本中,“index”参数用来配置该字段是否可以被用来搜索,true可以通过搜索该字段检索到文档,false为否,配置分词器,用analyzer参数。
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/kakaluoteyy/article/details/80324553
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 22:39:56
  • 阅读 ( 1576 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢