zoukankan      html  css  js  c++  java
  • 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的含义,按照官方的文档描述:

    1 Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define:
    2 
    3 > which string fields should be treated as full text fields.
    4 > which fields contain numbers, dates, or geolocations.
    5 > whether the values of all fields in the document should be indexed into the catch-all _all field.
    6 > the format of date values.
    7 > 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:

     1 [root@localhost ~]# curl -XPUT "localhost:9210/test" -d '
     2 {
     3   "settings": {
     4 >    "analysis": {
     5 >       "analyzer": {                         #配置分析器
     6 >          "csh_analyzer": {                  #分析器的名字是csh_analyer,这个是系统没有的,我自己定义的一个,可以取一个不和已有的分析器重名的名字
     7 >              "type": "standard",            #这个分析器的类型是基于系统自带的标准的standard分析器
     8 >              "stopwords": "_english_"       #禁用词,或者说无效词范围定义来自_english_列表
     9 >          }
    10 >       }
    11 >    }
    12 >  },
    13 >  "mappings": {
    14 >     "user": {                              #定义test索引下的一个type为user
    15 >        "properties": {                     #开始定义这个type的属性值(也可以用fields)
    16 >           "first_name": {                  #字段名为first_name
    17 >              "type": "string",             #数据类型为string
    18 >              "analyzer": "standard"       #分析器用系统默认的standard
    19 >           },
    20 >           "last_name": {                   #字段名字为last_name
    21 >               "type": "string",            #字段类型为string
    22 >               "analyzer": "csh_analyzer"   #分析器为自定义的csh_analyzer
    23 >           },
    24 >           "job": {                         #字段名字为job
    25 >               "type": "string",            #字段类型
    26 >               "analyzer": "csh_analyzer"   #分析器为自定义的csh_analyzer
    27 >           }
    28 >        }
    29 >      }
    30 >   }
    31 > }'

    获取索引的mapping信息:

     1 [root@localhost ~]# curl "localhost:9210/test/?mappings&pretty"
     2 {
     3   "test" : {
     4     "aliases" : { },
     5     "mappings" : {
     6       "user" : {
     7         "properties" : {
     8           "first_name" : {
     9             "type" : "string",
    10             "analyzer" : "standard"
    11           },
    12           "job" : {
    13             "type" : "string",
    14             "analyzer" : "csh_analyzer"
    15           },
    16           "last_name" : {
    17             "type" : "string",
    18             "analyzer" : "csh_analyzer"
    19           }
    20         }
    21       }
    22     },
    23     "settings" : {
    24       "index" : {
    25         "creation_date" : "1477538406319",
    26         "uuid" : "czFz6sMzQHSKcvEb4o_yYg",
    27         "analysis" : {
    28           "analyzer" : {
    29             "csh_analyzer" : {
    30               "type" : "standard",
    31               "stopwords" : "_english_"
    32             }
    33           }
    34         },
    35         "number_of_replicas" : "1",
    36         "number_of_shards" : "5",
    37         "version" : {
    38           "created" : "2040199"
    39         }
    40       }
    41     },
    42     "warmers" : { }
    43   }
    44 }

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

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

     1 [root@localhost ~]# curl -XPOST "localhost:9210/test/_analyze?pretty" -d '{
     2 "field": "first_name",
     3 "text": "the shihu"
     4 }'
     5 {
     6   "tokens" : [ {
     7     "token" : "the",
     8     "start_offset" : 0,
     9     "end_offset" : 3,
    10     "type" : "<ALPHANUM>",
    11     "position" : 0
    12   }, {
    13     "token" : "shihu",
    14     "start_offset" : 4,
    15     "end_offset" : 9,
    16     "type" : "<ALPHANUM>",
    17     "position" : 1
    18   } ]
    19 }

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

    再看另外一个测试例子:

     1 [root@localhost ~]# curl -XPOST "localhost:9210/test/_analyze?pretty" -d '{
     2 "field": "last_name",
     3 "text": "the shihu"
     4 }'
     5 {
     6   "tokens" : [ {
     7     "token" : "shihu",
     8     "start_offset" : 4,
     9     "end_offset" : 9,
    10     "type" : "<ALPHANUM>",
    11     "position" : 1
    12   } ]
    13 }

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

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

  • 相关阅读:
    语言只是个工具
    最近学到的一点东西
    iBeacon开发
    马上着手开发Mac应用程序
    Text Kit入门
    Text Kit进阶
    Web Notification
    Objective-C异步编程
    Clang Language Extensions
    黑客与画家
  • 原文地址:https://www.cnblogs.com/shihuc/p/6003596.html
Copyright © 2011-2022 走看看