Elasticsearch 基本用法

目录

一个 Elasticsearch 请求和任何 HTTP 请求一样由若干相同的部件组成:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'

< > 标记的部件:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

VERB 适当的 HTTP 方法谓词 : GETPOSTPUTHEAD 或者 DELETE
PROTOCOL http 或者 https(如果你在 Elasticsearch 前面有一个 https 代理)
HOST Elasticsearch 集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。
PORT 运行 Elasticsearch HTTP 服务的端口号,默认是 9200
PATH API 的终端路径(例如 _count 将返回集群中文档数量)。Path 可能包含多个组件,例如:_cluster/stats_nodes/stats/jvm
QUERY_STRING 任意可选的查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)
BODY 一个 JSON 格式的请求体 (如果请求需要的话)

集群重启

1、关闭分片分配 PUT /_cluster/settings文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

curl -XPUT 'http://127.0.0.1:9200/_cluster/settings' -d '
{
    "transient":{"cluster.routing.allocation.enable": "none"}
}
'

2、 执行一个同步刷新 POST /_flush/synced文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

curl -XPOST 'http://127.0.0.1:9200/_flush/synced'​

3、 关闭所有节点文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

4、 优先启动主节点( node.master: true文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

node状态

  • 当前集群master文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/master?pretty=true&v'
    id                     host         ip           node
    0-sR1-c4TBSPB9WbNVWzNA 10.10.18.179 10.10.18.179 node-179
  • 查询整个集群的文档数量文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/count?v'
    epoch      timestamp count
    1502244880 10:14:40  2635961
  • 查看集群的健康状态文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/health?v'
    epoch      timestamp cluster          status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1502244979 10:16:19  spotmau_oss_test green           3         3     20  10    0    0        0             0                  -                100.0%
    
    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty'
    {
    "cluster_name" : "spotmau_oss_test",
    "status" : "green",
    "timed_out" : false,
    "number_of_nodes" : 3,
    "number_of_data_nodes" : 3,
    "active_primary_shards" : 10,
    "active_shards" : 20,
    "relocating_shards" : 0,
    "initializing_shards" : 0,
    "unassigned_shards" : 0,
    "delayed_unassigned_shards" : 0,
    "number_of_pending_tasks" : 0,
    "number_of_in_flight_fetch" : 0,
    "task_max_waiting_in_queue_millis" : 0,
    "active_shards_percent_as_number" : 100.0
    }
  • 查看索引信息文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/indices/*?v&s=docs.count:desc&pretty'
    health status index           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    green  open   megacorp        5Htj3xNkQV-aLSvB3GNhpw   5   1    2635951            0    174.1mb           87mb
    green  open   aggreation_test du8SNooNQZOPFRW597cj0Q   5   1         10            0     44.4kb         22.2kb
  • 集群的当前master节点信息文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/master?v'
    id                     host         ip           node
    0-sR1-c4TBSPB9WbNVWzNA 10.10.18.179 10.10.18.179 node-179
    
  • 集群中的节点信息文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/nodes?v'
    ip           heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
    10.10.18.179            9          52   0    0.00    0.00     0.00 mdi       *      node-179
    127.0.0.1           16          25   0    0.00    0.00     0.00 mdi       -      node-178
    127.0.0.1           15          24   0    0.00    0.00     0.00 mdi       -      node-177
  • 集群中的shard信息文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    The shards command is the detailed view of what nodes contain which shards.文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

    [root@flumelog-10-10-18-177 ~]# curl -XGET 'http://127.0.0.1:9200/_cat/shards?v'
    index           shard prirep state     docs  store ip           node
    megacorp        3     r      STARTED 526499 17.3mb 10.10.18.179 node-179
    megacorp        3     p      STARTED 526499 17.3mb 127.0.0.1 node-178
    megacorp        2     p      STARTED 525965 17.3mb 127.0.0.1 node-177
    megacorp        2     r      STARTED 525965 17.3mb 10.10.18.179 node-179
    megacorp        1     r      STARTED 527456 17.5mb 127.0.0.1 node-177
    megacorp        1     p      STARTED 527456 17.5mb 127.0.0.1 node-178
    megacorp        4     r      STARTED 528088 17.3mb 10.10.18.179 node-179
    megacorp        4     p      STARTED 528088 17.4mb 127.0.0.1 node-178
    megacorp        0     p      STARTED 527943 17.4mb 127.0.0.1 node-177
    megacorp        0     r      STARTED 527943 17.4mb 10.10.18.179 node-179
    aggreation_test 3     r      STARTED      2  5.3kb 10.10.18.179 node-179
    aggreation_test 3     p      STARTED      2  5.3kb 127.0.0.1 node-178
    aggreation_test 2     r      STARTED      3  5.6kb 127.0.0.1 node-177
    aggreation_test 2     p      STARTED      3  5.6kb 127.0.0.1 node-178
    aggreation_test 1     r      STARTED      4  5.9kb 127.0.0.1 node-177
    aggreation_test 1     p      STARTED      4  5.9kb 127.0.0.1 node-178
    aggreation_test 4     r      STARTED      1    5kb 10.10.18.179 node-179
    aggreation_test 4     p      STARTED      1    5kb 127.0.0.1 node-178
    aggreation_test 0     p      STARTED      0   130b 127.0.0.1 node-177
    aggreation_test 0     r      STARTED      0   130b 10.10.18.179 node-179

字段数据类型

核心数据类型:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

  • 字符串类型: string (6.0将会移除) 、 textkeyword
  • 数字型数据类型: long integer short byte double float
  • 日期型数据类型: date
  • 布尔型数据类型: boolean
  • 二进制数据类型: binary

复杂数据类型:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

  • 对象数据类型: object ,单独的JSON对象
  • 嵌套数据类型: nested JSON对象的数组

地理数据类型:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

  • 地理点数据类型: geo_point 经纬点
  • 地理形状数据类型: geo_shape 多边形的复杂地理形状

索引

索引管理

创建索引

curl -XPUT 'http://127.0.0.1:9200/school?pretty'

创建索引时修改分片和副本的数量文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

curl -XPUT 'http://127.0.0.1:9200/school?pretty' -d '
{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":0
    }
}
'

可以通过api 修改副本的数量文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

curl -XPUT 'http://127.0.0.1:9200/school/_settings?pretty' -d '
{
    "number_of_replicas": 1
}
'

打开/关闭索引

curl -XPOST 'http://127.0.0.1:9200/school/_open?pretty'
curl -XPOST 'http://127.0.0.1:9200/school/_close?pretty'

关闭的索引只能显示索引元数据信息,不能进行读写操作文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

删除索引

curl -XDELETE 'http://127.0.0.1:9200/school'

索引映射管理

增加映射

  curl -XPUT 'http://127.0.0.1:9200/school?pretty' -d '
  {
      "mappings":{
          "student": {
              "properties": {
                  "first_name": {"type": "text"},
                  "last_name": {"type": "text"},
                  "age": {"type": "integer"},
                  "about": {"type": "text"}
              }
          }
      }
  }
  '
curl -XPUT 'http://127.0.0.1:9200/school/_mapping/teacher?pretty' -d '
{
    "properties": {
        "name": {"type": "text"}
  }
}
'
curl -XPUT 'http://127.0.0.1:9200/aggreation_test/_mapping/order?pretty' -d '
{
    "properties": {
        "from": {"type": "integer", "index":"not_analyzed"}
  }
}
'

获取映射

curl -XGET 'http://127.0.0.1:9200/school/_mappings/student?pretty'
curl -XGET 'http://127.0.0.1:9200/school/_mapping/student/field/first_name?pretty'
curl -XHEAD '127.0.0.1:9200/school/_mapping/student?pretty'

文档管理

插入(更新)数据

curl -XPUT 'http://127.0.0.1:9200/school/student/3?pretty' -d '
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing"
}
'

返回结果:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

第二次调用返回:文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

{
  "_index" : "school",
  "_type" : "student",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",     //NOTE
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : false         //NOTE
}

注意更新、插入返回的结果表现形式的区别文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

Update

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}

bulk

curl -XPUT 'http://127.0.0.1:9200/_bulk' -d '
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "1" } }
{ "name" : "stu1", "age":21, "socre": 81 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "2" } }
{ "name" : "stu2", "age":22, "socre": 66 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "3" } }
{ "name" : "stu3", "age":23, "socre": 111 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "4" } }
{ "name" : "stu4", "age":22, "socre": 88 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "5" } }
{ "name" : "stu5", "age":21, "socre": 73 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "6" } }
{ "name" : "stu6", "age":24, "socre": 99 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "7" } }
{ "name" : "stu7", "age":27, "socre": 81 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "8" } }
{ "name" : "stu8", "age":26, "socre": 81 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "9" } }
{ "name" : "stu9", "age":25, "socre": 111 }
{ "index" : { "_index" : "aggreation_test", "_type" : "student", "_id" : "10" } }
{ "name" : "stu10", "age":29, "socre": 99 }
'

查询文档

curl -XGET 'http://127.0.0.1:9200/school/student/1'
curl -XGET  -u elastic:changeme '192.168.140.129:9200/corp/employee/1/_source'  # 只返回_source
curl -XGET  -u elastic:changeme '192.168.140.129:9200/corp/employee/1?_source=age'  #只返回age字段的内容

删除文档

curl -XDELETE 'http://127.0.0.1:9200/school/student/1'

清空文档

curl -XPOST 'http://127.0.0.1:9200/das_customer_service/work_order/_delete_by_query?pretty' -d '
{
"query" :{
    "match_all":{}
}
}
'

搜索

计算集群中文档的数量文章源自编程技术分享-https://mervyn.life/61dc4fc1.html

curl -XGET   '127.0.0.1:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'
文章源自编程技术分享-https://mervyn.life/61dc4fc1.html
weinxin
我的微信公众号
微信扫一扫
mervyn
从 mysql 用户的角度使用 Elasticsearch 数据库

从 mysql 用户的角度使用 Elasticsearch

本文主要讲述初次接触ES时,如何以使用MySQL的方式来应用 Elasticsearch 从而达到快速入门的目的。 以下例子均基于 Elasticsearch 6.0 例: order_test 表结...
Elasticsearch-PHP 遇到的坑 PHP

Elasticsearch-PHP 遇到的坑

大数据量分页查询报错 问题详情 在用elasticsearch-php分页查询时,分页几次后报错,错误内容如下: { "error":{ "root_cause"...
Elasticsearch高级应用-聚合 数据库

Elasticsearch高级应用-聚合

聚合的分类 度量聚合 在一组文档中队某个数字型字段进行计算得出指标值 分组聚合 创建多个分组,每个分组关联一个关键字和相关文档标准 管道聚合 这一类的聚合的数据源是其他聚合的输出,然后进行相关指标的计...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: