欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

三、elasticsearch增删改查

程序员文章站 2022-07-05 08:05:18
...

一、新建索引

put http://{{host}}:9200/liarbry

二、增加document

  1. 指定id

put http://{{host}}:9200/liarbry/books/1
{
	"title":"spring boot program idea3",
	"user":{
		"first":"hongwei",
		"last":"zhang"
	},
	"price":300
}
  1. 不指定id
post  http://{{host}}:9200/liarbry/books
{
	"title":"spring boot program idea",
	"user":{
		"first":"hongwei",
		"last":"zhang"
	},
	"price":200
}

三、更新document

  1. 覆盖更新
    这个时候body中有什么字段,最终就会有什么字段
put http://{{host}}:9200/liarbry/books/1
{
	"title":"spring boot program idea4",
	"user":{
		"first":"hongwei",
		"last":"zhang"
	},
	"price":300
}
  1. 局部更新
    doc中可以是已经存在的字段,这个时候就会更新已经存在的字段
    如果是不存在的字段,就是添加字段操作
post http://{{host}}:9200/liarbry/books/1/_update
{
	"doc":{
		"date":"2019-06-28 00:01:01"
	}
}

四、删除document

  1. 删除document
delete http://{{host}}:9200/liarbry/books/1
  1. 删除type
delete http://{{host}}:9200/liarbry/books
  1. 删除index
delete http://{{host}}:9200/liarbry

五、查询document

查询是最复杂的,下面只是列出最简单的

  1. 根据id来查询
get http://{{host}}:9200/liarbry/books/1
  1. 批量获取

#batch query

GET /_mget
{
  "docs":[
    {
      "_index":"liarbry",
      "_type":"books",
      "_id":"1",
      "_source":["title","price"]
    }, 
    {
      "_index":"liarbry",
      "_type":"books",
      "_id":"AWujzBfklrzOujYjNVt4"
    }
  ]
}

#specified index/type

GET /liarbry/books/_mget
{
  "docs":[
    {"_id":"1"},
    {"_id":"AWujzBfklrzOujYjNVt4"}
  ]
}

#above shorthand

GET /liarbry/books/_mget
{
  "ids":["1","AWujzBfklrzOujYjNVt4"]
}

五、批量增删改

  1. bulk operate scheme

{action:{metadata}}\n
{request body}\n
{action:{metadata}}\n
{request body}\n

  1. action description
action description
create if not exists,create it
index if not exists then create it. if exists then cover it
delete delete document
update partial update
  1. example
POST /liarbry/books/_bulk
{"index":{"_id":"2"}}
{"title":"books2","price":10}
{"index":{"_id":"3"}}
{"title":"books3","price":30}