{"ctsdb_test" : {"tags" : {"region" : "string"},"time" : {"name" : "timestamp","format" : "epoch_millis"},"fields" : {"cpuUsage" : "float","diskUsage" : "string","dcpuUsage" : "integer"},"options" : {"expire_day" : 7,"refresh_interval" : "10s","number_of_shards" : 5,"indexed_fields" : "cpuUsage"}}}
参数名称 | 描述 |
gte | 大于或等于 |
gt | 大于 |
lte | 小于或等于 |
lt | 小于 |
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"range": {"timestamp": {"gte": "01/01/2018","lte": "03/01/2018","format": "MM/dd/yyyy","time_zone":"+08:00"}}}}'
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"range": {"cpuUsage": {"gte": 1.0,"lte": 10.0}}}}'
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"terms": {"region": ["sh", "bj"]}}}'
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"bool": {"filter": [{"range": {"timestamp": {"format": "yyyy-MM-dd HH:mm:ss","gte": "2017-11-06 23:00:00","lt": "2018-03-06 23:05:00","time_zone":"+08:00"}}},{"terms": {"region": ["sh"]}},{"terms": {"cpuUsage": ["2.0"]}}]}},"docvalue_fields": ["cpuUsage","timestamp"]}'
timestamp>='2017-11-06 23:00:00' AND timestamp<'2018-03-06 23:05:00' AND region=sh AND cpuUsage=2.0
。curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"bool": {"filter": [{"range": {"timestamp": {"format": "yyyy-MM-dd HH:mm:ss","gte": "2017-11-06 23:00:00","lt": "2018-11-06 23:05:00","time_zone":"+08:00"}}},{"term": {"region": "gz"}}],"should": [{"terms": {"cpuUsage": ["2.0"]}},{"terms": {"cpuUsage": ["2.5"]}}],"minimum_should_match": 1}},"docvalue_fields": ["cpuUsage","timestamp"]}'
timestamp>='2017-11-06 23:00:00' AND timestamp<'2018-11-06 23:05:00' AND region='gz' AND (cpuUsage=2.0 or cpuUsage=2.5)
。minimum_should_match 参数的意义在于设置 cpuUsage=2.0 和 cpuUsage=2.5 至少匹配的个数,系统默认 minimum_should_match 为0。curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"bool": {"filter": [{"range": {"timestamp": {"format": "yyyy-MM-dd HH:mm:ss","gte": "2017-11-06 23:00:00","lt": "2018-11-06 23:05:00","time_zone":"+08:00"}}},{"terms": {"region": ["gz"]}}],"must_not": [{"terms": {"cpuUsage": ["2.0"]}}]}},"docvalue_fields": ["cpuUsage","timestamp"]}'
timestamp>=2017-11-06 23:00:00 AND timestamp<2018-11-06 23:05:00 AND region='gz' AND cpuUsage !=2.0
。curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"from": 0,"size": 5,"query": {"bool": {"filter": {"range": {"timestamp": {"gte": "01/01/2018","lte": "03/01/2018","format": "dd/MM/yyyy","time_zone":"+08:00"}}},"must_not": {"terms": {"region": ["bj"]}}}}}'
_scroll_id
字段,后续遍历可以通过指定前一次遍历返回的 _scroll_id
来进行新的遍历,直到返回结果为空;初始化和遍历两个阶段都可以通过指定 scroll 参数来设定遍历的上下文环境保留时间,过期后 _scroll_id
将变得无效。时间格式如下表所示:格式 | 描述 |
d | days |
h | hours |
m | minutes |
s | seconds |
ms | milliseconds |
micros | microseconds |
nanos | nanoseconds |
curl -u root:le201909 -H 'Content-Type:application/json' -X POST 172.xx.xx.4:9201/ctsdb_test/_search?scroll=1m -d'{"size":5,"query": {"bool": {"filter": [{"terms": {"region": ["gz"]}}]}},"docvalue_fields": ["cpuUsage","region","timestamp"]}'
{"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoAwAAAAAADrOFFm5YSEhnMjdnUWNPcndHS1k5Wjc3bHcAAAAAAAz_1RZiRkZTcGp4dFRXR18xMGtzSmhEUFJRAAAAAAAP5vQWOXFOR29lc0hROHFWMmFGTkVmSkxmZw==","took": 10641,"timed_out": false,"_shards": {"total": 3,"successful": 3,"skipped": 0,"failed": 0},"hits": {"total": {"value":1592072666,"relation":"eq"},"max_score": 0.65708643,"hits": [{"_index": "ctsdb_test@0_-1","_type": "doc","_id": "oyylNU0U65cZjByyt7sW_JmPPgAACY4Bh0","_score": 0.65708643,"_routing": "354d14eb","fields": {"region": ["gz"],"cpuUsage": ["2.0"],"timestamp": [1509909300000]}},{"_index": "ctsdb_test@0_-1","_type": "doc","_id": "oyykFN0yd1d9NDPfzjRdrJ8whQAACYsBqc","_score": 0.65708643,"_routing": "14dd3277","fields": {"region": ["gz"],"cpuUsage": ["1.8"],"timestamp": [1509908340000]}},{"_index": "ctsdb_test@0_-1","_type": "doc","_id": "oyylHLp9jl_3sF4N2rnh67h4SgAABHIBso","_score": 0.65708643,"_routing": "1cba7d8e","fields": {"region": ["gz"],"cpuUsage": ["2.5"],"timestamp": [1509909720000]}},{"_index": "ctsdb_test@0_-1","_type": "doc","_id": "oyylH2JsOKnFHGUinQ7jM-ZwkgAAAvcBso","_score": 0.65708643,"_routing": "1f626c38","fields": {"region": ["gz"],"cpuUsage": ["2.1"],"timestamp": [1509909720000]}},{"_index": "ctsdb_test@0_-1","_type": "doc","_id": "oyylHLp9jl_3sF4N2rnh67h4SgAABGsBso","_score": 0.65708643,"_routing": "1cba7d8e","fields": {"region": ["gz"],"cpuUsage": ["2.0"],"timestamp": [1509909720000]}}]}}
curl -u root:le201909 -H 'Content-Type:application/json' -X POST 172.xx.xx.4:9201/_search/scroll -d'{"scroll" : "1m","scroll_id" : "DnF1ZXJ5VGhlbkZldGNoAwAAAAAADrOFFm5YSEhnMjdnUWNPcndHS1k5Wjc3bHcAAAAAAAz_1RZiRkZTcGp4dFRXR18xMGtzSmhEUFJRAAAAAAAP5vQWOXFOR29lc0hROHFWMmFGTkVmSkxmZw=="}'
_scroll_id
值。而下一次遍历则需要将 scroll_id 参数调整为上一次遍历返回的 _scroll_id
值,即每次请求中的 scroll_id 参数是上一次请求返回的 _scroll_id
值,直到返回结果为空,遍历结束。_scroll_id
值可能相同,无法利用 _scroll_id
进行指定页跳转。curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"bool": {"must": {"range": {"timestamp": {"gte": "01/01/2018","lte": "03/01/2018","format": "MM/dd/yyyy","time_zone":"+08:00"}}}}},"sort": [{"cpuUsage": {"order": "asc","mode": "min"}},{"timestamp": {"order": "asc"}},"diskUsage"]}'
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"terms": {"region": ["sh", "bj"]}},"docvalue_fields": ["timestamp", "cpuUsage"]}'
字段名称 | 描述 |
hits | 返回匹配的查询结果,其中 total 字段表示参与聚合的数据条数。里面的 hits 字段为数组结构,若无指定,则包含所有查询结果的前十条。hits 数组里的每个结果中包含 _index (查询涉及到的 子表),若查询中指定了 docvalue_fields,则会返回 fields字段具体指明每个字段的值。 |
took | 整个查询耗费的毫秒数。 |
_shards | 参与查询的分片数。其中 total 标识总分片数,successful 标识执行成功的数量,failed 标识执行失败的数量,skipped 标识跳过执行的分片数。 |
timed_out | 查询是否超时,取值有 false 和 true。 |
aggregations | 聚合返回结果。 |
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"size":0,"query": {"terms": {"region": ["sh", "bj"]}},"aggs": {"myname": {"max": {"field": "cpuUsage"}}}}'
{"took": 1,"timed_out": false,"_shards": {"total": 20,"successful": 20,"skipped": 0,"failed": 0},"hits": {"total": {"value":7,"relation":"eq"},"max_score": 0,"hits": []},"aggregations": {"myname": {"value": 4}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search?filter_path=aggregations -d'{"aggs": {"myname": {"terms": {"field":"region"}}}}'
{"aggregations": {"myname": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "sh","doc_count": 10},{"key": "Motor_sports","doc_count": 6},{"key": "gz","doc_count": 3},{"key": "bj","doc_count": 2},{"key": "cd","doc_count": 2},{"key": "Winter_sports","doc_count": 1},{"key": "water_sports","doc_count": 1}]}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search?filter_path=aggregations -d'{"aggs": {"myname": {"terms": {"field":"region","size":5}}}}'
{"aggregations": {"myname": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 2,"buckets": [{"key": "sh","doc_count": 10},{"key": "Motor_sports","doc_count": 6},{"key": "gz","doc_count": 3},{"key": "bj","doc_count": 2},{"key": "cd","doc_count": 2}]}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search?filter_path=aggregations -d'{"aggs": {"myname": {"terms": {"field":"region","order":{"_count":"desc"}}}}}'
{"aggregations": {"myname": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "sh","doc_count": 10},{"key": "Motor_sports","doc_count": 6},{"key": "gz","doc_count": 3},{"key": "bj","doc_count": 2},{"key": "cd","doc_count": 2},{"key": "Winter_sports","doc_count": 1},{"key": "water_sports","doc_count": 1}]}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search?filter_path=aggregations -d'{"aggs": {"myname": {"terms": {"field":"region","order":{"_term":"asc"}}}}}'
{"aggregations": {"myname": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Motor_sports","doc_count": 6},{"key": "Winter_sports","doc_count": 1},{"key": "bj","doc_count": 2},{"key": "cd","doc_count": 2},{"key": "gz","doc_count": 3},{"key": "sh","doc_count": 10},{"key": "water_sports","doc_count": 1}]}}}
water_
开头的数据进行聚合并返回结果):curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search?filter_path=aggregations -d'{"aggs": {"myname": {"terms": {"field":"region","include" : ".*sport.*","exclude" : "water_.*"}}}}'
{"aggregations": {"myname": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Motor_sports","doc_count": 6},{"key": "Winter_sports","doc_count": 1}]}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search?filter_path=aggregations -d'{"aggs" : {"region_zone" : {"terms" : {"field" : "region","include" : ["sh", "bj","cd","gz"]}},"region_sports" : {"terms" : {"field" : "region","exclude" : ["sh", "bj","cd","gz"]}}}}'
{"aggregations": {"region_sport": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Motor_sports","doc_count": 6},{"key": "Winter_sports","doc_count": 1},{"key": "water_sports","doc_count": 1}]},"region_zone": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "sh","doc_count": 10},{"key": "gz","doc_count": 3},{"key": "bj","doc_count": 2},{"key": "cd","doc_count": 2}]}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"terms": {"region": ["sh", "bj"]}},"aggs": {"time_1h_agg": {"date_histogram": {"field": "timestamp","interval": "1h"},"aggs": {"avgCpuUsage": {"avg": {"field": "cpuUsage"}}}}}}'
{"took": 5,"timed_out": false,"_shards": {"total": 20,"successful": 20,"skipped": 0,"failed": 0},"hits": {"total": {"value":6,"relation":"eq"},"max_score": 0.074107975,"hits": [{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2QtGR5xcjRaw2ETf-","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2QtGR5xcjRaw2ETf_","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2Q5Xr5xcjRaw2ETgA","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2Q5Xr5xcjRaw2ETgB","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2RGfF5xcjRaw2ETgC","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2RGfF5xcjRaw2ETgD","_score": 0.074107975,"_routing": "sh"}]},"aggregations": {"time_1h_agg": {"buckets": [{"key_as_string": "1520222400","key": 1520222400000,"doc_count": 1,"avgCpuUsage": {"value": 2.5}},{"key_as_string": "1520226000","key": 1520226000000,"doc_count": 0,"avgCpuUsage": {"value": null}},{"key_as_string": "1520229600","key": 1520229600000,"doc_count": 0,"avgCpuUsage": {"value": null}},{"key_as_string": "1520233200","key": 1520233200000,"doc_count": 0,"avgCpuUsage": {"value": null}},{"key_as_string": "1520236800","key": 1520236800000,"doc_count": 0,"avgCpuUsage": {"value": null}},{"key_as_string": "1520240400","key": 1520240400000,"doc_count": 0,"avgCpuUsage": {"value": null}},{"key_as_string": "1520244000","key": 1520244000000,"doc_count": 0,"avgCpuUsage": {"value": null}},{"key_as_string": "1520247600","key": 1520247600000,"doc_count": 1,"avgCpuUsage": {"value": 2}},{"key_as_string": "1520251200","key": 1520251200000,"doc_count": 4,"avgCpuUsage": {"value": 2.25}}]}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"terms": {"region": ["sh", "bj"]}},"aggs":{"myname":{"percentiles":{"field": "cpuUsage","percents": [1,25,50,70,99]}}}}'
{"took": 18,"timed_out": false,"_shards": {"total": 20,"successful": 20,"skipped": 0,"failed": 0},"hits": {"total": {"value":6,"relation":"eq"},"max_score": 0.074107975,"hits": [{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2QtGR5xcjRaw2ETf-","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2QtGR5xcjRaw2ETf_","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2Q5Xr5xcjRaw2ETgA","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2Q5Xr5xcjRaw2ETgB","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2RGfF5xcjRaw2ETgC","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2RGfF5xcjRaw2ETgD","_score": 0.074107975,"_routing": "sh"}]},"aggregations": {"myname": {"values": {"1.0": 2,"25.0": 2,"50.0": 2.25,"70.0": 2.5,"99.0": 2.5}}}}
curl -u root:le201909 -H 'Content-Type:application/json' -X GET 172.xx.xx.4:9201/ctsdb_test/_search -d'{"query": {"terms": {"region": ["sh", "bj"]}},"aggs":{"myname":{"cardinality":{"field": "cpuUsage"}}}}'
{"took": 15,"timed_out": false,"_shards": {"total": 20,"successful": 20,"skipped": 0,"failed": 0},"hits": {"total": {"value":6,"relation":"eq"},"max_score": 0.074107975,"hits": [{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2QtGR5xcjRaw2ETf-","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2QtGR5xcjRaw2ETf_","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2Q5Xr5xcjRaw2ETgA","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2Q5Xr5xcjRaw2ETgB","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2RGfF5xcjRaw2ETgC","_score": 0.074107975,"_routing": "sh"},{"_index": "ctsdb_test@1520092800000_3","_type": "doc","_id": "AWH2RGfF5xcjRaw2ETgD","_score": 0.074107975,"_routing": "sh"}]},"aggregations": {"myname": {"value": 2}}}
本页内容是否解决了您的问题?