原创

elasticsearch索引的初始化操作以及marvel操作(增删改查),批量查询_mget,批量操作_bulk

文中的简洁版都是使用marvel操作的

安装marvel插件的教程

https://blog.csdn.net/u013294097/article/details/100144725

1.创建索引之前可以对索引做初始化操作,

比如指定shards数量以及replicas的数量

正确的语法格式:

CURL -XPUT 'http://ip:port/library/' -d '{ "settings":{ "index":{ "number_of_shards":5, "number_of_replicas":1 } } }'

使用marvel工具的语法


#---------------------------------
#初始化初始化索引
#创建索引之前可以对索引做初始化操作,比如指定shards数量以及replicas的数量

PUT http://localhost:9200/lilbrary/ 
{
  "settings":{
    "index":{
      "number_of_shards":5,
      "number_of_replicas":1
        }
    }
}

上面的number_of_replicas可以换成其他的

blocks_read_only  设为true,则当前索引只允许读,不允许写或者更新

blocks_read 设为true,则禁止读操作

blocks_write 设为true,则禁止写操作

blocks_metadata 设为true,则禁止对metadata操作



2.可以通过GET 获取索引的详细配置信息

GET /lilbrary/_settings

如果是两个索引

GET /library1,library2/_settings

获取所有的索引信息

GET /_all/_settings

以上的完整的语句:

CURL -XGET 'http://ip:port/library/_settings' CURL -XGET 'http://ip:port/library,library2/_settings' CURL -XGET 'http://ip:port/_all/_settings'

3.API创建,更新,删除索引

3.1如何用API创建索引

例:curl -XPUT 'http://ip:port/twitter/tweet/1' -d '{"user":"kimchv","post_date":"2019-08-29T14:12:12","message":"trying out Elasticsearch"}'

使用marvel的案例:

带上ID的新增

PUT lilbrary/books/1
{
  "title":"Elasticsearch: The Definitive Guide",
  "name":{
    "first":"Zachary",
    "last":"Tong"
  },
  "publicsh_date":"2019-08-29",
  "price":"49.99"
}

不带ID的POST(可以更新,可以新增,更新的时候把id带上)

POST /library/books/
{
  "title":"Elasticsearch : blueprints",
  "name":{
    "first":"Vineeth",
    "last":"Monan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}


3.2如何用API更新索引

例如:curl -XPOST 'http://ip:port/test/type1/1/_update' -d '{"script":"ctx._source.conter+=count","params":{"count":4}}'

案例同上面的POST的案例,以上的方法不推荐

案例2:

通过 _update API的方式单独更新想要更新的
POST /library/books/1/_update
{
  "doc":{
    "price":"59.9"
    
  }
}


3.3如何用API删除索引

例:curl -XDELETE 'http://ip:port/twitter/tweet/1'

marvel案例:

DELETE /library/books/1

可以删除一个TYPE

DELETE /library/books

删除整个索引

DELETE /library


3.4如何查询索引

marvel方式:

GET /library/books/1

通过_source获取指定的字段,多字段或者所有的信息

GET /library/books/1?_source=title

GET /library/books/1?_source=title,price

GET /library/books/1?_source

4.批量查询

4.1普通的查询:

curl 'localhost:9200/_mget' -d{ "docs":[ { "_index":"library", "_type":"books", "_id":1, "_source":["keyword","keyword2"] }, { "_index":"library", "_type":"books", "_id":8, "_source":"keyword" } , { "_index":"library", "_type":"books", "_id":10, "_source":"keyword" } ] }

marvel的查询:

GET /_mget
{
  "docs":[
    {
      "_index":"library",
      "_type":"books",
      "_id":1,
      "_source":["keyword","keyword2"]
    },
    {
      "_index":"library",
      "_type":"books",
      "_id":8,
      "_source":"keyword"
    }
    ,
    {
      "_index":"library",
      "_type":"books",
      "_id":10,
      "_source":"keyword"
    }
    ]
}

4.2 批量查询_mget 获取相同index相同type下不同ID的文档

普通方式:

方式1: curl 'localhost:9200/library/books/_mget' -d { "docs":[ {"_id":6}, {"_id":28} ] } 方式2: curl 'localhost:9200/library/books/_mget' -d { "ids":["6","28"] }

marvel方式:

方式1:
GET /library/books/_mget
{
    "docs":[
        {"_id":6},
        {"_id":28}
    ]
}

方式2:
GET /library/books/_mget
{
    "ids":["6","28"]
}

5.批量操作_bulk

为了实现多个文档的create,index,update,delete而使用的

bulk的请求体格式(不能美化展示):

action有四种:create,index,update,delete

create:当文档不存在时创建之。

index:创建新文档或替换已有文档。

update:局部更新文档。

delete:删除一个文档

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

例如:{"delete":{"_index":"library","_type":"books","_id":"1"}}

示例1(marvel方式):

POST /library/books/_bulk
{"index":{"_id":1}}
{"title":"Elasticsearch: The Definitive Guide","price":5}
{"index":{"_id":2}}
{"title":"The Elasticsearch cookbook","price":15}
{"index":{"_id":3}}
{"title":"Elasticsearch Blueprints","price":9}
{"index":{"_id":4}}
{"title":"Think in Python","price":22}
{"index":{"_id":5}}
{"title":"Thinking in Java","price":7}

示例2(marvel方式)

接下来是使用update,delete操作示例

 POST /library/books/_bulk
{"delete":{"_index":"library","_type":"books","_id":"1"}}
{"create":{"_index":"music","_type":"classical","_id":"1"}}
{"title":"Ave Verum Corpus"}
{"index":{"_index":"music","_type":"classical"}}
{"title":"Litaniac de Venerabili Altaris Sacromento"}
{"update":{"_index":"library","_type":"books","_id":"2"}}
{"doc":{"price":"18"}}

5.2 bulk处理文档大小的最佳值

去从实际的环境中去测试,硬件不同,性能不同

正文到此结束