欢迎投稿

今日深度:

elasticsearch详解(四)——elasticsearch数据的基本操作,

elasticsearch详解(四)——elasticsearch数据的基本操作,


和elasticsearch交互有两个方式,一种是http的方式,另一种是es的java客户端用tcp,用java代码交互,这也是前面我们看到的elasticsearch会暴漏出两个9200httpserver、9300tcpserver。

http的方式最为简单且常用,这里列举一些es常用的数据操作rest接口。

这里http的调试客户端用postman,一个功能强大的http调试客户端。
github地址:https://github.com/postmanlabs/newman
官网地址:https://www.getpostman.com/
下载后打开,看一下应该都会用的

1.集群的常用操作

1.1集群的健康

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
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

1.2集群所有的节点

GET /_cat/nodes?v
ip           heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.2.12           21          68   0    0.03    0.04     0.05 mdi       -      j29ev7V
192.168.2.11           22          77  11    2.33    2.47     2.48 mdi       *      HXd0iop

1.3 集群所有的索引

GET /_cat/indices?v
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   fdsds   t4wSTgd8TTaNB3V-AzoHnw   5   1          0            0      1.8kb           955b
green  open   .kibana tqFB5-9XRBOgjdN9-_LukA   1   1          3            0     31.8kb         15.9kb
green  open   test    j5QAMePIQOCqQSgA6ubbUw   5   1          0            0      1.8kb           955b

以上api的规则可以看到都是/_cat/xxx,这是cat api的规则,更多cat api查看这里
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cat.html

2.索引的基本操作

2.1创建索引

PUT /testindex?pretty

pretty参数是将返回结果格式化的

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

查看索引信息

GET /testindex?pretty
{
    "testindex": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1511149556373",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "ASzur3xMTY6SL_IU6pLEyw",
                "version": {
                    "created": "5060499"
                },
                "provided_name": "testindex"
            }
        }
    }
}

2.2索引数据插入

PUT /testindex/testtype/1?pretty
{

    "name":"test",
    "age":20
}
{
    "_index": "testindex",
    "_type": "testtype",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

查看插入的数据

GET /testindex/testtype/1?pretty
{
    "_index": "testindex",
    "_type": "testtype",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "test",
        "age": 20
    }
}

2.3删除索引

DELETE /testindex?pretty
{
    "acknowledged": true
}

3.数据的基本操作

3.1 插入数据

2.2已经说明了插入数据的api
语法为:PUT /[index]/[type]/[id] +json数据

3.2 修改数据

语法为:POST /[index]/[type]/[id]+更新的json数据

POST /testindex/testtype/1?pretty
{

    "name":"test1",
    "age":20
}
{
    "_index": "testindex",
    "_type": "testtype",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": false
}

3.3 删除数据

语法为:DELETE /[index]/[type]/[id]

DELETE /testindex/testtype/1
{
    "found": true,
    "_index": "testindex",
    "_type": "testtype",
    "_id": "1",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    }
}

以上是集群的基本操作,用作熟悉elasticsearch,配合head插件来了解elasticsearch更容易。
更多api请访问官网
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html
document apis,search apis,aggregations,indices apis,cat apis,cluster apis等

www.htsjk.Com true http://www.htsjk.com/Elasticsearch/28086.html NewsArticle elasticsearch详解(四)——elasticsearch数据的基本操作, 和elasticsearch交互有两个方式,一种是http的方式,另一种是es的java客户端用tcp,用java代码交互,这也是前面我们看到的elasticsearch会暴...
相关文章
    暂无相关文章
评论暂时关闭