ES数据管理

索引操作

创建索引库

PUT /索引名称
{
  "settings": {
    "属性名": "属性值"
  }
}
  • settings:就是索引库设置,其中可以定义索引库的各种属性,比如分片数,副本数等。

image-20220111202254963

PUT /bntang_index
{
  "settings": {}
}

判断索引是否存在

HEAD /索引名称

image-20220111202352080

查看索引

GET /索引名称

image-20220111202424753

批量查看索引

GET /索引名称1,索引名称2,索引名称3,...

image-20220111202516641

查看所有索引

GET _all

image-20220111202725471

GET /_cat/indices?v

image-20220111202806799

  • 绿色:索引的所有分片都正常分配。
  • 黄色:至少有一个副本没有得到正确的分配。
  • 红色:至少有一个主分片没有得到正确的分配。

打开索引

POST /索引名称/_open

image-20220111202928608

关闭索引

POST /索引名称/_close

image-20220111203008358

删除索引库

DELETE /索引名称1,索引名称2,索引名称3,...

image-20220111203058552

映射操作

也就是相当于操作,数据库-表-字段-约束信息,索引-_doc-字段映射,设置字段的约束信息,叫做字段映射。

创建映射字段

先要创建索引库, 再创建映射。

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

image-20220111203737403

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

image-20220111204214446

查看所有索引映射关系

GET _mapping

image-20220111204538257

GET _all/_mapping

image-20220111204616813

修改索引映射关系

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

image-20220111204733783

PUT /my_index/_mapping
{
  "properties": {
    "age": {
      "type": "long", 
      "index": true,
      "store": true
    } 
  }
}

GET my_index/_mapping

一次性创建索引和映射

PUT /索引库名称
{
  "settings": {
    "索引库属性名": "索引库属性值"
  },
  "mappings": {
    "properties": {
      "字段名": {
        "映射属性名": "映射属性值"
      }
    }
  }
}

image-20220111205031651

PUT /bntang_index
{
  "settings": {},
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}

GET /bntang_index/_mapping

文档操作

文档,即索引库中的数据,会根据规则创建索引,将来用于搜索,相当于数据库当中的一条记录。

新增文档(自己指定 id)

POST /索引名称/_doc/{id}

image-20220111205318560

POST /my_index/_doc/1
{
  "name": "BNTang",
  "age": 18
}

新增文档(自动生成 id)

POST /索引名称/_doc
{
  "field": "value"
}

image-20220111205415413

POST /my_index/_doc
{
  "name": "Jonathan_Lee"
}

查看单个文档

GET /索引名称/_doc/{id}

image-20220111230645370

GET /my_index/_doc/0pSqSX4BoEG1qzQhAUbA

查看所有文档

POST /索引名称/_search
{
  "query": {
    "match_all": {}
  }
}

image-20220111230729648

POST /my_index/_search
{
  "query": {
    "match_all": {}
  }
}

更新文档

使用 PUT 指定 id 进行更新,id 对应文档存在,则修改,id 对应文档不存在,则新增。

POST /索引名/_update/{id}
{
  "doc": {
    "field": "value"
  }
}

不存在则新增:

PUT /my_index/_doc/2
{
  "name": "BNTang"
}

image-20220111231317975

存在则修改:

PUT /my_index/_doc/2
{
  "name": "BNTang2"
}

image-20220111231327726

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

image-20220111230947385

POST /my_index/_update/1
{
  "doc": {
    "name": "唐"
  }
}

全部更新,是直接把之前的老数据,标记为删除状态,然后,再添加一条更新的(使用 PUT 或者 POST)

根据 id 进行删除

DELETE /索引名/_doc/{id}

image-20220111231835744

DELETE /my_index/_doc/2

根据查询条件进行删除

POST /索引库名/_delete_by_query
{
  "query": {
    "match": {
      "字段名": "搜索关键字"
    }
  }
}

image-20220111231924502

POST /my_index/_delete_by_query
{
  "query": {
    "match": {
      "name": "BNTang"
    }
  }
}

删除所有文档

POST 索引名/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

image-20220112085405504

POST /my_index/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

强制创建

PUT /index/_doc/{id}/_create
{
}

image-20220112085634292

PUT /my_index/_doc/2/_create
{
}

如果 id 存在就会报错:

image-20220112085702285

查询操作

添加示例数据:

image-20220112090000536

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": "羽毛球"
}
GET /索引名称/类型/_search

image-20220112090155016

GET /my_index01/_doc/_search

条件查询

GET /索引名称/类型/_search?q=*:***

image-20220112090327809

GET /my_index01/_doc/_search?q=age:24

范围查询

GET /索引名称/类型/_search?q=***[25 TO 26]

image-20220112090500184

GET /my_index01/_doc/_search?q=age[24 TO 26]

根据多个 ID 进行批量查询

GET /索引名称/类型/_mget

image-20220112090638165

GET /my_index01/_doc/_mget
{
  "ids": ["1", "2"]
}

查询年龄小于等于 24 岁

GET /索引名称/类型/_search?q=age:<=**

image-20220112090752634

GET /my_index01/_doc/_search?q=age:<=24

查询年龄大于 24 的

GET /索引名称/类型/_search?q=age:>**

image-20220112090905920

GET /my_index01/_doc/_search?q=age:>24

分页查询

相当于 SQL 中的 limit 0, 1:

GET /索引名称/类型/_search?q=age:>**&from=**&size=**

image-20220112091110912

GET /my_index01/_doc/_search?q=age:>18&from=0&size=2

_source 定制返回结果

GET /索引名称/_doc/id?_source=file1,file2

image-20220112131123444

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

image-20220112131037096

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

对查询结果排序

GET /索引名称/类型/_search?sort=字段 desc

image-20220112154503431

GET /my_index01/_doc/_search?_source=name,age&q=age:>18&from=0&size=4&sort=age:desc
posted @ 2022-01-11 23:05  BNTang  阅读(118)  评论(0编辑  收藏  举报