文章目录
一、索引基本配置信息查看
1.1 alias
"aliases" : { },
1.2 mapping
mapping中主要设置主要包括两部分:映射配置、字段属性配置
- 映射设置
dynamic属性,表示是否开启动态映射,默认打开。该参数共有3种属性:
true : 默认开启,当文档中出现新的字段时,自动为其为其索引,并添加到字段映射配置中
false : 对文档中新增的字段不进行索引,不会为其添加字段映射配置,但是文档查询时会出现在_source中
strict : 当文档有新的字段时抛出异常
- 字段映射配置
索引下所有被索引的字段及其对应数据类型、分词器(存储、查询)、的设置。
"mappings" : { //各个字段以及数据类型的映射关系
"properties" : {
"age" : {
"type" : "long"
},
"info" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
1.3 setting
setting为索引的基本设置信息,比较核心关注的number_of_shards、number_of_replicas两个参数,分别表示索引的分片数(7.4之后默认主分片数为1)以及副本数。
"settings" : {
"index" : {
"creation_date" : "1596012703101",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "mT47Qv4AQ82nOpSdKZiydw",
"version" : {
"created" : "7040099"
},
"provided_name" : "users"
}
}
二、CRUD
2.1 创建索引
1、语法
在ES中,我们可以通过PUT命令提前对索引的alias、mapping、setting等信息进行设置,这种方式比较灵活,可以根据业务具体需求进行规划设置。当然如果直接对索引写入文档记录,ES的动态映射可以进行自动识别为其添加基本设置信息,但是这种方式索引的某些设置可能无法满足我们的业务需求。
1)手动设置索引基本配置
PUT /${index_name}?pretty
{
"settings": { //setting设置,主要设置分片数以及副本数
"index": {
"number_of_shards": M,
"number_of_replicas": N
}
},
"mappings": {
"dynamic": true, //动态映射建议打开
"properties": { //各字段映射关系
"${col1}": {
"type": "long"
},
"${col2}": {
"type": "text",
"fields": {
"keyword":{
"type" : "keyword"
}
}
}
}
}
}
2)查看索引信息
# 查看索引的基本配置信息
GET /${index_name}
# 查看索引的基本状态信息
GET _cat/indices/${index_name}
3)索引基本信息的修改
索引的分片数要在索引创建前提前规划好,否则后续如果需要修改索引分片数只能进行reindex重建索引。以下一些比较常见的索引配置信息修改主要有:
- 副本数修改
PUT /${index_name}/_settings
{
"index" : {
"number_of_replicas" : 2
}
}
- 新增字段
PUT /users_test/_mapping
{
"properties": {
"info": {
"type": "text"
}
}
}
- refresh_interval修改
PUT /users_test/_settings
{
"index" : {
"refresh_interval" : "2s"
}
}
- 分词器修改
- 别名修改
2.2 新增文档
1、语法
1)新增文档,并指定id字段(replace)
该模式下,若指定id对应的文档存在,则删除源文档后并写入新文档,并且version版本加1。
PUT ${index_name}/_doc/1
{
...
}
2)新增文档,并指定ID字段(insert)
该模式下,若指定id对应的文档存在,则直接返回报错
PUT ${index_name}/_create/1
{
...
}
或者
PUT ${index_name}/_create/1?op_type=create
{
...
}
3)新增文档,不指定ID字段,系统自动生产ID
POST ${index_name}/_doc
{
"name":"aaa",
"age":"22"
}
2.3 更新文档
1)更新指定ID文档,存在则更新,不存在则新增(也是新增文档的一种)
PUT ${index_name}/_doc/1
{
...
}
2)更新指定ID文档,更新文档必须存在,否则报错
POST /${index_name}/_update/1
{
"doc": {
"name":"aa",
"age":12
}
}
3)更新指定查询结果的文档
POST ${index_name}/_update_by_query
{
"script": {
"source": "ctx._source.name = params.name",
"params":{
"name":"aaa"
},
"lang":"painless"
},
"query": {
"term": {
"name": "cc"
}
}
}
2.4 删除文档
1)删除指定ID文档
DELETE /${index_name}/_doc/1
2)删除满足条件查询文档
POST /${index_name}/_delete_by_query
{
"query":{
"match":{
"name":"cc"
}
}
}
2.5 查询文档
1)GET查询指定ID文档
GET /${index_name}/_doc/3
2)search api查询满足条件的文档
## 查询索引下全部文档
GET /${index_name}/_search
## 查询满足指定条件全部文档
GET /#{index_name}/_search
{
"query": {
"match": {
"name": "aaa"
}
}
}
2.6 bulk api
支持在一次api中对不同的索引进行操作
支持create index update delete
POST _bulk
{"index":{"_index":"users","_id":"1"}}
{"name":"aa"}
{"delete":{"_index":"users","_id":"2"}}
{"create":{"_index":"users","_id":"2"}}
{"name":"bb"}
{"update":{"_id":"1","_index":"users"}}
{"doc":{"auto_inc":"11"}}
get users/_doc/1
2.6 批量查询
mget 是通过文档ID列表得到文档信息。msearch 是根据查询条件,搜索到相应文档。
1、mget
GET _mget
{
"docs":[{
"_index":"users",
"_id":"1"
},
{
"_index":"users",
"_id":"2"
}
]
}
2、msearch
POST users/_msearch
{}
{"query":{"match_all":{}},"from":0,"size":10}
{}
{"query":{"match_all":{}}}
{"_index":"users"}
{"query":{"match_all":{}}}
一个比较好的ES学习的博客,推荐给大家:
https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&__biz=MzIxMjE3NjYwOQ==&scene=1&album_id=1337850434433744897#wechat_redirect