索引和文档基本使用
字数: 0 字 时长: 0 分钟
Search API
json
GET test_index/_search
GET kibana_sample_data_flights/_search?from=1000&size=2
可选参数
- size : 单次查询多少条文档,默认为 10
- from : 起始文档偏移量。需要为非负数,默认为 0
- timeout : 指定等待每个分片响应的时间段,如果在超时时间未响应则请求失败,默认为无超时
Index API
索引由三部分组成:
aliases
: 索引别名mappings
: 索引映射,包含源数据字段和类型定义、具体字段分词器定义等settings
: 包含分片数、副本数、刷新间隔等
设置 settings
json
PUT test_index
{
"settings": {
"number_of_shards": 1, //主分片数为 1
"number_of_replicas": 1 //副本数为 1
}
}
修改 settings
json
// 修改主分片副本数
PUT test_index/_settings
{
"settings": {
"number_of_replicas": 2
}
}
settings 中部分配置一旦指定就不可修改,只能在创建索引时或者在关闭状态的索引上设置:
index.number_of_shards
: 主分片数,默认为1,不可修改
settings 中也有部分配置可用 _setting
API 实时修改:
index.number_of_replicas
: 每个主分片的副本数index.refresh_interval
: 刷新频率,默认为 1s ; 可以设置为 -1 来禁用刷新,在索引重建、数据迁移等大吞吐量场景下可以提升性能index.max_result_window
: 单次搜索此索引的结果最大值,默认为 10,000
索引的不可变性
索引一旦创建后,以下属性将不可修改
- 索引名称
- 主分片数量
- 字段类型
索引迁移 Reindex
json
POST _reindex
{
"dest": {
"index": "my_index_new"
},
"source": {
"index":"my_index"
}
}
Document API
Create 创建
在 create_index
索引中强制创建 id 为 1 的文档,包含 name 和 age 两个字段
json
PUT careate_index/_doc/1?op_type=create
{
"name":"威少",
"age": 35
}
//简写为
PUT careate_index/_create/1
{
"name":"威少",
"age": 35
}
如果不存在则创建
json
{
"_index": "careate_index",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
如果存在,则报错
json
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [1])",
"index_uuid": "nkqoz_WGR5GpJKnIJiEOfQ",
"shard": "0",
"index": "careate_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [1])",
"index_uuid": "nkqoz_WGR5GpJKnIJiEOfQ",
"shard": "0",
"index": "careate_index"
},
"status": 409
}
Index 索引
在 ES 中,写入操作也可被称为 index ,这里为动词。可以是创建,也可以是全量替换。
json
//创建
PUT index/_doc/1?op_type=index
{
"name" : "甜甜"
}
//简写,并且全量替换
PUT index/_doc/1
{
"name":"甜甜2"
}
查询结果,发现以及被全量替换
json
{
"_index": "index",
"_id": "1",
"_score": 1,
"_source": {
"name": "甜甜2"
}
source API
使用 _source
可以打开或者关闭数据源字段, true
为打开, 默认为 true
json
GET index/_search?_source=true
GET index/_search?_source=false
也可以只查询源数据,不展示元数据字段
json
GET index/_source/1
{
"name": "甜甜2"
}
delete API
json
//必须指定 doc 的 id
DELETE index/_doc/1
update API
json
POST index/_update/1
{
"doc": {
"name":"宝贝"
}
}
Multi get API
_mget
可以同时指定查询不同 index 中的不同数据
json
GET /index,my_index/_mget
{
"docs": [
{
"_index": "index",
"_id": "1"
},
{
"_index": "my_index",
"_id": "1"
}
]
}
查询结果:
json
{
"docs": [
{
"_index": "index",
"_id": "1",
"_version": 3,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"name": "宝贝"
}
},
{
"_index": "my_index",
"_id": "1",
"_version": 3,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"name": "李四",
"age": 18
}
}
]
}
Bulk API
json
POST /_bulk
{ "index" : { "_index" : "index", "_id" : "2" } }
{"name":"甜甜3"}
{ "index" : { "_index" : "my_index", "_id" : "1" } }
{"name":"宝贝"}