第十二篇 elasticsearch中的mapping透彻理解

首先插入几条数据,让es自动为我们建立一个索引

PUT /website/article/1
{
  "post_date": "2017-01-01",
  "title": "my first article",
  "content": "this is my first article in this website",
  "author_id": 11400
}

PUT /website/article/2
{
  "post_date": "2017-01-02",
  "title": "my second article",
  "content": "this is my second article in this website",
  "author_id": 11400
}

PUT /website/article/3
{
  "post_date": "2017-01-03",
  "title": "my third article",
  "content": "this is my third article in this website",
  "author_id": 11400
}

尝试各个搜索

GET /website/article/_search?q=2017  //查出3条结果             
GET /website/article/_search?q=2017-01-01 //也查出3条结果
GET /website/article/_search?q=post_date:2017-01-01  //查出1条结果
GET /website/article/_search?q=post_date:2017  //查出1条结果

引出一个问题:为什么搜索结果会不一致
mapping,就是index的type的元数据,每个type都有一个自己的mapping,决定了数据类型,建立倒排索引的行为,还有进行搜索的行为

对mapping的透彻理解

(1)往es里面直接插入数据,es会自动建立索引,同时建立type以及对应的mapping
(2)mapping中就自动定义了每个field的数据类型
(3)不同的数据类型(比如说text和date),可能有的是exact value,有的是full text
(4)exact value,在建立倒排索引的时候,分词的时候,是将整个值一起作为一个关键词建立到倒排索引中的;full text,会经历各种各样的处理,分词,normaliztion(时态转换,同义词转换,大小写转换,单复数转换),才会建立到倒排索引中
(5)同时呢,exact value和full text类型的field就决定了,在一个搜索过来的时候,对exact value field或者是full text field进行搜索的行为也是不一样的,会跟建立倒排索引的行为保持一致;比如说exact value搜索的时候,就是直接按照整个值进行匹配,full text query string,也会进行分词和normalization再去倒排索引中去搜索
(6)可以用es的dynamic mapping,让其自动建立mapping,包括自动设置数据类型;也可以提前手动创建index和type的mapping,自己对各个field进行设置,包括数据类型,包括索引行为,包括分词器,等等

mapping的核心数据类型

String、byte、short、intege、long、float、double、boolean、date

dynamic mapping

true or false –> boolean
123 –> long
123.45 –> double
2017-01-01 –> date
“hello world” –> string/tex

查看mapping
GET /index/_mapping/type

猜你喜欢

转载自blog.csdn.net/r_p_j/article/details/78405443