Elasticsearch Mapping是啥?

什么是Mapping

我们知道,es如果对应数据表,表中的数据是不是有数据类型,那么esmapping就是来设置这个字段类型的。它的主要作用:

  1. 定义字段名称
  2. 定义字段的数据类型,例如字符串、数值等
  3. 字段 倒排索引的相关配置,比如说可以通过配置字段是否需要被索引
  • Mapping 会把 Json 文档映射成 Lucene 所需的扁平格式
  • 一个 Mapping 属于一个索引的 Type ,在 7.0 之后版本索引只有一个 Type(_doc)

常用来设置 Mapping 的数据类型

  • 简单类型
  1. Text/Keyword
  2. Date
  3. Integer/Float/Double/Long
  4. Boolean
  5. Ip

这里说明一下TextKetword类型的区别

在es5之前是string,后面拆分成了Text和Keyword

按照官方文档的阐述,text类型的数据用来索引长文本,例如电子邮箱主体部分或者一些产品的介绍,这些文本会被分析,在建立索引后被分词器进行分词,转化为词组。经过分词机制后es允许检索到该文本切分而成的词语,但text类型的数据不能用来做过滤、排序、聚合等操作

keyword类型的数据可以满足电子邮箱、主机名、状态码等数据的要求,不进行分词,常常被用来做过滤、排序、聚合等操作

  • 复杂类型-对象和嵌套对象

对象类型/嵌套类型

  • 特殊类型(针对地理位置信息有特殊处理)
  1. geo_point
  2. geo_shape / percolator

Dynamic Mapping

简单来说,如果你不手动创建Mappinges会自动根据json来推断数据类型,但是不准确,这个的话我一般不会自动映射,所以大家知道一下这个就ok

手动创建 Mapping

PUT phone
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "cpu": {
        "type": "text"
      },
      "created_at": {
        "type": "date"
      },
      "system_code": {
        "type": "integer",
        "index": false
      }
    }
  }
}

POST phone/_doc
{
  "name": "苹果",
  "cpu": "4核",
  "created_at": "2019-11-01",
  "system_code": 111 
}

POST phone/_doc
{
  "name": "华为",
  "cpu": "4核",
  "created_at": "2020-11-01",
  "system_code": 221
}


GET phone/_search
{
  "query": {
    "match": {
      "system_code": {
        "query": 111
      }
    }
  }
}

上面建立mapping的时候,我对system_code这个字段index设置为falsees将不会对这个字段建立倒排索引

index-false

Index Options

ES 有四种不同级别的 Index Options 配置

  • docs 记录 doc id
  • freqs 记录 doc id 和 term 频次
  • positions 记录 doc id 和 term 频次 和 term 位置
  • offsets 记录 doc id 和 term 频次 和 term 位置和字符偏移量

Text 类型默认 positions,其他默认为 docs

copy_to

copy_to 是为瞒足一些特定搜素需求,将多个字段 数值拷贝到目标字段,目标字段不会出现在 _source

PUT phone_1
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "copy_to": "fullname"
      },
      "cpu": {
        "type": "text",
        "copy_to": "fullname"
      },
      "created_at": {
        "type": "date"
      },
      "system_code": {
        "type": "integer",
        "index": false
      }
    }
  }
}



POST phone_1/_doc
{
  "name": "苹果",
  "cpu": "4核",
  "created_at": "2019-11-01",
  "system_code": 111 
}

POST phone_1/_doc
{
  "name": "华为",
  "cpu": "4核",
  "created_at": "2020-11-01",
  "system_code": 221
}


GET phone_1/_search
{
  "query": {
    "match": {
      "fullname": {
        "query": "华为4核"
      }
    }
  }
}

查询的时候,fullname并没有在mapping中声明,照样可以进行合并搜索

copy_to

您的支持是对我最大的鼓励!

发表于: 作者:憧憬。
关注互联网以及分享全栈工作经验的原创个人博客和技术博客,热爱编程,极客精神
Github 新浪微博 SegmentFault 掘金专栏