Elasticsearch的CRUD操作

1、添加信息	

PUT http://192.168.56.201:9200/demo/employee/1?op_type=create
{
	"first_name" : "John",
	"last_name" :  "Smith",
	"age" :        25,
	"about" :      "I love to go rock climbing",
	"interests": [ "sports", "music" ]
}
PUT http://192.168.56.201:9200/demo/employee/1/_create
{
	"first_name" : "John",
	"last_name" :  "Smith",
	"age" :        25,
	"about" :      "I love to go rock climbing",
	"interests": [ "sports", "music" ]
}

2、删除信息

DELETE  http://192.168.56.201:9200/megacorp/employee/1

3、搜索信息

3.1、搜索所有
GET http://192.168.56.201:9200/megacorp/employee/_search
3.2、搜索指定ID
GET http://192.168.56.201:9200/megacorp/employee/1
3.3、按照关键词搜索
GET http://192.168.56.201:9200/megacorp/employee/_search?q=last_name:Smit
其中last_name是要搜索的字段,Smith是该字段的值
3.4 使用Query  DSL查询
GET http://192.168.56.201:9200/megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

4、更新或添加信息

PUT http://192.168.56.201:9200/demo/employee/1
{
	"first_name" : "John",
	"last_name" :  "Smith",
	"age" :        25,
	"about" :      "I love to go rock climbing",
	"interests": [ "sports", "music" ]
}
如果不存在则新建

5、乐观并发控制
在更新或者删除时可以使用_version参数来控制

6、更新部分文档部分内容
POST http://192.168.56.201:9200/demo/employee/1/_update
{
	"first_name" : "John",
	"tags":["test"]
}

7、冲突重试
POST http://192.168.56.201:9200/demo/employee/1/_update?retry_on_conflict=5
使用场景:更新文章点击次数

8、检索多个文档
GET /_mget
{
   "docs" : [
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "pageviews",
         "_id" :    1,
         "_source": "views"
      }
   ]
}
相同index和type
GET /website/blog/_mget
{
   "ids" : [ "2", "1" ]
}

9、批量操作
POST _bulk?pretty
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }} 
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title":    "My first blog post" }
{ "index":  { "_index": "website", "_type": "blog" }}
{ "title":    "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }

备注:每行使用换行符分开,bulk操作不是原子性的;批量要注意每次提交的文档大小


Tagged , , . Bookmark the permalink.

Comments are closed.