ES数据管理
索引操作
创建索引库
PUT /索引名称
{
"settings": {
"属性名": "属性值"
}
}
- settings:就是索引库设置,其中可以定义索引库的各种属性,比如分片数,副本数等。

PUT /bntang_index
{
"settings": {}
}
判断索引是否存在
HEAD /索引名称

查看索引
GET /索引名称

批量查看索引
GET /索引名称1,索引名称2,索引名称3,...

查看所有索引
GET _all

GET /_cat/indices?v

绿色:索引的所有分片都正常分配。黄色:至少有一个副本没有得到正确的分配。红色:至少有一个主分片没有得到正确的分配。
打开索引
POST /索引名称/_open

关闭索引
POST /索引名称/_close

删除索引库
DELETE /索引名称1,索引名称2,索引名称3,...

映射操作
也就是相当于操作,数据库-表-字段-约束信息,索引-_doc-字段映射,设置字段的约束信息,叫做字段映射。
创建映射字段
先要创建索引库, 再创建映射。
PUT /索引库名/_mapping
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}

PUT /my_index
PUT /my_index/_mapping
{
"properties": {
"name": {
"type": "text",
"index": true,
"store": true,
"analyzer": "ik_max_word"
}
}
}
GET /my_index
- type:类型,可以是
text、long、short、date、integer、object等。- String:text 可分词,keyword 不可分词。
- Date
- Array
- Object
- index:是否索引,默认为
true。 - store:是否存储,默认为
false,原始文本存储_source。 - analyzer:指定分词器。
官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
查看映射关系
GET /索引名称/_mapping

查看所有索引映射关系
GET _mapping

GET _all/_mapping

修改索引映射关系
PUT /索引库名/_mapping
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}

PUT /my_index/_mapping
{
"properties": {
"age": {
"type": "long",
"index": true,
"store": true
}
}
}
GET my_index/_mapping
一次性创建索引和映射
PUT /索引库名称
{
"settings": {
"索引库属性名": "索引库属性值"
},
"mappings": {
"properties": {
"字段名": {
"映射属性名": "映射属性值"
}
}
}
}

PUT /bntang_index
{
"settings": {},
"mappings": {
"properties": {
"name": {
"type": "text"
}
}
}
}
GET /bntang_index/_mapping
文档操作
文档,即索引库中的数据,会根据规则创建索引,将来用于搜索,相当于数据库当中的一条记录。
新增文档(自己指定 id)
POST /索引名称/_doc/{id}

POST /my_index/_doc/1
{
"name": "BNTang",
"age": 18
}
新增文档(自动生成 id)
POST /索引名称/_doc
{
"field": "value"
}

POST /my_index/_doc
{
"name": "Jonathan_Lee"
}
查看单个文档
GET /索引名称/_doc/{id}

GET /my_index/_doc/0pSqSX4BoEG1qzQhAUbA
查看所有文档
POST /索引名称/_search
{
"query": {
"match_all": {}
}
}

POST /my_index/_search
{
"query": {
"match_all": {}
}
}
更新文档
使用 PUT 指定 id 进行更新,id 对应文档存在,则修改,id 对应文档不存在,则新增。
POST /索引名/_update/{id}
{
"doc": {
"field": "value"
}
}
不存在则新增:
PUT /my_index/_doc/2
{
"name": "BNTang"
}

存在则修改:
PUT /my_index/_doc/2
{
"name": "BNTang2"
}

局域更新,只是修改某个字段(使用 POST)

POST /my_index/_update/1
{
"doc": {
"name": "唐"
}
}
全部更新,是直接把之前的老数据,标记为删除状态,然后,再添加一条更新的(使用 PUT 或者 POST)
根据 id 进行删除
DELETE /索引名/_doc/{id}

DELETE /my_index/_doc/2
根据查询条件进行删除
POST /索引库名/_delete_by_query
{
"query": {
"match": {
"字段名": "搜索关键字"
}
}
}

POST /my_index/_delete_by_query
{
"query": {
"match": {
"name": "BNTang"
}
}
}
删除所有文档
POST 索引名/_delete_by_query
{
"query": {
"match_all": {}
}
}

POST /my_index/_delete_by_query
{
"query": {
"match_all": {}
}
}
强制创建
PUT /index/_doc/{id}/_create
{
}

PUT /my_index/_doc/2/_create
{
}
如果 id 存在就会报错:

查询操作
添加示例数据:

ctrl + a 全选左侧命令一键执行:
PUT /my_index01/_doc/1
{
"name":"zs",
"sex": 1,
"age": 24,
"hobby": "篮球"
}
PUT /my_index01/_doc/2
{
"name":"ls",
"sex": 0,
"age": 25,
"hobby": "足球"
}
PUT /my_index01/_doc/3
{
"name":"ww",
"sex": 0,
"age": 26,
"hobby": "足球"
}
PUT /my_index01/_doc/4
{
"name":"zl",
"sex": 0,
"age": 24,
"hobby": "乒乓球"
}
PUT /my_index01/_doc/5
{
"name":"zq",
"sex": 0,
"age": 18,
"hobby": "羽毛球"
}
查询当前类型中的所有文档 _search
GET /索引名称/类型/_search

GET /my_index01/_doc/_search
条件查询
GET /索引名称/类型/_search?q=*:***

GET /my_index01/_doc/_search?q=age:24
范围查询
GET /索引名称/类型/_search?q=***[25 TO 26]

GET /my_index01/_doc/_search?q=age[24 TO 26]
根据多个 ID 进行批量查询
GET /索引名称/类型/_mget

GET /my_index01/_doc/_mget
{
"ids": ["1", "2"]
}
查询年龄小于等于 24 岁
GET /索引名称/类型/_search?q=age:<=**

GET /my_index01/_doc/_search?q=age:<=24
查询年龄大于 24 的
GET /索引名称/类型/_search?q=age:>**

GET /my_index01/_doc/_search?q=age:>24
分页查询
相当于 SQL 中的 limit 0, 1:
GET /索引名称/类型/_search?q=age:>**&from=**&size=**

GET /my_index01/_doc/_search?q=age:>18&from=0&size=2
_source 定制返回结果
GET /索引名称/_doc/id?_source=file1,file2

GET /my_index01/_doc/1?_source=name,age

GET /my_index01/_doc/_search?_source=name,age&q=age:>18&from=0&size=3
对查询结果排序
GET /索引名称/类型/_search?sort=字段 desc

GET /my_index01/_doc/_search?_source=name,age&q=age:>18&from=0&size=4&sort=age:desc

浙公网安备 33010602011771号