查看集群

1. 查看集群健康

  1. curl -X GET "localhost:9200/_cat/health?v"

2. 查看集群节点

  1. curl -X GET "localhost:9200/_cat/nodes?v"

3. 查看集群所有索引

  1. curl -X GET "localhost:9200/_cat/indices?v"

get 获取指定数据

1. 直接获取数据

  1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/AWSudIFgTuj3oZBEhyxK?pretty"

格式为 /{index}/{type}/{id}

字段 含义
monitor_log_mch_order_out 索引 (_index)
logs 索引的类型 (_type), 不知道类型可以用 _all 匹配
AWSudIFgTuj3oZBEhyxK id (_id)
pretty json格式显示数据, 可省略

2. 屏蔽或只查看 _source

  1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/AWSudIFgTuj3oZBEhyxK?pretty&_source=false"

添加 _source=false 即可

  1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/AWSudIFgTuj3oZBEhyxK/_source?pretty"

3. 过滤字段

  1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/AWSudIFgTuj3oZBEhyxK?pretty&_source_include=log*&_source_exclude=logType"

获取包含 log* 且不为 logType 的字段

  1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/AWSudIFgTuj3oZBEhyxK?pretty&_source=logType,logLevel"

只查询指定字段的简易写法

mget 多条件匹配查询

  1. 匹配多个索引, 同时查询多个id的数据

    1. curl -X GET "localhost:9200/_mget?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "docs" : [
    4. {
    5. "_index" : "monitor_log_mch_order_out",
    6. "_type" : "logs",
    7. "_id" : "AWSudIFgTuj3oZBEhyxK"
    8. },
    9. {
    10. "_index" : "monitor_log_mch_order_out",
    11. "_type" : "logs",
    12. "_id" : "AWSuXewETuj3oZBEhywS"
    13. }
    14. ]
    15. }
    16. '
  2. 可以将索引写在host后面, 代表查询的都为同一索引下的数据

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/_mget?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "docs" : [
    4. {
    5. "_type" : "logs",
    6. "_id" : "AWSudIFgTuj3oZBEhyxK"
    7. },
    8. {
    9. "_type" : "logs",
    10. "_id" : "AWSuXewETuj3oZBEhywS"
    11. }
    12. ]
    13. }
    14. '
  3. 合并index和type, 代表查询的都为同一索引下type也相同的数据

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/_mget?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "docs" : [
    4. {
    5. "_id" : "AWSudIFgTuj3oZBEhyxK"
    6. },
    7. {
    8. "_id" : "AWSuXewETuj3oZBEhywS"
    9. }
    10. ]
    11. }
    12. '

    简化后如下:

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/_mget?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "ids" : ["AWSudIFgTuj3oZBEhyxK", "AWSuXewETuj3oZBEhywS"]
    4. }
    5. '

    注: 当多个条件的 _type 相同时 可以使用 _all 或者省略

  4. 过滤字段, 每个Id分别对 _source进行过滤

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/_mget?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "docs" : [
    4. {
    5. "_id" : "AWSudIFgTuj3oZBEhyxK",
    6. "_source" : false
    7. },
    8. {
    9. "_id" : "AWSuXewETuj3oZBEhywS",
    10. "_source" : ["bizId", "method"]
    11. },
    12. {
    13. "_id" : "AWSuLAYqTuj3oZBEhysH",
    14. "_source" : {
    15. "include": ["log*"],
    16. "exclude": ["logLevel"]
    17. }
    18. }
    19. ]
    20. }
    21. '

    _search 搜索

    1. 匹配bizId 查询

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/_search?pretty&q=bizId:2009011201807190133430748068"

    2. 同时指定类型

    同时指定类型, 多个类型用 ‘,’ 隔开, 也支持多个索引勇士搜索, 多个索引用 ‘,’ 隔开, 或者模糊搜索

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/_search?pretty&q=bizId:2009011201807190133430748068"

    3. 占位符 _all 匹配所有索引

    1. curl -X GET "localhost:9200/_all/logs/_search?pretty&q=bizId:2009011201807190133430748068"

    4. 匹配所有索引所有类型

    1. curl -X GET "localhost:9200/_search?pretty&q=bizId:2009011201807190133430748068"

    注: q 代表映射query_string

    5. 请求体的方式

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/_search?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "query" : {
    4. "term" : { "bizId" : "2009011201807190133430748068" }
    5. }
    6. }
    7. '

    6. 分页查询 from/size

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/logs/_search?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "from" : 0, "size" : 1,
    4. "query" : {
    5. "term" : { "bizId" : "2009011201807190133430748068" }
    6. }
    7. }
    8. '

    7. 查询并过滤字段

    根据字段查询并筛选掉指定字段

    1. curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "_source": {
    4. "includes": [ "costTime", "bizId" ],
    5. "excludes": [ "logLevel" ]
    6. },
    7. "query" : {
    8. "term" : { "bizId" : "2009011201807190133430748068" }
    9. }
    10. }
    11. '

    范围查询

    1. 按照时间范围查询

    可以省略索引查询全部

    1. curl -X GET "localhost:9200/monitor_log_mch_order_out/_search?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "query": {
    4. "range" : {
    5. "time" : {
    6. "gte": "2018-07-19 00:14:25:000",
    7. "lte": "2018-07-19 00:14:30:000",
    8. "format": "yyyy-MM-dd HH:mm:ss:SSS"
    9. }
    10. }
    11. }
    12. }
    13. '