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
         }
      }
   ]
}


你可能感兴趣的:(elasticsearch,操作,curd)