以下操作基于kibana客户端,数据也是使用kiaba自带添加的数据。
1、获得集群的健康情况
GET /_cat/health?v
说明:v是用来要求在结果中返回表头
状态值说明
Green - everything is good (cluster is fully functional),即最佳状态
Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即数据和集群可用,但是集群的备份有的是坏的
Red - some data is not available for whatever reason (cluster is partially functional),即数据和集群都不可用
2、获取集群所有节点
GET /_cat/nodes?v
3、获取index 列表
GET /_cat/indices?v
4、是否设置缓存加锁(ES建议关闭缓存使用)
GET _nodes?filter_path=**.mlockall
5、创建index
PUT /customer
6、创建Document
PUT /custome/_doc/1?pretty
{
"name":"john doe"
}
7、查看Document
GET /custome/_doc/1
8、删除index
DELETE /custome
9、创建文档
PUT /customer/_doc/1
{
"name":"john doe"
}
10、查看文档
GET /customer/_doc/1
11、修改文档(覆盖)
POST /customer/_doc/1
{
"name":"joan doe1"
}
12、修改文档(部分修改)
POST /customer/_update/1
{
"doc":{
"name":"joon doe"
,"age":5
}
}
13、修改文档(使用脚本)
POST /customer/_update/1
{
"script":"ctx._source.age +=5"
}
14、批量操作(注意格式)
POST /customer/_bulk
{"index":{"_id":"2"}}
{"name":"bulk1"}
{"update":{"_id":"2"}}
{"doc":{"age":21}}
15、queryString 查询
GET /kibana_sample_data_ecommerce/_search?q=*&sort=order_date:desc
16、使用 DSL查询(查询所有文档并按 order_id 降序排列,默认展示10个)
GET /kibana_sample_data_ecommerce/_search
{
"query": {"match_all": {}}
, "sort": [
{
"order_id": {
"order": "desc"
}
}
]
}
17、查询所有文档,数量size限制为1
GET /kibana_sample_data_ecommerce/_search
{
"query": {"match_all": {}}
, "size": 1
}
18、查询所有文档的 "currency","order_id" 字段,展示 第10个开始后的10个文档
GET /kibana_sample_data_ecommerce/_search
{
"query": {"match_all": {}}
, "_source": ["currency","order_id"]
, "from": 10
, "size": 10
}
19、查询文档 匹配customer_full_name 字段前缀是 Eddie的
GET /kibana_sample_data_ecommerce/_search
{
"query": {"match_phrase_prefix": {
"customer_full_name": "Eddie"
}}
}
20、查询文档 匹配customer_full_name字段中包含 Eddie 和Wolfe
GET /kibana_sample_data_ecommerce/_search
{
"query": {
"bool": {
"must": [
{"match": {
"customer_full_name": "Eddie"
}}
,{"match": {
"customer_full_name": "Wolfe"
}}
]
}
}
}
21、查询文档 匹配customer_full_name 包含 Eddie 或Wolfe
GET /kibana_sample_data_ecommerce/_search
{
"query": {
"bool": {
"should": [
{"match": {
"customer_full_name": "Eddie"}
}
,{"match": {
"customer_full_name": "Wolfe"
}}
]
}
}
}
22、查询文档 匹配customer_full_name中不包含Eddie
GET /kibana_sample_data_ecommerce/_search
{
"query": {
"bool": {
"must_not": [
{"match": {
"customer_full_name": "Eddie"
}}
]
}
}
}
23、查询文档 匹配customer_full_name 字段包含Eddie与不包含Wolfe的,且只查询customer_full_name字段
GET /kibana_sample_data_ecommerce/_search
{
"query": {
"bool": {
"must": [
{"match": {
"customer_full_name": "Eddie"
}}
]
, "must_not": [
{"match": {
"customer_full_name": "Wolfe"
}}
]
}
}
,"_source": "customer_full_name"
}
24、查询文档 匹配 customer_full_name包含Eddie并且taxful_total_price的值在50-100之间的
GET /kibana_sample_data_ecommerce/_search
{
"query": {
"bool": {
"must": [
{"match": {
"customer_full_name": "Eddie"
}}
]
,"filter": {
"range": {
"taxful_total_price": {
"gte": 50,
"lte": 100
}
}
}
}
}
}
25、按照 day_of_week 分组展示20个 并且求 组内taxful_total_price与taxless_total_price的均值,组间按照taxful_total_price 的均值倒序排列
size0 表示我们只在意分组统计结果,普通查询结果不关心。
GET /kibana_sample_data_ecommerce/_search
{
"aggs": {
"agg_by_day_of_week": {
"terms": {
"field": "day_of_week"
,"size": 20
, "order": {
"avg_taxful_total_price": "desc"
}
}
, "aggs": {
"avg_taxful_total_price": {
"avg": {
"field": "taxful_total_price"
}
}
,"avg_taxless_total_price":{
"avg": {
"field": "taxless_total_price"
}
}
}
}
}
, "size": 0
}
26、total_quantity按区间进行分组,组内再按day_of_week 进行二次分组,再求二次分组后taxless_total_price的均值。
GET /kibana_sample_data_ecommerce/_search
{
"size": 0
, "aggs": {
"agg_by_total_quantity": {
"range": {
"field": "total_quantity",
"ranges": [
{
"from": 0,
"to": 2
}
,{
"from": 2,
"to": 4
}
,{
"from": 4
, "to": 6
}
]
}
, "aggs": {
"agg_by_day_of_week": {
"terms": {
"field": "day_of_week"
}
, "aggs": {
"avg_taxless_total_price": {
"avg": {
"field": "taxless_total_price"
}
}
}
}
}
}
}