[ElasticSearch]基本操作

本文介绍了ElasticSearch-5.6.3的基本操作,包括查看集群健康状况、节点及索引信息,演示了如何新建、查看、更新、删除索引和文档,以及批量创建文档等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ElasticSearch 基本操作

本篇博文介绍的是 ElasticSearch-5.6.3基本操作。

查看集群健康状况
curl -XGET 'localhost:9200/_cat/health?v&pretty'

执行结果如下所示:

epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1509052590 05:16:30  leo     yellow          1         1     26  26    0    0       26             0                  -                 50.0%

status有以下三种状态:
- green, 表示集群一切 ok;
- yellow, 数据可用但是一些分片未分配;
- red,部分数据不可用。这种情况就需要处理了。

查看集群所有节点
curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

执行结果如下所示:

host      ip        heap.percent ram.percent load node.role master name
127.0.0.1 127.0.0.1            6          97 3.54 d         *      Turac

可以发现,集群现在只有一个节点,命名为“Turac”。

查看集群所有索引
curl -XGET 'localhost:9200/_cat/indices?v'

结果如下所示:

health status index    pri rep docs.count docs.deleted store.size pri.store.size
yellow open   myweibo1   5   1          7            0     22.3kb         22.3kb

目前只用一个索引“myweibo1”。

新建索引
##新键索引
curl -XPUT 'localhost:9200/employees'
## 查看索引
curl -XGET 'localhost:9200/_cat/indices?v'
##查看_settings
curl -XGET 'localhost:9200/employees/_settings'

新建索引后,执行查看索引语句结果如下:

yellow open   employees   5   1          0            0       260b           260b

新增了employees这个索引。
查看_settings,结果如下:

    "employees": {
        "settings": {
            "index": {
                "creation_date": "1509088075357",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "0twhlJ89QuSF1Y1p_Mj7dw",
                "version": {
                    "created": "2040699"
                }
            }
        }
    }
}

其中 number_of_shards 的默认值是5,number_of_replicas 的默认值是1,当然建立索引时可以如下进行设置:

curl -XPUT 'localhost:9200/employees' -d '{
    "settings": {
            "index": {
                "number_of_shards": "3",
                "number_of_replicas": "1",
              }
        }
   }'

再次查看_settings,结果如下:

{
    "employees": {
        "settings": {
            "index": {
                "creation_date": "1509088734594",
                "number_of_shards": "3",
                "number_of_replicas": "1",
                "uuid": "xBVUUhm5SwWvoMQoTW8y1w",
                "version": {
                    "created": "2040699" }
            }
        }
    }
}
删除索引
curl -XDELETE 'localhost:9200/employees/'

执行查看索引命令,索引“employees ”就没有了。

创建文档
curl -XPUT "http://localhost:9200/<index>/<type>/[<id>]" -d'{JSON}'

以上是ES 创建文档的一般格式,其中 index 和 type 是必选项,id 可选。
指定 id 如下所示:

curl -XPOST "http://localhost:9200/employees/employee/1" -d'
{
    "name":"Leo",
    "age":"27",
    "tel":12345678901
}'

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "1",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

不指定 id 如下所示:

curl -XPOST "http://localhost:9200/employees/employee/" -d'
{
    "name":"Ming",
    "age":"27",
    "tel":12345643901
}'

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "AV9czyvByznybavM0vVu",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

如上所示,当不指定id时,系统会自动分配一个 id,本例中 id 为“AV9czyvByznybavM0vVu”

查看文档
 curl -XGET "http://localhost:9200/employees/employee/1"

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "Leo",
        "age": "27",
        "tel": 12345678901
    }
}

只查看_source字段

curl -XGET 'http://localhost:9200/employees/employee/1/_source'

执行结果如下所示:

{
    "name":"Leo",
    "age":"27",
    "tel":12345678901
}
更新文档
curl -XPOST "http://localhost:9200/employees/employee/1" -d'
{
    "name":"Leo",
    "age":"27",
    "tel":23415678901
}'

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": false
}

由于前面在创建文档时已经创建了 id 为1的雇员,所以此时是执行的更新操作,”_version”字段从1变为了2, “result”字段为updated,执行了修改电话号码的操作。如果索引中无 id 为1的雇员,此时会新建一个文档。

删除文档
curl -XDELETE "http://localhost:9200/employees/employee/1" 

文档删除。

批量创建文档
curl -XPOST 'localhost:9200/employees/employee/_bulk?pretty&pretty'  -d'
{"index":{"_id":"2"}}
{"name": "John Doe", "age":"28", "tel":234567776}
{"index":{"_id":"3"}}
{"name": "Jane Doe", "age":"38", "tel":234334764}
'
从文件导入数据到索引中

以官网数据account.json为例。下载问价到 ElasticSearch根目录下,执行以下命令:

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
curl 'localhost:9200/bank/account/_count'

执行结果如下所示:

{
    "count": 1000,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    }
}

共有1000条数据导入。

清理索引缓存
curl -XPOST "http://localhost:9200/employees/_cache/clear"
刷新索引数据
curl -XPOST "http://localhost:9200/employees/_refresh"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值