Elasticsearch【mappings】类型配置操作

时间:2023-03-10 01:25:03
Elasticsearch【mappings】类型配置操作

在介绍ES的更新操作的时候,说过,ES的索引创建是很简单的,没有必要多说,这里是有个前提的,简单是建立在ES默认的配置基础之上的。

比如,当ES安装完毕后,我们就可以通过curl命令完成index,type以及文档的创建。这些创建过程,都是建立在ES的默认配置上的,这里主要说的配置指的是ES的分析器以及数据字段类型。ES的强大之处在于,我们向文档添加字段时,可以不用指定各个field的数据类型,也不用配置这些field在搜索的时候,采用什么analyzer进行分词(ES默认采用的是standard analyzer)。

在进行本博文案例分析前,有必要说下,ES支持的数据类型:

  • 简单数据类型: string, date, long, double,integer,boolean 以及ip等等
  • 层级结构类型:JSON型的object,嵌套类型 (都是JSON)
  • 特殊结构类型:geo_point, geo_shape以及completion。

这些数据类型,可以在创建索引的时候,指定。在此,需要讲解一下mapping的含义,按照官方的文档描述:

 Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define:

 > which string fields should be treated as full text fields.
> which fields contain numbers, dates, or geolocations.
> whether the values of all fields in the document should be indexed into the catch-all _all field.
> the format of date values.
> custom rules to control the mapping for dynamically added fields.

也就是说,这个mapping有点类似我们定义MySQL的数据库表结构的时候,需要指定每个字段的名字,其数据类型一样。当然,这个定义过程,也指明了这个表结构一共含有多少个字段了。对于ES而言,就相当于指定了一个document有多少field,每个field的数据类型,注意,这个比MySQL定义表过程,还多了一个有用的操作,就是指定每个字段可用的分析器(analyzer). 当然,不指定的话,就是采用默认的standard analyzer,当然你也可以指定某个字段不需要分析器(not_analyzed).

下面,再来说说分析器analyzer。

ES系统默认提供了很多的分析器,最著名的是standard analyzer。另外,还有下面的一些分析器,这些分析器,可以进入官网进行深入研究。

这些分析器中,重点在于如何对待搜索的目标进行分词(token)。

下面,将通过一个简单的例子,来说说mapping的操作,以及基于standard analyzer自定义一个自己的分析器csh_analyaer:

 [root@localhost ~]# curl -XPUT "localhost:9210/test" -d '
{
"settings": {
> "analysis": {
> "analyzer": { #配置分析器
> "csh_analyzer": { #分析器的名字是csh_analyer,这个是系统没有的,我自己定义的一个,可以取一个不和已有的分析器重名的名字
> "type": "standard", #这个分析器的类型是基于系统自带的标准的standard分析器
> "stopwords": "_english_" #禁用词,或者说无效词范围定义来自_english_列表
> }
> }
> }
> },
> "mappings": {
> "user": { #定义test索引下的一个type为user
> "properties": { #开始定义这个type的属性值(也可以用fields)
> "first_name": { #字段名为first_name
> "type": "string", #数据类型为string
> "analyzer": "standard"   #分析器用系统默认的standard
> },
> "last_name": { #字段名字为last_name
> "type": "string", #字段类型为string
> "analyzer": "csh_analyzer" #分析器为自定义的csh_analyzer
> },
> "job": { #字段名字为job
> "type": "string", #字段类型
> "analyzer": "csh_analyzer" #分析器为自定义的csh_analyzer
> }
> }
> }
> }
> }'

获取索引的mapping信息:

 [root@localhost ~]# curl "localhost:9210/test/?mappings&pretty"
{
"test" : {
"aliases" : { },
"mappings" : {
"user" : {
"properties" : {
"first_name" : {
"type" : "string",
"analyzer" : "standard"
},
"job" : {
"type" : "string",
"analyzer" : "csh_analyzer"
},
"last_name" : {
"type" : "string",
"analyzer" : "csh_analyzer"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "",
"uuid" : "czFz6sMzQHSKcvEb4o_yYg",
"analysis" : {
"analyzer" : {
"csh_analyzer" : {
"type" : "standard",
"stopwords" : "_english_"
}
}
},
"number_of_replicas" : "",
"number_of_shards" : "",
"version" : {
"created" : ""
}
}
},
"warmers" : { }
}
}

现在,是不是有个疑问,这个mapping或者说里面的settings有什么用呢,其实,主要是在搜索的时候,ES系统内部自己用的。我们不给index指定mapping或不进行settings设置,其实在很多时候也工作的很好。但是,对于操作的数据对象,我们自己了解的信息一定不会比ES系统猜测的信息全和准确。所以,我们自己在工程应用中,最好还是要自己给自己的索引做settings和mappings的设置

看一下例子,针对上面我们建立的索引test,进行测试,看看字段在分词的时候,是不是我们想要的结果:

 [root@localhost ~]# curl -XPOST "localhost:9210/test/_analyze?pretty" -d '{
"field": "first_name",
"text": "the shihu"
}'
{
"tokens" : [ {
"token" : "the",
"start_offset" : ,
"end_offset" : ,
"type" : "<ALPHANUM>",
"position" :
}, {
"token" : "shihu",
"start_offset" : ,
"end_offset" : ,
"type" : "<ALPHANUM>",
"position" :
} ]
}

看到没,上面操作的数据,field是first_name,分析的字符串text为"the shihu", 最后得到的结果是两个分词(token),一个是the,一个是shihu。还记得么,first_name对应的analyzer是standard。而standard分析器分词的依据之一就是把目标内容拆分成一个个的单词,分割器可以是空格,逗号等标点符号,请求看官方文档Standard Analyzer

再看另外一个测试例子:

 [root@localhost ~]# curl -XPOST "localhost:9210/test/_analyze?pretty" -d '{
"field": "last_name",
"text": "the shihu"
}'
{
"tokens" : [ {
"token" : "shihu",
"start_offset" : ,
"end_offset" : ,
"type" : "<ALPHANUM>",
"position" :
} ]
}

这个操作中的field是last_name,分析的字符串依然是“the shihu”,但是最后看到的结果中只有一个token,只有shihu,没有了the这个词。区别在于这个last_name的analyzer是用的自定义的csh_analyzer,而这个是基于standard的自定义分析器,stopwords改成了_english_,这个里面应该是包含了the这种英文分词常见的词,这个词是大概率出现的词,按照信息论的概念,信息量与概率的大小成反比,所以the这种大概率的词含有很小的信息量,通常不作为搜索返回结果,所以在分词列表中去掉了

到此,ES的mappings相关的介绍,就抛砖结束了,其间顺便也讲解了点分析器,希望对理解ES工作原理的伙伴有点帮助!