Elasticsearch的CURD操作

Elasticsearch的索引初始化操作,操作地址爲http://192.168.3.71:9200/_plugin/marvel/sense/index.html

#命令如下
PUT http://192.168.3.71:9200/library/        #library爲索引設置的名稱
{
  "settings":{
    "number_of_shards":5,                    #設置默認shard數,設定後就不能更改
    "number_of_replicas":1
  }
}

#執行後結果如下
{
   "acknowledged": true
}

獲取索引library的信息

#命令如下
GET /library/_settings

#執行結果
{
   "library": {
      "settings": {
         "index": {
            "creation_date": "1458268337375",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "version": {
               "created": "1070299"
            },
            "uuid": "QuVaw6znQVCbLTBeHikoqQ"
         }
      }
   }
}

#在linux命令行也可以獲取結果

[root@elk-search1 ~]# curl -X GET 'http://192.168.3.71:9200/library/_settings'
{
    "library":{
        "settings":{
            "index":{
                "creation_date":"1458268337375",
                "number_of_shards":"5",
                "number_of_replicas":"1",
                "version":{
                    "created":"1070299"
                 },
                "uuid":"QuVaw6znQVCbLTBeHikoqQ"
            }
        }
    }
}

創建索引

#命令如下
PUT /library/type/1
{
  "title":"Elasticsearch: The Definitive Guide",
  "name":{
    "first":"zhangsan",
    "last":"wang"
  },
  "publish_date":"2015-02-08",
  "price":"92"
}

#執行結果
{
   "_index": "library",
   "_type": "type",
   "_id": "1",
   "_version": 1,
   "created": true
}

獲取創建的文檔信息

#命令如下
GET /library/type/1

#執行結果
{
   "_index": "library",
   "_type": "type",
   "_id": "1",
   "_version": 1,
   "found": true,
   "_source": {
      "title": "Elasticsearch: The Definitive Guide",
      "name": {
         "first": "zhangsan",
         "last": "wang"
      },
      "publish_date": "2015-02-08",
      "price": "92"
   }
}

批量插入操作

#命令如下
POST /library/books/_bulk
{"index":{"_id":6}}
{"title":"Elasticsearch: The Definitive Guide","price":5}
{"index":{"_id":7}}
{"title":"Elasticsearch cookbook","price":15}
{"index":{"_id":8}}
{"title":"Elasticsearch Blueprints","price":9}
{"index":{"_id":9}}
{"title":"Thinking in python","price":22}
{"index":{"_id":10}}
{"title":"Thinking in java","price":7}

#執行結果
{
   "took": 42,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "library",
            "_type": "books",
            "_id": "6",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "library",
            "_type": "books",
            "_id": "7",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "library",
            "_type": "books",
            "_id": "8",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "library",
            "_type": "books",
            "_id": "9",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "library",
            "_type": "books",
            "_id": "10",
            "_version": 1,
            "status": 201
         }
      }
   ]
}


#批量獲取結果
GET /library/books/_mget
{
  "ids":["1","2","3","4","5"]
}

#結果如下
{
   "docs": [
      {
         "_index": "library",
         "_type": "books",
         "_id": "1",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Elasticsearch: The Definitive Guide",
            "price": 5
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "2",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Elasticsearch cookbook",
            "price": 15
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "3",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Elasticsearch Blueprints",
            "price": 9
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "4",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Thinking in python",
            "price": 22
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "5",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Thinking in java",
            "price": 7
         }
      }
   ]
}

批量操作例子

#命令如下
POST /library/books/_bulk
{"delete":{"_index":"library","_type":"books","_id":1}}
{"create":{"_index":"music","_type":"classical","_id":1}}
{"title":"Are Verum Corpus"}
{"index":{"_index":"musci","_type":"classical"}}
{"title":"Litaniac de venerabili altaris sacromento"}
{"update":{"_index":"library","_type":"books","_id":2}}
{"doc":{"price":"18"}}

#執行結果如下
{
   "took": 819,
   "errors": false,
   "items": [
      {
         "delete": {
            "_index": "library",
            "_type": "books",
            "_id": "1",
            "_version": 2,
            "status": 200,
            "found": true
         }
      },
      {
         "create": {
            "_index": "music",
            "_type": "classical",
            "_id": "1",
            "_version": 1,
            "status": 201
         }
      },
      {
         "create": {
            "_index": "musci",
            "_type": "classical",
            "_id": "AVOIKhQEMOT1gJfW5tJU",
            "_version": 1,
            "status": 201
         }
      },
      {
         "update": {
            "_index": "library",
            "_type": "books",
            "_id": "2",
            "_version": 2,
            "status": 200
         }
      }
   ]
}

#查詢結果如下
GET /library/books/_mget
{
  "ids":["1","2","3","4","5"]
}

{
   "docs": [
      {
         "_index": "library",
         "_type": "books",
         "_id": "1",
         "found": false
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "2",
         "_version": 2,
         "found": true,
         "_source": {
            "title": "Elasticsearch cookbook",
            "price": "18"
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "3",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Elasticsearch Blueprints",
            "price": 9
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "4",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Thinking in python",
            "price": 22
         }
      },
      {
         "_index": "library",
         "_type": "books",
         "_id": "5",
         "_version": 1,
         "found": true,
         "_source": {
            "title": "Thinking in java",
            "price": 7
         }
      }
   ]
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章