- 索引 含有相同属性的文档集合
- 类型 索引可以定义一个或多个类型,文档必须属于一个类型
- 文档 文档是可以被索引的基本数据单位
- 分片 每个索引都有多个分片,每个分片是一个Lucene索引
- 备份 拷贝一份分片就可以完成了分片的备份
2 基础用法
2.1 RESTFul API
API基本格式:http://<ip>:<port>/<索引>/<索引类型>/<文档id>
常用HTTP动词:GET/PUT/POST/DELETE
2.2 创建索引
2.2.1 使用默认配置创建索引
例如创建一个名为people的索引库
请求URL:http://127.0.0.1:9200/people
请求方法:PUT
请求参数:无
返回结果:
{
"acknowledged": true,
"shards_acknowledged": true
}
2.2.2 指定配置创建索引
例如创建一个名为people的索引库,并指定分片个数为3,副本数为1
请求URL:http://127.0.0.1:9200/people
请求方法:PUT
请求参数:
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
}
}
返回结果:
{
"acknowledged": true,
"shards_acknowledged": true
}
2.3 文档数据插入
2.3.1 自定义文档id插入数据
向people索引库中插入一个类型为man,id为1的数据
请求URL:http://127.0.0.1:9200/people/man/1
请求方法:PUT
请求参数:
{
"name":"rukey",
"age":24
}
返回结果:
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
2.3.2 自动生成文档id插入数据
上述例子中显示指定了id为1,也可以让es自动为文档生成ID。
请求URL:http://127.0.0.1:9200/people/man
请求方法:POST
请求参数:
{
"name":"luke",
"age":21
}
返回结果:
{
"_index": "people",
"_type": "man",
"_id": "AXKiIOPUal2MQt749lnL",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
2.3 文档数据修改
2.3.1 直接修改文档数据
将上述people索引库中类型为man,id为1的文档数据中的age字段值改为26。
请求URL:http://127.0.0.1:9200/people/man/1/_update
请求方法:POST
请求参数:
{
"doc":{
"age":26
}
}
返回结果:
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
2.3.2 脚本修改文档数据
使用脚本的方式将上述people索引库中类型为man,id为1的文档数据中的age字段值减6
请求URL:http://127.0.0.1:9200/people/man/1/_update
请求方法:POST
请求参数:
{
"script":{
"lang": "painless",
"inline":"ctx._source.age=ctx._source.age-6"
}
}
返回结果:
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
2.4 删除操作
2.4.1 删除文档数据
2.4.1.1 根据ID删除单条文档
例如删除people索引库中类型为man,id为1的文档数据
请求URL:http://127.0.0.1:9200/people/man/1
请求方法:DELETE
请求参数:无
返回结果:
{
"found": true,
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
2.4.1.2 删除查询的所有文档
2.4.2 删除索引库
删除索引库,会将索引,以及该索引下的所有文档数据一并删除,例如删除名为people的索引库
请求URL:http://127.0.0.1:9200/people
请求方法:DELETE
请求参数:无
返回结果:
{
"acknowledged": true
}
2.5 常用查询
在进行查询之前,先准备一下数据。
创建一个名为people的索引库,并设置mappings
POST请求http://127.0.0.1:9200/people,请求参数如下
{
"mappings": {
"man": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
}
}
}
}
批量插入数据
POST请求http://127.0.0.1:9200/_bulk,请求参数如下:
{ "index":{"_index":"people","_type":"man","_id":1 }}
{ "name":"张三","age":16,"birthday":1086941876000}
{ "index":{"_index":"people","_type":"man","_id":2 }}
{ "name":"李四","age":20,"birthday":960711476000}
{ "index":{"_index":"people","_type":"man","_id":3 }}
{ "name":"王五","age":16,"birthday":1086077876000}
请求结果:
{
"took": 7,
"errors": false,
"items": [
{
"index": {
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
},
{
"index": {
"_index": "people",
"_type": "man",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
},
{
"index": {
"_index": "people",
"_type": "man",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
}
]
}
2.5.1 简单查询
查询people索引库中类型为man,id为1的文档内容
请求URL:http://127.0.0.1:9200/people/man/1
请求方法:GET
请求参数:无
返回结果:
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "张三",
"age": 16,
"birthday": 1086941876000
}
}
2.5.2 条件查询
查询people索引库中所有数据
请求URL:http://127.0.0.1:9200/people/_search
请求方法:POST
请求参数:
{
"query":{
"match_all": {}
}
}
返回结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 20,
"birthday": 960711476000
}
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 16,
"birthday": 1086941876000
}
},
{
"_index": "people",
"_type": "man",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 16,
"birthday": 1086077876000
}
}
]
}
}
指定分页信息
通过from指定从哪里返回,通过size指定返回大小
请求URL:http://127.0.0.1:9200/people/_search
请求方法:POST
请求参数:
{
"query":{
"match_all": {}
},
"from":1,
"size":1
}
返回结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 16,
"birthday": 1086941876000
}
}
]
}
}
指定排序信息查询
查询所有文档,并按照age字段降序排序
请求URL:http://127.0.0.1:9200/people/_search
请求方法:POST
请求参数:
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
返回结果:
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "2",
"_score": null,
"_source": {
"name": "李四",
"age": 20,
"birthday": 960711476000
},
"sort": [
20
]
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": null,
"_source": {
"name": "张三",
"age": 16,
"birthday": 1086941876000
},
"sort": [
16
]
},
{
"_index": "people",
"_type": "man",
"_id": "3",
"_score": null,
"_source": {
"name": "王五",
"age": 16,
"birthday": 1086077876000
},
"sort": [
16
]
}
]
}
}
指定关键字查询
查询名字为王五的文档
请求URL:http://127.0.0.1:9200/people/_search
请求方法:POST
请求参数:
{
"query": {
"match": {
"name": "王五"
}
}
}
返回结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "王五",
"age": 16,
"birthday": 1086077876000
}
}
]
}
}
2.5.3 聚合查询
按照关键字聚合
查询所有文档,并按照age字段聚合
请求URL:http://127.0.0.1:9200/people/_search
请求方法:POST
请求参数:
{
"aggs": {
"group_by_age":{
"terms":{
"field": "age"
}
}
}
}
返回结果:
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 20,
"birthday": 960711476000
}
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 16,
"birthday": 1086941876000
}
},
{
"_index": "people",
"_type": "man",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 16,
"birthday": 1086077876000
}
}
]
},
"aggregations": {
"group_by_age": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 16,
"doc_count": 2
},
{
"key": 20,
"doc_count": 1
}
]
}
}
}