搜索
您的当前位置:首页正文

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

    这样,我们映射了某一个字段为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参数。
--------------------- 
原文:https:///kakaluoteyy/article/details/80324553 
 

因篇幅问题不能全部显示,请点此查看更多更全内容

Top