ElasticSearch映射 Mapping

谁践踏了优雅 2022-02-24 07:22 451阅读 0赞

ElasticSearch映射 Mapping

为了能够把日期字段处理成日期, 把数字字段处理成数字, 把字符串字段处理成全文本( Full-text) 或精确的字符串值, Elasticsearch需要知道每个字段里面都包含了什么类型。 这些类型和字段的信息存储( 包含) 在映射( mapping) 中。
索引中每个文档都有一个类型(type)。 每个类型拥有自己的映射(mapping)或者模式定义(schema definition)。 一个映射定义了字段类型, 每个字段的数据类型, 以及字段被Elasticsearch处理的方式。 映射还用于设置关联到类型上的元数据。

我们首先添加几个文档

  1. PUT /myindex/article/1
  2. {
  3. "post_date": "2019-03-10",
  4. "title": "Java",
  5. "content": "java is the best language",
  6. "author_id": 119
  7. }
  8. PUT /myindex/article/2
  9. {
  10. "post_date": "2019-04-03",
  11. "title": "html",
  12. "content": "I like html",
  13. "author_id": 120
  14. }
  15. PUT /myindex/article/3
  16. {
  17. "post_date": "2019-03-16",
  18. "title": "es",
  19. "content": "Es is distributed document store",
  20. "author_id": 110
  21. }

1.查询包含”es”的文档

  1. get myindex/article/_search?q=es
  2. 查询结果
  3. {
  4. "took": 11,
  5. "timed_out": false,
  6. "_shards": {
  7. "total": 5,
  8. "successful": 5,
  9. "skipped": 0,
  10. "failed": 0
  11. },
  12. "hits": {
  13. "total": 1,
  14. "max_score": 0.2876821,
  15. "hits": [
  16. {
  17. "_index": "myindex",
  18. "_type": "article",
  19. "_id": "3",
  20. "_score": 0.2876821,
  21. "_source": {
  22. "post_date": "2019-03-16",
  23. "title": "es",
  24. "content": "Es is distributed document store",
  25. "author_id": 110
  26. }
  27. }
  28. ]
  29. }
  30. }

从结果中我们看到查到了一条记录。

2.查询包含以”e”开头的文档

  1. get myindex/article/_search?q=e*

查询结果和1的基本相同

3.查询包含”2019-04-03”

  1. get myindex/article/_search?q=2019-04-03
  2. {
  3. "took": 14,
  4. "timed_out": false,
  5. "_shards": {
  6. "total": 5,
  7. "successful": 5,
  8. "skipped": 0,
  9. "failed": 0
  10. },
  11. "hits": {
  12. "total": 1,
  13. "max_score": 1,
  14. "hits": [
  15. {
  16. "_index": "myindex",
  17. "_type": "article",
  18. "_id": "2",
  19. "_score": 1,
  20. "_source": {
  21. "post_date": "2019-04-03",
  22. "title": "html",
  23. "content": "I like html",
  24. "author_id": 120
  25. }
  26. }
  27. ]
  28. }
  29. }

从结果中我们看到查到了一条记录。

4.查询包含以”2019-04-0*“开头的文档

  1. get myindex/article/_search?q=2019-04-0*
  2. 查询结果
  3. {
  4. "took": 8,
  5. "timed_out": false,
  6. "_shards": {
  7. "total": 5,
  8. "successful": 5,
  9. "skipped": 0,
  10. "failed": 0
  11. },
  12. "hits": {
  13. "total": 0,
  14. "max_score": null,
  15. "hits": []
  16. }
  17. }

从结果中可以看到,并没有查到对应的文档,”2019-04-0*“匹配不了”2019-04-03”,这是为什么?

