method | 描述 |
---|---|
PUT | 创建 |
POST | 创建、修改 |
DELETE | 删除 |
GET | 查询 |
①数据模型
Index:索引,由多个Document组成 ,相当于关系型数据库中的数据库(database)
Type:索引类型,6.x中仅支持一个,以后将逐渐被移除,相当于关系型数据库中的表(tables)
Document:文档,由多个Field组成,相当于关系型数据库中的行(rows)
Field:字段,包括字段名与字段值,相当于关系型数据库中的字段(columns)
②Document管理:文档是ES最小数据单元
注意:索引前面的/可加可不加
PUT /索引名/类型名/文档id {请求体}
例如:
PUT test/type/1
{
"name":"tom",
"interest":"play"
}
GET _cat/indices
方法一:(如果不传递值就会被覆盖)
#相同文档id提交可以实现覆盖完成修改
PUT /test/type/1
{
"name":"tom",
"interest":"learn"
}
方法二:(只需编辑更新的数据,灵活性更高)
POST /test/type/1/_update
{
"doc": {
"interest":"dance"
}
}
#删除指定编号文档
DELETE test/type/1
#删除整个索引
DELETE test
PUT test/type/1
{
"name":"tom",
"interest":"learn",
"gender":"male",
"age":"18"
}
#批量操作步骤
PUT _bulk
{"index/create/update/delete":{"_index":"...","_type":"...","_id":"..."}}
{"field1":"value1",……}
#以插入数据为例
PUT _bulk
{"create":{"_index":"student01","_type":"student","_id":"1"}}
{"id":"1","name":"zhangsan","join_date":"2014","interest":"basketball"}
{"create":{"_index":"student01","_type":"student","_id":"2"}}
{"id":"2","name":"join","join_date":"2017","interest":"boxing"}
{"create":{"_index":"student01","_type":"student","_id":"3"}}
{"id":"3","name":"John Kerry","join_date":"2016","interest":"cooking"}
方式一:
GET student01/student/_mget
{"docs":[{"_id":"1"},{"_id":"2"}]}
方式二:
GET student01/_mget
{"docs":[{"_type":"student","_id":"1"},{"_type":"student","_id":"2"}]}
方式三:(可读取不同索引的表)
GET _mget
{"docs":[{"_index":"student01","_type":"student","_id":"1"},{"_index":"test","_type":"type","_id":"1"}]}
1)简单查询
①搜索指定编号的数据
GET test/type/1/
②带条件的查询(当索引中含有多条数据时)
GET test/type/_search?q=name:tom
2)复杂查询
①match查询
GET test/_search
{
"query": {
"match_all": {}
}
}
GET student01/_search
{
"query": {
"match": {
"name":"John Kerry"
}
}
}
GET student01/_search
{
"query": {
"match_phrase": {
"name":"John Kerry"
}
}
}
GET student01/_search
{
"query": {
"match_phrase_prefix": {
"name":"John Ke"
}
}
}
GET student01/_search
{
"query": {
"multi_match": {
"query": "John like cooking",
"fields": ["name","interest"]
}
}
}
②term查询 (词条查询:按照存储在倒排索引中的确切字词,对字段进行匹配)
GET student01/_search
{
"query": {
"term":{
"name":"john" #注意值全部为小写
}
}
}
③terms查询 (词条查询:按照存储在倒排索引中的确切字词,对字段进行多词条匹配)
GET student01/_search
{ "query": {
"terms":{
"name":["john","da"] #注意值全部为小写
}
}
}
④range查询 (范围查询)
gt 大于
gte 大于等于
lt 小于
lte 小于等于!
GET student01/_search
{"query": {
"range" : {
"join_date" : {
"gte" : 2016,
"lte" : 2017
}
}
}
}
⑤fliter过滤器(filter是放在bool查询中的)
GET student01/_search
{"query": {
"bool": {
"filter": {
"range": {
"join_date": {
"gte": 2016
}
}
}
}
}
}
⑥sort排序
GET student01/_search
{"query": {
"match_all": {
}
},
"sort": {
"id":"desc"
}
}
⑦bool查询(复合查询子句)
#请在学生信息索引“student01”中查询同时满足以下要求的文档
#姓名为“john”
#在2016年后入学
#爱好“boxing”
GET student01/student/_search
{"query":{
"bool": {
"must": [
{"match": {
"name.keyword": "join"
}},{
"match": {
"interest": "boxing"
}
}
],
"filter": {
"range": {
"join_date": {
"gte": 2016
}
}
}
}
}
}
方法一:size:页面容量 from:数据下标是从0开始的,表示从第几个数据开始
GET student01/_search?size=5&from=2
方法二:
GET student01/_search
{
"query": {
"match_all": {}
},
"size": 5,
"from": 2
}
①返回scroll_id与第一页内容,scroll_id 5分钟有效
GET stu/_search?scroll=5m
{
"from": 0,
"size": 5,
"query": {"match_all": {}}
}
②根据scroll_id不断获取下一页内容
GET _search/scroll
{
"scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAANy……",
"scroll": "5m"
}