ElasticSearch索引库操作

1.mapping映射属性

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

  • type:字段数据类型,常见的简单类型有:
    • 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
    • 数值:long、integer、short、byte、double、float、
    • 布尔:boolean
    • 日期:date
    • 对象:object
  • index:是否创建索引,默认为true
  • analyzer:使用哪种分词器
  • properties:该字段的子字段

2.索引库的CRUD

本文章统一使用postMan编写DSL的方式来演示。

2.1. 创建索引库

基本语法:

请求方式:PUT

请求路径:/索引库名,可以自定义

请求参数:mapping映射

格式:

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

示例:

在请求中定义索引库名为teacher

{
    "mappings": {
        "properties": {
            "age":{
                "type":"integer"
            },
            "weight": {
                "type": "integer"
            },
            "isMarried": {
                "type": "boolean"
            },
            "info": {
                "type": "text"
            },
            "email": {
                "type": "keyword"
            },
            "score": {
                "type": "float"
            },
            "name":{
                "properties":{
                    "firstName":{
                        "type": "keyword"
                    },
                    "lastName": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

运行结果:

 2.2.查询数据库

基本语法:

  • 请求方式:GET
  • 请求路径:/索引库名
  • 请求参数:无

格式:

GET/索引库名

示例:

运行结果:

{
    "teacher": {
        "aliases": {},
        "mappings": {
            "properties": {
                "age": {
                    "type": "integer"
                },
                "email": {
                    "type": "keyword"
                },
                "info": {
                    "type": "text"
                },
                "isMarried": {
                    "type": "boolean"
                },
                "name": {
                    "properties": {
                        "firstName": {
                            "type": "keyword"
                        },
                        "lastName": {
                            "type": "keyword"
                        }
                    }
                },
                "score": {
                    "type": "float"
                },
                "weight": {
                    "type": "integer"
                }
            }
        },
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "teacher",
                "creation_date": "1727602068299",
                "number_of_replicas": "1",
                "uuid": "tmKL0I6QTeyT1d8ItftmQQ",
                "version": {
                    "created": "7160299"
                }
            }
        }
    }
}

2.3修改索引库

        倒排索引结构虽然不复杂,但是一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改mapping。
        虽然无法修改mapping中已有的字段,但是却允许添加新的字段到mapping中,因为不会对倒排索引产生影响。

语法:

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

2.4删除索引库

语法:

  • 请求方式:DELETE
  • 请求路径:/索引库名
  • 请求参数:无

格式:

        DELETE/索引库名

示例:

运行结果:

2.5总结

  • 创建索引库:PUT /索引库名
  • 查询索引库:GET /索引库名
  • 删除索引库:DELETE /索引库名
  • 添加字段:PUT /索引库名/_mapping
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值