ES-mapping篇

命令 GET index/type/_mapping 查看某一索引的mapping

个人比较喜欢 将mapping 单独写一个文件,而不是将注解写在bean的属性上

比如新建一个Product 的bean对象 可以这么写:

1.

@Document(indexName = "index_product" ,type = “index_product”,shards = 1,replicas =2)

public class Product{

@Id

private String id;

@Field(type = FieldType.Text, analyzer = "ik_max_word")

private String productName;

@Field(index = false, type = FieldType.Keyword)

private String productInfo;

....

}

 

2.

也可以写在mapping里面,如下: 

@Mapping(mappingPath="/XXX/XXX/index_product_mapping.json")

@Document(indexName = "index_product" ,type = “index_product”,shards = 1,replicas =2)

public class Product{

@Id

private String id;

@Field(type = FieldType.Text, analyzer "ik_max_word")

private String productName;

@Field(index false, type = FieldType.Keyword)

private String productInfo;

....

}

index_product_mapping.json文件

{

  "index_product":{

    "properties":{

      "id":{

        "type":"text"

      }

      “productInfo”:{

        "type":"text",

        "index": false   #index参数作用是控制当前字段是否被索引,默认为true,false表示不记录,即不可被搜索, 

      }

    }

  }

}

如果index是false,则不能被搜索,

GET index_product/index_product/_search { "query": { "match": { "productInfo": "111" } } }  则会报错

 数据类型:

核心数据类型:

字符串型:  text、keyword(不会分词)
数值型:     long、integer、short、byte、double、float、half_float等
日期类型:  date
布尔类型:  boolean
二进制类型: binary
范围类型:  integer_range、float_range、long_range、double_range、date_range


复杂数据类型:

数组类型:array
对象类型:object
嵌套类型:nested object
地理位置数据类型:geo_point、geo_shape
专用类型:ip(记录ip地址)、completion(实现自动补全)、token_count(记录分词数)、murmur3(记录字符串hash值

 multi-fields多字段特性 
允许对同一个字段采用不同的配置,比如分词,常见的例子如对人名实现拼音搜索,只需要在人名中新增一个子字段为pinyin即可。

PUT myindex1
{
"mappings": {
"doc": {
"properties": {
"username": {
"type":"text",
"fields": {
"pinyin": {
"type": "text",
"analyzer": "pinyin"
}
}
}
}
}
}
}

GET myindex1/_search
{
"query": {
"match": {
"username.pinyin": "hanhan"
}
}
}

 

通过dynamic参数来控制字段的新增

  • true:默认值,允许自动新增字段
  • false:不允许自动新增字段,但是文档可以正常写入,但无法对字段进行查询等操作
  • strict:文档不能写入,报错

#定义索引,定义title、name、age三个字段类型,对于其他新增字段dynamic设置为false
PUT myindex
{
"mappings": {
"doc": {
"dynamic": false,
"properties": {
"title": {
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}

#查看刚才自定义的mapping
GET myindex/_mapping

#索引一条文档,字段title、desc,其中desc为新增字段
PUT myindex/doc/1
{
"title": "hello world",
"desc": "nothing"
}

#使用title字段查询,一切正常
GET myindex/_search
{
"query": {
"match": {
"title": "hello"
}
}
}

#无法使用desc字段进行查询,返回为0
GET myindex/_search
{
"query": {
"match": {
"desc": "nothing"
}
}
}

index:控制当前字段是否索引,默认为true,即记录索引,false不记录,即不可搜索

PUT myindex
{
"mappings": {
"doc": {
"properties": {
"cookie": {
"type": "text",
"index":false
}
}
}
}
}
#使用cookie字段查询会报错

 

index_options:用于控制倒排索引记录的内容,有如下4种配置

docs: 只记录doc id
freqs: 记录doc id 和term frequencies
positions: 记录doc id、term frequencies和term position
offsets: 记录doc id、term frequencies、term position和character offsets
text类型默认配置为positions,其他默认为docs 
记录内容越多,占用空间越大。

PUT myindex1
{
"mappings": {
"doc": {
"properties": {
"cookie": {
"type": "text",
"index_options": "offsets"
}
}
}
}
}


null_value: 当字段遇到null值时的处理策略,默认为null,即空值,此时es会忽略该值。可以通过设定该值设定字段的默认值

PUT myindex1
{
"mappings": {
"doc": {
"properties": {
"status_code": {
"type": "keyword",
"null_value": "NULL"
}
}
}
}
}

 

动态字段映射

 

posted @ 2019-07-23 13:16  shigw  阅读(2383)  评论(0编辑  收藏  举报