目录
索引库操作
索引库属性解释
mappings:是对索引库中文档的约束;常见属性如下:
- type:数据类型
- index:是否索引
- analyzer:分词器
- properties:子字段
type: 字段数据类型,常见的类型有:
- 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
- 数值:long、integer、short、byte、double、float
- 布尔:boolean
- 日期:date
- 对象:object
index:是否创建索引,默认为true(需要根据某个字段查询,默认为true;不需要查询当前字段设置为false)
analyzer:使用哪种分词器(只有text值需要分词,其他基本不需要要)
properties:该字段的子字段
DSL创建索引库
ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。
语法
PUT /索引库名称 { "mappings": { "properties": { "字段名1": { "type": "text", "analyzer": "ik_smart" }, "字段名2": { "type": "keyword", "index": "false" }, "字段名3": { "properties": { "子字段": { "type": "keyword" } } }, // ...省略 } } }
示例
PUT /user { "mappings": { "properties":{ "info": { "type": "text", "analyzer": "ik_smart" }, "name": { "type": "keyword", }, "email": { "type": "keyword", "index": false }, "address": { "type": "object", "properties": { "province": { "type": "keyword" }, "city": { "type": "keyword" }, "district": { "type": "keyword" } } } } } }
DSL修改索引库
修改索引库和mapping一旦创建无法修改,但是可以添加新的字段。
语法
PUT /索引库名/_mapping { "properties": { "新字段名": { "type": "integer" } } }
示例
PUT /user/_mapping { "properties": { "age": { "type": "integer" } } }
DSL查看索引库
语法
GET /索引库名
示例
GET /user
DSL删除索引库
语法
DELETE /索引库名
示例
DELETE /user
文档操作
DSL新增文档
语法
POST /索引库名/_doc/文档id { "字段1": "字段值1", "字段2": "字段值2", "字段3": { "子属性1": "值3", "子属性2": "值4" }, // 省略 }
示例
POST /user/_doc/1 { "info": "一个小可爱呀", "email": "xka@xiaokeai.cn", "addrees": { "province": "广东省", "city": "广州市", "district": "天河区" } }
DSL查看文档
语法
GET /索引库名/_doc/文档id
示例
GET /user/_doc/1
DSL删除文档
语法
DELETE /索引库名/_doc/文档id
示例
DELETE /user/_doc/1
DSL修改文档
方法一:全量修改,会删除旧文档,添加新文档(输入文档内所有的值)
语法
PUT /索引库名/_doc/文档id { "字段1": "值1", "字段2": "值2", // ... 省略 }
示例
PUT /user/_doc/1 { "info": "一个小小可爱呀", "email": "xxka@xiaokeai.cn", "addrees": { "province": "河南省", "city": "郑州", "district": "中原区" } }
方式二:增量修改,修改指定字段值
语法
POST /索引库名/_update/文档id { "doc": { "字段名": "新的值", "字段名2": "新的值2", // ... 省略 } }
示例
POST /user/_update/文档id { "doc": { "email": "kljj@kailangjiaojiao.cn" } }