想知道为什么,就去看一下Mapping

  1. get myindex/_mapping
  2. {
  3. "myindex": {
  4. "mappings": {
  5. "article": {
  6. "properties": {
  7. "author_id": {
  8. "type": "long"
  9. },
  10. "content": {
  11. "type": "text",
  12. "fields": {
  13. "keyword": {
  14. "type": "keyword",
  15. "ignore_above": 256
  16. }
  17. }
  18. },
  19. "post_date": {
  20. "type": "date"
  21. },
  22. "title": {
  23. "type": "text",
  24. "fields": {
  25. "keyword": {
  26. "type": "keyword",
  27. "ignore_above": 256
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }

从结果中可以看到 “post_date”: { “type”: “date” },也就是说post_date这个字段是date类型的,es自动创建了index,type,以及type对应的mapping(dynamic mapping),用字符串匹配date类型肯定是匹配不到的。

映射:mapping定义了type中的每个字段的数据类型以及这些字段如何分词等相关属性

创建索引的时候,可以预先定义字段的类型以及相关属性,这样就能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理字符串值等。

支持的数据类型:

(1)核心数据类型(Core datatypes)

  1. 字符型:stringstring类型包括
  2. text keyword
  3. text类型被用来索引长文本,在建立索引前会将这些文本进行分词,转化为词的组合,建立索引。允许es来检索这些词语。text类型不能用来排序和聚合。
  4. Keyword类型不需要进行分词,可以被用来检索过滤、排序和聚合。keyword 类型字段只能用本身来进行检索
  5. 数字型:long, integer, short, byte, double, float
  6. 日期型:date
  7. 布尔型:boolean
  8. 二进制型:binary

(2)复杂数据类型(Complex datatypes)

  1. 数组类型(Array datatype):数组类型不需要专门指定数组元素的type,例如:
  2. 字符型数组: \[ "one", "two" \]
  3. 整型数组:\[ 1, 2 \]
  4. 数组型数组:\[ 1, \[ 2, 3 \]\] 等价于\[ 1, 2, 3 \]
  5. 对象数组:\[ \{ "name": "Mary", "age": 12 \}, \{ "name": "John", "age": 10 \}\]
  6. 对象类型(Object datatype):\_ object \_ 用于单个JSON对象;
  7. 嵌套类型(Nested datatype):\_ nested \_ 用于JSON数组;

(3)地理位置类型(Geo datatypes)

  1. 地理坐标类型(Geo-point datatype):\_ geo\_point \_ 用于经纬度坐标;
  2. 地理形状类型(Geo-Shape datatype):\_ geo\_shape \_ 用于类似于多边形的复杂形状;

(4)特定类型(Specialised datatypes)

  1. IPv4 类型(IPv4 datatype):\_ ip \_ 用于IPv4 地址;
  2. Completion 类型(Completion datatype):\_ completion \_提供自动补全建议;
  3. Token count 类型(Token count datatype):\_ token\_count \_ 用于统计做了标记的字段的index数目,该值会一直增加,不会因为过滤条件而减少。
  4. mapper-murmur3
  5. 类型:通过插件,可以通过 \_ murmur3 \_ 来计算 index hash 值;
  6. 附加类型(Attachment datatype):采用 mapper-attachments
  7. 插件,可支持\_ attachments \_ 索引,例如 Microsoft Office 格式,Open Document 格式,ePub, HTML 等。

支持的属性:

“store”:false//是否单独设置此字段的是否存储而从_source字段中分离,默认是false,只能搜索,不能获取值

“index”: true//分词,不分词是:false,设置成false,字段将不会被索引

“analyzer”:”ik”//指定分词器,默认分词器为standard analyzer

“boost”:1.23//字段级别的分数加权,默认值是1.0

“doc_values”:false//对not_analyzed字段,默认都是开启,分词字段不能使用,对排序和聚合能提升较大性能,节约内存

“fielddata”:{“format”:”disabled”}//针对分词字段,参与排序或聚合时能提高性能,不分词字段统一建议使用doc_value

“fields”:{“raw”:{“type”:”string”,”index”:”not_analyzed”}} //可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词

“ignore_above”:100 //超过100个字符的文本,将会被忽略,不被索引

“include_in_all”:ture//设置是否此字段包含在_all字段中,默认是true,除非index设置成no选项

“index_options”:”docs”//4个可选参数docs(索引文档号),freqs(文档号+词频),positions(文档号+词频+位置,通常用来距离查询),offsets(文档号+词频+位置+偏移量,通常被使用在高亮字段)分词字段默认是position,其他的默认是docs

“norms”:{“enable”:true,”loading”:”lazy”}//分词字段默认配置,不分词字段:默认{“enable”:false},存储长度因子和索引时boost,建议对需要参与评分字段使用 ,会额外增加内存消耗量

“null_value”:”NULL”//设置一些缺失字段的初始化值,只有string可以使用,分词字段的null值也会被分词

“position_increament_gap”:0//影响距离查询或近似查询,可以设置在多值字段的数据上火分词字段上,查询时可指定slop间隔,默认值是100

“search_analyzer”:”ik”//设置搜索时的分词器,默认跟ananlyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能

“similarity”:”BM25”//默认是TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效

“term_vector”:”no”//默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量) 对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用

映射的分类:

(1)动态映射:

当ES在文档中碰到一个以前没见过的字段时,它会利用动态映射来决定该字段的类型,并自动地对该字段添加映射。

可以通过dynamic设置来控制这一行为,它能够接受以下的选项:

  1. true:默认值。动态添加字段
  2. false:忽略新字段
  3. strict:如果碰到陌生字段,抛出异常

dynamic设置可以适用在根对象上或者object类型的任意字段上。

自己建个索引试一下

  1. put /test1
  2. {
  3. "settings":{
  4. "number_of_shards":3,
  5. "number_of_replicas":0
  6. },
  7. "mappings":{
  8. "books":{
  9. "properties":{
  10. "title":{"type":"text"},
  11. "name":{"type":"text","index":false},
  12. "publish_date":{"type":"date","index":false},
  13. "price":{"type":"double"},
  14. "number":{"type":"integer"}
  15. }
  16. }
  17. }
  18. }
  19. put test2
  20. {
  21. "settings":{
  22. "number_of_shards" : 3,
  23. "number_of_replicas" : 0
  24. },
  25. "mappings":{
  26. "books":{
  27. "properties":{
  28. "title":{"type":"text"},
  29. "name":{"type":"text","index":false},
  30. "publish_date":{"type":"date","index":false},
  31. "price":{"type":"double"},
  32. "number":{
  33. "type":"object",
  34. "dynamic":true
  35. }
  36. }
  37. }
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,451人围观)

还没有评论,来说两句吧...

相关阅读