ES通过HTTP Restful方式管理数据: - 格式:#操作 /index/type/id (#操作符 索引名/类型/主键) - 操作:可以进行 - 添加(POST) - 修改(PUT) - 删除(DELETE) - 查询(GET) - 批量插入(_bulk) - 批量获取(_mget)
查看所有索引
[root@admin ~]# curl -X GET 'http://192.168.1.76:9200/_cat/indices?v'
列出索引类型
[root@admin ~]# curl 'http://192.168.1.76:9200/_mapping?pretty=true'
新建索引wwwwww
[root@admin ~]# curl -X PUT "http://192.168.1.76:9200/wwwwww"
查询索引信息
[root@admin ~]# curl -X GET "http://192.168.1.76:9200/logstash-nginx-*.*?pretty"
[root@admin ~]# curl -X GET "http://192.168.1.76:9200/logstash-nginx-2020.08.11?pretty"
post添加数据,未指定id随机创建
[root@admin ~]# curl -X POST -H "Content-Type: application/json" "http://192.168.1.76:9200/su/i" --data '{"name":"s","age":19}'
{"_index":"su","_type":"i","_id":"HI_323MBKV9-De-YLRP6","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
通过elasticsearch 的header插件查看数据:
删除索引
[root@admin ~]# curl -X DELETE "http://192.168.1.76:9200/su"
{"acknowledged":true}
删除 指定id数据
[root@admin ~]# curl -X DELETE "http://192.168.1.76:9200/su/i/II8I3HMBKV9-De-YjyHC"
{"_index":"su","_type":"i","_id":"II8I3HMBKV9-De-YjyHC","_version":2,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
增加数据或者修改数据(指定id,id存在更新数据,id不存在就插入新数据)
[root@admin ~]# curl -X PUT -H "Content-Type: application/json" "http://192.168.1.76:9200/su/i/II8I3HMBKV9-De-YjyHC" --data '{"name":"f","age":22}'
[root@admin ~]# curl -X PUT -H "Content-Type: application/json" "http://192.168.1.76:9200/su/i/111" --data '{"name":"f","age":22}'
修改数据个别字段(用post方法 和 _update)
[root@admin ~]# curl -X POST -H "Content-Type: application/json" "http://192.168.1.76:9200/su/i/111/_update" --data '{"doc":{"name":"haha"}}'
查询数据(查询所有数据)
[root@admin ~]# curl "http://192.168.1.76:9200/su/i/_search"
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"su","_type":"i","_id":"II8I3HMBKV9-De-YjyHC","_score":1.0,"_source":{"name":"f","age":22}},{"_index":"su","_type":"i","_id":"111","_score":1.0,"_source":{"name":"haha","age":22}}]}}
#took该操作的耗时,单位为毫秒.
#timed_out字段表示是否超时.
#hits字段表示命中的记录,字段含义如下.
#total:返回记录数2条.
#max_score:最高的匹配程度,本例是1.0.
#hits:返回的记录组成的数组.
#每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列.
根据feild查询(q=字段名="要查询的字段值")
[root@admin ~]# curl -X GET 'http://192.168.1.76:9200/su/i/_search?q=name="haha"'
Match查询,match查询会先对搜索词进行分词,分词完毕后再逐个对分词结果进行匹配,相对于term的精确搜索,match是分词匹配搜索,match搜索还有两个相似功能的变种:match_phrase、multi_match
[root@admin ~]# curl -H "Content-Type: application/json" -X GET 'http://192.168.1.76:9200/su/_search' -d '{"query":{"match":{"name":"haha"}}}'
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.6931471,"hits":[{"_index":"su","_type":"i","_id":"111","_score":0.6931471,"_source":{"name":"haha","age":22}}]}}
[root@admin ~]# curl -H "Content-Type: application/json" -X GET 'http://192.168.1.76:9200/logstash-nginx-2020.08.11/_search' -d '{"query":{"match":{"domain":"www.example.com"}},"size":1}'
#"size":1作用只返回1个查询结果
查询结果分页
[root@admin ~]# curl -H "Content-Type: application/json" -X GET 'http://192.168.1.76:9200/su/_search' -d '{"query":{"match":{"name":"haha"}},"from":1,"size":4}'
#默认"from" : 0, "size" : 10
排序sort
[root@admin ~]# curl -H "Content-Type: application/json" -X GET 'http://192.168.1.76:9200/su/_search' -d '{"query":{"match":{"name":"haha"}},"sort": [{"name": "desc"},"_score"]}'
#(以上命令会报错)es的text字段默认禁止聚合/排序操作,需要开启 fielddata=true,但是会消耗更多内存;也可以针对text字段额外映射一个keyword字段,使用keyword字段来进行排序、聚合和脚本操作,ext用于全文搜索的,而keyword用于关键词搜索.
###对text字段添加keyword,然后排序:
[root@admin ~]# curl -H "Content-Type: application/json" -X PUT 'http://192.168.1.76:9200/su/_mapping' -d '{
"properties":{
"name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
}
}
}
}'
###对keyword字段排序使用name.keyword,字段.keyword 形式
[root@admin ~]# curl -H "Content-Type: application/json" -X POST 'http://192.168.1.76:9200/su/_search' -d '{"sort": {"name.keyword": "desc"},"query":{"term":{"name":"haha"}}}'
###开启fielddata方式进行排序:
[root@admin ~]# curl -H "Content-Type: application/json" -X PUT 'http://192.168.1.76:9200/su/_mapping' -d '
{
"properties":{
"name":{
"type": "text",
"fielddata": true
}
}
}'
[root@admin ~]# curl -H "Content-Type: apOST 'http://192.168.1.76:9200/su/_search' -d '{"sort": {"name": "desc"},"query":{"term":{"name":"haha"}}}'
多字段排序
[root@admin ~]# curl -H "Content-Type: application/json" -X POST 'http://192.168.1.76:9200/su/_search' -d '{"sort": [{"name": "desc"},{"age":"desc"}],"query":{"term":{"name":"haha"}}}'
term查询,term代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词
[root@admin ~]# curl -H "Content-Type: application/json" -X GET 'http://192.168.1.76:9200/su/_search' -d '{"query":{"term":{"name":"haha"}}}'
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.6931471,"hits":[{"_index":"su","_type":"i","_id":"111","_score":0.6931471,"_source":{"name":"haha","age":22}}]}}
多条件查询(使用bool下的must)
[root@admin ~]# curl -H "Content-Type: application/json" -X GET 'http://192.168.1.76:9200/logstash-nginx-2020.08.11/_search' -d '
{
"query": {
"bool": {
"must": [
{"term":{"domain":"www.example.com"}},
{"term":{"status":200}}
]
}
},
"size":1
}'
控制返回特定字段_source
[root@admin ~]# curl -H "Content-Type: application/json" -X POST 'http://192.168.1.76:9200/su/_search' -d '{"_source":{"includes":"name","excludes":"age"},"sort": [{"name": "desc"},{"age":"desc"}],"query":{"term":{"name":"haha"}}}'
#返回includes匹配的字段,剔除excludes匹配到的字段。