【原创】elasticsearch入门命令

安装

命令

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.tar.gz  
tar -xvzf elasticsearch-6.4.0.tar.gz  
cd elasticsearch-6.4.0/bin
./elasticsearch -d

修改配置文件

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.141.129
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#

再次启动报错:

[2018-09-13T09:29:43,060][INFO ][o.e.b.BootstrapChecks    ] [7hyiUY2] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解决方案:

vi /etc/security/limits.conf # 添加两行行配置,并重连SSH
elasticsearch soft nofile 65536
elasticsearch hard nofile 65537

vi /etc/sysctl.conf # 添加一行配置
vm.max_map_count=262144
sysctl -p

页面访问

地址:
http://192.168.141.129:9200/?pretty
显示:

基础概念

集群(Cluster)

一个集群是由一个或多个节点(服务器)组成的,通过所有的节点一起保存你的全部数据并且提供联合索引和搜索功能的节点集合。每个集群有一个唯一的名称标识,默认是“elasticsearch”。这个名称非常重要,因为一个节点(Node)只有设置了这个名称才能加入集群,成为集群的一部分。

节点(Node)

一个节点是一个单一的服务器,是你的集群的一部分,存储数据,并且参与集群的索引和搜索功能。跟集群一样,节点在启动时也会被分配一个唯一的标识名称,这个名称默认是一个随机的UUID(Universally Unique IDentifier)。如果你不想用默认的名称,你可以自己定义节点的名称。这个名称对于管理集群节点,识别哪台服务器对应集群中的哪个节点有重要的作用。

索引(Index)

一个索引就是含有某些相似特性的文档的集合。

类型(Type)[Deprecated]

一个类型是你的索引中的一个分类或者说是一个分区,它可以让你在同一索引中存储不同类型的文档,例如,为用户建一个类型,为博客文章建另一个类型。现在已不可能在同一个索引中创建多个类型,并且整个类型的概念将会在未来的版本中移除。
比如天气weather这个 Index 索引里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。

文档(Document)

一个文档是一个可被索引的数据的基础单元。例如,你可以给一个单独的用户创建一个文档,给单个产品创建一个文档,以及其他的单独的规则。这个文档用JSON格式表现。

在一个索引或类型中,你可以根据自己的需求存储任意多的文档。注意,虽然一个文档在物理存储上属于一个索引,但是文档实际上必须指定一个在索引中的类型。

分片(Shard)

前面已经提到,集群能够存储超出单机容量的信息。为了实现这种需求,ElasticSearch把数据分发到多个存储Lucene索引的物理机上。这些Lucene索引称为分片索引,这个分发的过程称为索引分片(Sharding)。在ElasticSearch集群中,索引分片(Sharding)是自动完成的,而且所有分片索引(Shard)是作为一个整体呈现给用户的。需要注意的是,尽管索引分片这个过程是自动的,但是在应用中需要事先调整好参数。因为集群中分片的数量需要在索引创建前配置好,而且服务器启动后是无法修改的,至少目前无法修改。

副本(Replica)

通过索引分片机制(Sharding)可以向ElasticSearch集群中导入超过单机容量的数据,客户端操作任意一个节点即可实现对集群数据的读写操作。当集群负载增长,用户搜索请求阻塞在单个节点上时,通过索引副本(Replica)机制就可以解决这个问题。索引副本(Replica)机制的的思路很简单:为索引分片创建一份新的拷贝,它可以像原来的主分片一样处理用户搜索请求。同时也顺便保证了数据的安全性。即如果主分片数据丢失,ElasticSearch通过索引副本使得数据不丢失。索引副本可以随时添加或者删除,所以用户可以在需要的时候动态调整其数量。

命令

集群健康监控

curl -XGET 192.168.141.129:9200/_cat/health?v&pretty
GET /_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
1536806018 10:33:38  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

获取集群中的节点列表

curl -XGET 192.168.141.129:9200/_cat/nodes?v&pretty
GET /_cat/nodes?v

响应

ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.141.129           24          89   2    0.00    0.10     0.18 mdi       *      7hyiUY2

列出所有的索引

curl -XGET 192.168.141.129:9200/_cat/indices?v&pretty
GET /_cat/indices?v

响应

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana e9VV3rJSTduGLdcwqg2ZKw   1   0          1            0        4kb            4kb

创建索引

curl -XPUT 192.168.141.129:9200/customer?pretty
PUT /customer?pretty

响应

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "customer"
}

删除索引

curl -XDELETE 192.168.141.129:9200/customer?pretty
DELETE /customer?pretty

响应

{
  "acknowledged": true
}

创建文档

# 无id
curl -XPOST 192.168.141.129:9200/customer/doc?pretty -H 'Content-Type: application/json' -d '{"name": "John Doe"}'
POST /customer/doc?pretty
{
  "name": "John Doe"
}

# 有id
curl -XPUT 192.168.141.129:9200/customer/doc/1/_create?pretty -H 'Content-Type: application/json' -d '{"name": "John Doe"}'
PUT /customer/doc/1/_create
{
  "name": "John Doe"
}

响应

{
  "_index": "customer",
  "_type": "doc",
  "_id": "6zaU0WUBANcevg0yn4q5",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}
{
  "_index": "customer",
  "_type": "doc",
  "_id": "5",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

更新文档

curl -XPOST 192.168.141.129:9200/customer/doc/1/_update?pretty -H 'Content-Type: application/json' -d '{"doc": { "name": "Jane Doe" }}'
POST /customer/doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe" }
}

响应

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 27,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 27,
  "_primary_term" : 1
}

查看文档

curl -XGET 192.168.141.129:9200/customer/doc/1?pretty
GET /customer/doc/1?pretty

响应

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

删除文档

curl -XDELETE 192.168.141.129:9200/customer/doc/2?pretty
DELETE /customer/doc/2?pretty

响应

{
  "_index": "customer",
  "_type": "doc",
  "_id": "2",
  "_version": 2,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

批处理

curl -XPOST '192.168.141.129:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'

POST /customer/doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

响应

{
  "took": 9,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "customer",
        "_type": "doc",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 29,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "customer",
        "_type": "doc",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 4,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

搜索API

闷头造测试数据中,api命令还未学习整理。

猜你喜欢

转载自www.cnblogs.com/Candies/p/9638886.html