Elasticsearch(二)——Rest API

weixin_34185560發表於2018-09-27

RESTful 介面 URL 的格式是:
http://cluster的地址: 9200/<index>I<type>I [<id>]
其中,index, type 是必須提供的( index 可以理解為資料庫;type 理解為資料表); id 是可選的(相當於資料庫表中記 錄的主鍵是唯一的。如果不提供, Elasticsearch 會向動生成。增 、刪、改,查分別對應 HTTP 請求的 PUT 、DELETE、POST、GET方法。

Kibana DevTools

143845-85259a9b4dc64865.png
image.png
建立資料

建立index是province,type是citys ,當前id為1的document資料

PUT /province/citys/1
{
  "name":"北京",
  "code":"010"
}

返回結果

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

不適用id建立資料

POST /province/citys/
{
  "name":"上海",
  "code":"021"
}

返回結果

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

修改資料

只想修改id唯一的field是name的值

POST /province/citys/1/_update
{
  "doc":{
    "name":"廣州"
  }
}
替換資料
PUT /province/citys/1
{
  "name":"廣州"
}

這個行為是替換資料,和修改資料是兩種不同形式的資料更新

刪除資料
DELETE /province/citys/1

返回結果

{
  "_index": "province",
  "_type": "citys",
  "_id": "1",
  "_version": 7,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 6,
  "_primary_term": 1
}

curl命令列

建立索引

$ curl -XPUT "http://127.0.0.1:9200/school"
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "school"
}

刪除索引

curl -XDELETE http://localhost:9200/school
{"acknowledged":true}

插入資料

指定id

$ curl -H "Content-Type:application/json"  -XPUT http://localhost:9200/school/student/1 -d '{"name":"xiaoming","age":20}'
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

不指定id

curl -H "Content-Type:application/json"  -XPOST http://localhost:9200/school/student/ -d '{"name":"xiaowang","age":18}'
{
    "_index": "school",
    "_type": "student",
    "_id": "kYeii2UBT-VMLOAXEoWx",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

修改資料

curl -H "Content-Type:application/json"  -XPOST http://localhost:9200/school/student/1/_update -d '{"doc":{"name":"xiaohong","age":21}}'
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

查詢資料

curl -XGET http://localhost:9200/school/student/1
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 2,
    "found": true,
    "_source": {
        "name": "xiaohong",
        "age": 21
    }
}

刪除

刪除資料

curl -XDELETE http://localhost:9200/school/student/1
{
    "_index": "school",
    "_type": "student",
    "_id": "1",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

刪除Type

curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/_delete_by_query  -d '{"query":{"match_all":{}}}'
{
    "took": 40,
    "timed_out": false,
    "total": 1,
    "deleted": 1,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1,
    "throttled_until_millis": 0,
    "failures": []
}

bulk批量匯入

POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

匯入結果

{
  "took": 176,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "delete": {
        "_index": "test",
        "_type": "_doc",
        "_id": "2",
        "_version": 1,
        "result": "not_found",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 404
      }
    },
    {
      "create": {
        "_index": "test",
        "_type": "_doc",
        "_id": "3",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "update": {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}

reindex資料遷移

POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

批量匯入資料

POST /blog/paper/_bulk
{"index":{"_id":1}}
{"pID":"7ec0e0e5","uID":1,"publish":false,"date":"2018-01-02"}
{"index":{"_id":2}}
{"pID":"69590fe7","uID":2,"publish":true,"date":"2018-10-11"}
{"index":{"_id":3}}
{"pID":"aec30fa2","uID":1,"publish":false,"date":"2018-06-25"}
{"index":{"_id":4}}
{"pID":"a8733527","uID":3,"publish":false,"date":"2017-01-16"}

相關文章