0
点赞
收藏
分享

微信扫一扫

008-slot插槽

索引库操作

1、mapping映射属性

可以查看官方文档学习:ES官方手册

mapping是对索引库中文档的约束,常见的mapping属性包括:

  • type:字段数据类型,常见的简单类型有:

    • 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)

    • 数值:long、integer、short、byte、double、float、

    • 布尔:boolean

    • 日期:date

    • 对象:object
  • index:是否创建索引,默认为true
  • analyzer:使用哪种分词器(绝大多数情况下分词text即可)
  • properties:该字段的子字段

2、创建索引库

ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。创建索引库和mapping的DSL语法如下:

PUT /索引库名称
{
  "mappings": {
    "properties": {
      "字段名":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "字段名2":{
        "type": "keyword",
        "index": "false"
      },
      "字段名3":{
        "properties": {
          "子字段": {
            "type": "keyword"
          }
        }
      },
      // ...略
    }
  }
}

例如:

# 创建名为rediaz的索引库
PUT /rediaz
{
  "mappings": {
    "properties": {
      "info": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email": {
        "type": "keyword",
        "index": false  //不会创建索引,不会被搜索
      },
      "name": {
        "type": "object",
        "properties": {
          "firstname": {
            "type": "keyword"
          },
          "lastname": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

索引库创建结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "rediaz"
}

3、查询、删除和修改索引库

  1. 查询:

    GET /索引库名
    

    示例:

    GET /rediaz
    
  2. 删除:

    DELETE /索引库名
    

    示例:

    DELETE /rediaz
    
  3. 修改:

    在ES中禁止修改索引库:在索引库创建成功之后,其数据结构(即mapping映射)已经创建好了,ES会基于mapping来创建倒排索引,如果修改索引库,则会将ES库中的所有倒排索引失效。

    但是可以添加新的字段:

    PUT /索引库名/_mapping
    {
    "properties" : {
      "新字段名":{
        "type" : "integer "}
      }
    }
    

    示例:

    PUT /rediaz/_mapping
    {
    "properties" : {
      "age":{
        "type" : "integer"}
      }
    }
    
举报

相关推荐

0 条评论