Elasticsearch(四)----Elasticsearch中的mapping问题

Mapping在Elasticsearch中是非常重要的一个概念。决定了一个index中的field使用什么数据格式存储,使用什么分词器解析,是否有子字段等

1.问题:为什么要学习Mapping

如果没有mapping所有text类型属性默认都使用standard分词器。所以如果希望使用IK分词就必须配置自定义mapping。

2.mapping核心数据类型

Elasticsearch中的数据类型有很多,在这里只介绍常用的数据类型。
只有text类型才能被分词。其他类型不允许。

文本(字符串):text
整数:byte、short、integer、long
浮点型:float、double
布尔类型:boolean
日期类型:date
数组类型:array  {a:[]}
对象类型:object  {a:{}}
不分词的字符串(关键字): keyword

3.查看索引mapping

可以通过命令查看已有index的mapping具体信息,语法如下:
GET 索引名/_mapping
如:

GET test_index/_mapping
结果:

{
  "test_index": { # 索引名
    "mappings": { # 映射列表
      "test_type": { # 类型名
        "properties": { # 字段列表
          "age": { # 字段名
            "type": "long" # 字段类型
          },
          "gender": {
            "type": "text",
            "fields": { # 子字段列表
              "keyword": { # 子字段名
                "type": "keyword", # 子字段类型,keyword不进行分词处理的文本类型
                "ignore_above": 256 # 子字段存储数据长度
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

4.创建索引时指定mapping

语法:

PUT 索引名称
{
  "mappings":{
    "类型名称":{
      "properties":{
        "字段名":{
          "type":类型,
          ["analyzer":字段的分词器,]
          ["fields":{
            "子字段名称":{
              "type":类型,
              "ignore_above":长度限制
}
}]
}
}
}
}
}

#创建一个用户索引,
#需求:有name(姓名),address(地址),名称不分词,地址使用IK分词器


PUT /user_index
{
  "settings": {
    "number_of_shards": 2, 
    "number_of_replicas": 1
  }
  , "mappings": {
      "user_type":{
        "properties":{
           "name":{
               "type":"keyword"
            },
           "address":{
               "type":"text",
                "analyzer":"ik_max_word"
         }
        }
    }
  }
}

5.为已有索引添加新的字段mapping

语法:

PUT user_index/_mapping/user_type
{
  "properties": {
    "class":{
      "type":"keyword"
    },
    "mather":{
      "type": "text",
      "analyzer": "ik_smart"
      
    }
  }
}

6.测试不同的字段的分词器

#测试索引的field是否可以分词,只能是text类型
GET user_index/_analyze
{
“field”: “address”,
“text”: “我是小明,帅哥一枚,女朋友无数”
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值