1. [基本命令]
  2. : curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/ <PATH>?<QUERY_STRING>' -d '<BODY>'
  3. // VERB HTTP方法: GET, POST, PUT, HEAD, DELETE
  4. // PROTOCOL http或者https协议 (只有在Elasticsearch前面有https代理的时候可用)
  5. // HOST Elasticsearch集群中的任何一个节点的主机名, 如果是在本地的节点, 那么就叫localhost
  6. // PORT Elasticsearch HTTP服务所在的端口, 默认为9200
  7. // PATH API路径 (例如_count将返回集群中文档的数量),
  8. // - PATH可以包含多个组件, 例如_cluster/stats或者_nodes/stats/jvm
  9. // QUERY_STRING 一些可选的查询请求参数, 例如?pretty参数将使请求返回更加美观易读的JSON数据
  10. // BODY 一个JSON格式的请求主体 (如果请求需要的话)
  11. // -i 显示响应头
  12. => pretty 将使请求返回更加美观易读的JSON数据
  13. # 查看
  14. : curl 'http://localhost:9200/'
  15. : curl 'http://localhost:9200/?pretty'
  16. {
  17. "name" : "localhost.localdomain",
  18. "cluster_name" : "elasticsearch",
  19. "cluster_uuid" : "75n5rGvyRi-6r5__H_ZA_g",
  20. "version" : {
  21. "number" : "7.2.0",
  22. "build_flavor" : "default",
  23. "build_type" : "tar",
  24. "build_hash" : "508c38a",
  25. "build_date" : "2019-06-20T15:54:18.811730Z",
  26. "build_snapshot" : false,
  27. "lucene_version" : "8.0.0",
  28. "minimum_wire_compatibility_version" : "6.8.0",
  29. "minimum_index_compatibility_version" : "6.0.0-beta1"
  30. },
  31. "tagline" : "You Know, for Search"
  32. }
  33. # 关闭
  34. : curl -XPOST 'http://localhost:9200/_shutdown'
  35. []
  36. : curl -H "Content-Type: application/json" -XPUT 'http://192.168.10.10:9200/' -d '{}'

索引

  1. []
  2. # 查看所有索引
  3. : GET /_cat/indices
  4. # 计算集群中的文档数量
  5. : GET /_count?pretty -d '{"query": {"match_all": {}}}'
  6. {
  7. "count" : 14325106,
  8. "_shards" : {
  9. "total" : 72,
  10. "successful" : 72,
  11. "skipped" : 0,
  12. "failed" : 0
  13. }
  14. }
  15. []


// JSON文档来表示一个用户对象

{
    "email":      "john@smith.com",
    "first_name": "John",
    "last_name":  "Smith",
    "info": {
        "bio":         "Eco-warrior and defender of the weak",
        "age":         25,
        "interests": [ "dolphins", "whales" ]
    },
    "join_date": "2014/05/01"
}

//

    : PUT 新增更新文档
  : GET/POST 查询搜索文档
  : DELETE 删除文档
  : HEAD   检查文档是否存在

[索引]

    # 列出所有索引
    : /_cat/indices?v

    # 新建文档
    : PUT /索引名/类型名/编号 {"字段": 值}
  : curl -H "Content-Type: application/json" -XPUT 'http://192.168.10.10:9200/megacorp/employee/1' -d '{"first_name" : "John"}'
    : PUT /megacorp/employee/1 {"first_name" : "John"}
  // "_index":"索引","_type":"类型","_id":"文档编号"

{
    "_index": "megacorp",
    "_type": "employee",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

[搜索]

    # 直接查询文档 [/索引/类型/文档编号 , 直接定位唯一文档]
  : GET /索引/类型/文档编号
{
    "_index": "索引",
    "_type": "类型",
    "_id": "文档编号",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "字段": 值
    }
}

    # 简单搜索, 查询该索引该类型的索引文档 [/索引/类型/_search]
    : _search
  // GET /索引/类型/_search
  // GET /索引/类型/_search?pretty

  # 查询字符串(query string)搜索, 字段 first_name值为John 数据的文档
  : GET /megacorp/employee/_search?q=first_name:John

  # DSL语句查询 (以JSON形式传入请求内容)
  : curl -H "Content-Type: application/json" \
    -XGET 'http://192.168.10.10:9200/megacorp/employee/_search?pretty'\
    -d '{ "query" : { "match" : { "first_name" : "John" } } }'
  : GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "first_name" : "John"
        }
    }
}

    # 复杂查询, DSL
  // 添加过滤器 (filter)
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "smith"
                }
            }
        }
    }
}

  # 全文搜索
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

    # 短语搜索
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}