一、索引
1.创建索引
语法:put 索引名称
示例:PUT test_index
2.增加索引配置
增加索引配置:JSON格式的主体内容
语法:put 索引名称 {主题内容}
示例:
PUT test_index1
{
"aliases": {
"test1": {}
}
}
3.删除索引
DELETE 索引名称
DELETE test_index1
4.修改配置
es不允许修改索引信息!!!
5.校验索引是否存在
语法:HEAD 索引名称
示例:HEAD test_index1
6.查询索引
语法:GET 索引名称
示例:
GET test_index
GET test_index1
GET test1
7.查询所有索引
语法:GET _cat/indices
二、文档
1.创建文档(索引数据)
创建文档(索引数据)- 增加唯一性标识(手动 put,自动 post)
语法:put 和post两种 put必须手动指定数据id post自动生成id
put 索引名称/_doc/数据id {主体内容} 可指定id 手动
post 索引名称/_doc {主体内容} 不可指定id 自动
示例:
PUT test_doc/_doc/1001
{
"id":1001,
"name":"zhangsan",
"age":30
}
POST test_doc/_doc
{
"id":1002,
"name":"lisi",
"age":40
}
2.查询文档
语法:GET 索引名称/_doc/数据id
示例:GET test_doc/_doc/1001
3.查询索引中所有的文档
语法:
GET 索引名称/_search
示例:
GET test_doc/_search
4.修改文档数据
修改分为put和post两种,语法是一样的
语法:
PUT 索引名称/_doc/数据id {主体内容}
POST 索引名称/_doc/数据id {主体内容}
示例:
PUT test_doc/_doc/1001
{
"id":10011,
"name":"zhangsan1",
"age":300,
"mobile":"12312312311"
}
POST test_doc/_doc/sVsLVJEB40kQ9ev-ncIi
{
"id":10012,
"name":"zhangsan2",
"age":200,
"mobile":"12312312312"
}
5.删除数据
语法:
DELETE 索引名称/_doc/数据id
示例:
DELETE test_doc/_doc/sVsLVJEB40kQ9ev-ncIi
6.批量添加数据
语法:
put 索引名称/_bulk
{"index":{"_index" : "索引名称","_id" : "数据id"}}
{数据主题}
示例:
PUT test_query/_bulk
{"index":{"_index" : "test_query","_id" : "1001"}}
{"id":"1001","name":"zhangsan","age":30}
{"index":{"_index" : "test_query","_id" : "1002"}}
{"id":"1002","name":"lisi","age":35}
{"index":{"_index" : "test_query","_id" : "1003"}}
{"id":"1003","name":"wangwu","age":40}
{"index":{"_index" : "test_query","_id" : "1004"}}
{"id":"1004","name":"zhang san","age":30}
{"index":{"_index" : "test_query","_id" : "1005"}}
{"id":"1005","name":"li si","age":35}
{"index":{"_index" : "test_query","_id" : "1006"}}
{"id":"1006","name":"wang wu","age":40}
三、匹配查询
1.匹配查询所有
语法:
GET 索引名称/_search
{
"query": {
"match_all": {}
}
}
match_all 指的是查询所有
示例:
GET test_query/_search
{
"query": {
"match_all": {}
}
}
2.match 按条件匹配查询所有
match是分词查询,es会将数据分词(关键词)保存 比如 zhang san 会分为zhang 和 san 两个分词,满足其中一个就能查出来
语法:
GET 索引名称/_search
{
"query": {
"match": {
"属性名称": "分词1 分词2"
}
}
}
示例:
GET test_query/_search
{
"query": {
"match": {
"name": "zhangsan li"
}
}
}
3.term 按条件匹配查询所有
term是完整查询,es会将数据分词(关键词)保存 比如 zhang san 会分为zhang 和 san 两个分词,但是term中,分词必须完全匹配value值才能查询出来
语法:
GET 索引名称/_search
{
"query": {
"term": {
"属性名称":{
"value": "分词"
}
}
}
}
示例:
GET test_query/_search
{
"query": {
"term": {
"name":{
"value": "zhangsan"
}
}
}
}
4…对查询结果字段进行限制
语法:
GET 索引名称/_search
{
"_source": [" 属性1","属性2"],
"query": {
"match": {
"属性名称": "分词1 分词2"
}
}
}
示例:
GET test_query/_search
{
"_source": [" name","age"],
"query": {
"match": {
"name": "zhangsan li"
}
}
}
5…组合多个条件 类似于sql中的or
语法:
GET 索引名称/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"属性名称": "分词"
}
},
{
"match": {
"属性名称": 查询条件
}
}
]
}
}
}
示例:
GET test_query/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "zhang"
}
},
{
"match": {
"age": 40
}
}
]
}
}
}
6.排序后查询
语法:
GET 索引名称/_search
{
"query": {
"match": {
"属性名称": "分词1 分词2"
}
},
"sort": [
{
"排序字段(属性)": {
"order": "desc(asc)"
}
}
]
}
示例:
GET test_query/_search
{
"query": {
"match": {
"name": "zhang li"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
7.分页查询
from : 从下标(0,1,2)为几据开始查询数据,size:每页数据量 from = (pageno-1) *size
语法:
GET 索引名称/_search
{
"query": {
"match_all": {}
},
"from": 2,
"size": 2
}
示例:
GET test_query/_search
{
"query": {
"match_all": {}
},
"from": 2,
"size": 2
}
四、分组查询
1.分组查询
"size": 0 展示的索引数据0条,便于查看分组信息,不然太杂乱
"field": "age" 表示对age进行分组
语法:
GET 索引名称/_search
{
"aggs": {
"分组名称": {
"terms": {
"field": "属性字段"
}
}
},
"size": 0
}
示例:
GET test_query/_search
{
"aggs": {
"agegroups": {
"terms": {
"field": "age"
}
}
},
"size": 0
}
2.分组后聚合(求和)
先按年龄分组,再计算年龄之和;再agegroups分组中,再次对age进行求和
示例:
GET test_query/_search
{
"aggs": {
"agegroups": {
"terms": {
"field": "age"
},
"aggs": {
"agesum": {
"sum": {
"field": "age"
}
}
}
}
},
"size": 0
}
3.分组查询年龄平均值
示例:
GET test_query/_search
{
"aggs": {
"ageavg": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
4.分组查询年龄前三名
根据age进行降序,排序字段可以有多个,与sql类似
"sort": [{
"age":{
"order": "desc"
}
}]
top_hits 表示排序后取size条数据
示例:
GET test_query/_search
{
"aggs": {
"top3": {
"top_hits": {
"size": 3,
"sort": [{
"age":{
"order": "desc"
}
}]
}
}
},
"size": 0
}
五、模板
1.创建/修改模板
语法:
PUT _template/模板名称
{
"index_patterns": [
"模板名称匹配规则 如my* 表示索引名称必须以my开头才能使用模板"
],
"settings": {
"index":{
"number_of_shards":"2"
}
},
"mappings": {
"properties": {
"now":{
"type": "date",
"format": "yyyy/MM/dd"
}
}
}
}
示例:
PUT _template/mytemplate
{
"index_patterns": [
"my*"
],
"settings": {
"index":{
"number_of_shards":"2"
}
},
"mappings": {
"properties": {
"now":{
"type": "date",
"format": "yyyy/MM/dd"
}
}
}
}
2.查看模板
语法:
GET _template/模板名称
示例:
GET _template/mytemplate
3.使用模板
创建索引时,按照模板的匹配规则进行创建,如 my* 就是创建时的索引名称必须以my开头才能使用
示例:PUT my_test_temp
4.删除模板
语法:
DELETE _template/模板名称
示例:
DELETE _template/mytemplate