- 添加或更新数据
- 语法:数据存在更新;如果不存在添加(指定id)
PUT _index/_type/_id { "属性": "值", .... }
实例
PUT czxy/book/b001 { "title": "标题123", "price": 1234 }
删除数据
语法:
DELETE _index/_type/_id
实例
DELETE czxy/book/b001
查询操作
简单查询
指定id查询
GET czxy/book/b001
指定id查询,检索指定内容
GET czxy/book/b001?_source=title,price
高级查询
准备数据
PUT czxy2 PUT czxy2/_mapping/user { "properties": { "username": { "type": "text", "analyzer": "ik_max_word" }, "city": { "type": "keyword" }, "age": { "type": "float" } } } PUT czxy2/user/u001 { "username": "张三", "city": "山西", "age": 18 } PUT czxy2/user/u002 { "username": "张四", "city": "山东", "age": 20 } PUT czxy2/user/u003 { "username": "张三三", "city": "山东", "age": 22 }
查询所有
match_all:检索所有的数据
GET _search { "query": { "match_all": {} } }
查询指定索引的所有
GET czxy2/_search { "query": { "match_all": {} } }
查询部分结果
GET czxy2/_search { "query": { "match_all": {} }, "_source": ["username","city"] }
条件查询:匹配查询
GET czxy2/_search { "query": { "match": { "username": "张三" } } }
条件查询:复合查询(多条件查询)
bool:多条件拼接
must:必须
must_not:不含
should:可有可无
GET czxy2/_search { "query": { "bool": { "must": [ { "match": { "city": "山东" } } ], "must_not": [ { "match": { "age": 22 } } ] } } }
should并集查询
注意:must和should同时存在的时候,should将会变得不起作用
GET czxy2/_search { "query": { "bool": { "should": [ { "match": { "city": "山东" } }, { "match": { "city": "山西" } } ] } } }
精确匹配
term 进行精确匹配(数据要求:数字、日期、布尔、not_analyzed的字符串(未分词))
GET czxy2/_search { "query": { "term": { "age": 2 } } }
范围查询
range 用于确定范围,gt 大于、lt 小于、gte大于等于、lte小于等于
GET czxy2/_search { "query": { "range": { "age": { "gte": 19, "lte": 21 } } } }
排序
GET czxy2/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ] }
分页查询
from 设置索引号,从0开始
size 设置每页个数
GET czxy2/_search { "query": { "match_all": {} }, "from": 0, "size": 1 }