[toc]


一. 数据筹备

当初有上面几个index:books,my_index, users

1.books索引内容:

_index_type_id▲_scorebookIdbookNameauthor
books_doc111Thinking in JavaBruce Eckel
books_doc212中国通史吕思勉
books_doc313中国通史吕思勉
books_docNCvOY4ABRxSL2QPxNNHf14中国通史4吕思勉
books_docNSvOY4ABRxSL2QPxPdEy14中国通史4吕思勉
books_docNivOY4ABRxSL2QPxQdEo14中国通史4吕思勉
books_docNyvOY4ABRxSL2QPxRNHC14中国通史4吕思勉
books_docOCvOY4ABRxSL2QPxSdHA14中国通史4吕思勉
books_docOSvOY4ABRxSL2QPxTNHb14中国通史4吕思勉
books_docOivOY4ABRxSL2QPxYNGs14鲁迅散文鲁迅

2. my_index索引内容:

_index_type_id_scorefirstNamelastName
my_index_doc11niewj
my_index_doc21

3.users索引内容

_index_type_id_scorenameagegenderbirth
users_doc11niewj36male1985-01-01
users_doc21nie26female1995-02-02
users_doc31lifubo33male
users_doc41weibinbin32male

二、bulk、mget、msearch的API测试demo

1. bulk执行多个索引的index/create/update/delete

1.1 测试执行需要

  1. users中的gender改为"male";
  2. my_index中减少"张大千"应用index;"李太白,杜甫"的两条记录应用create
  3. books中删除bookId=4的记录

1.2 执行脚本

POST _bulk{"delete":{"_index":"books","_id":"NSvOY4ABRxSL2QPxPdEy"}}{"delete":{"_index":"books","_id":"NivOY4ABRxSL2QPxQdEo"}}{"delete":{"_index":"books","_id":"NyvOY4ABRxSL2QPxRNHC"}}{"delete":{"_index":"books","_id":"OCvOY4ABRxSL2QPxSdHA"}}{"delete":{"_index":"books","_id":"OSvOY4ABRxSL2QPxTNHb"}}{"delete":{"_index":"books","_id":"OivOY4ABRxSL2QPxYNGs"}}{"delete":{"_index":"books","_id":"NCvOY4ABRxSL2QPxNNHf"}}{"index":{"_index":"my_index","_id":"3"}}{"firstName":"张","lastName":"大千"}{"create":{"_index":"my_index","_id":"4"}}{"firstName":"李","lastName":"太白"}{"create":{"_index":"my_index","_id":"5"}}{"firstName":"杜","lastName":"甫"}{"update":{"_index":"users","_id":"2"}}{"doc":{"gender":"male"}}

1.3 留神delete不能间接指定:bookId:4

1.4 执行后果:

{  "took" : 68,  "errors" : false,  "items" : [    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "NSvOY4ABRxSL2QPxPdEy",        "_version" : 2,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 12,        "_primary_term" : 1,        "status" : 200      }    },    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "NivOY4ABRxSL2QPxQdEo",        "_version" : 2,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 13,        "_primary_term" : 1,        "status" : 200      }    },    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "NyvOY4ABRxSL2QPxRNHC",        "_version" : 2,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 14,        "_primary_term" : 1,        "status" : 200      }    },    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "OCvOY4ABRxSL2QPxSdHA",        "_version" : 2,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 15,        "_primary_term" : 1,        "status" : 200      }    },    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "OSvOY4ABRxSL2QPxTNHb",        "_version" : 2,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 16,        "_primary_term" : 1,        "status" : 200      }    },    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "OivOY4ABRxSL2QPxYNGs",        "_version" : 2,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 17,        "_primary_term" : 1,        "status" : 200      }    },    {      "delete" : {        "_index" : "books",        "_type" : "_doc",        "_id" : "NCvOY4ABRxSL2QPxNNHf",        "_version" : 3,        "result" : "deleted",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 18,        "_primary_term" : 1,        "status" : 200      }    },    {      "index" : {        "_index" : "my_index",        "_type" : "_doc",        "_id" : "3",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 3,        "_primary_term" : 1,        "status" : 201      }    },    {      "create" : {        "_index" : "my_index",        "_type" : "_doc",        "_id" : "4",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 4,        "_primary_term" : 1,        "status" : 201      }    },    {      "create" : {        "_index" : "my_index",        "_type" : "_doc",        "_id" : "5",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 5,        "_primary_term" : 1,        "status" : 201      }    },    {      "update" : {        "_index" : "users",        "_type" : "_doc",        "_id" : "2",        "_version" : 10,        "result" : "updated",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 15,        "_primary_term" : 4,        "status" : 200      }    }  ]}

1.5 执行完后果: users

_index_type_id_scorefirstNamelastName
my_index_doc11niewj
my_index_doc21
my_index_doc31大千
my_index_doc41太白
my_index_doc51

1.6 执行完后果: my_index

_index_type_id_scorenameagegenderbirth
users_doc11niewj36male1985-01-01
users_doc31lifubo33male
users_doc41weibinbin32male
users_doc21nie26male1995-02-02

1.7 执行完后果: books

_index_type_id▲_scorebookIdbookNameauthor
books_doc111Thinking in JavaBruce Eckel
books_doc212中国通史吕思勉
books_doc313中国通史吕思勉

2. mget:get多个索引中的文档

2.1 mget的脚本

GET _mget{"docs":[  {"_index":"users","_id":"1"},  {"_index":"books","_id":"1"},  {"_index":"my_index","_id":"1"} ]}

2.2 mget后果:

{  "docs" : [    {      "_index" : "users",      "_type" : "_doc",      "_id" : "1",      "_version" : 1,      "_seq_no" : 12,      "_primary_term" : 4,      "found" : true,      "_source" : {        "name" : "niewj",        "age" : 36,        "gender" : "male",        "birth" : "1985-01-01"      }    },    {      "_index" : "books",      "_type" : "_doc",      "_id" : "1",      "_version" : 2,      "_seq_no" : 1,      "_primary_term" : 1,      "found" : true,      "_source" : {        "bookId" : "1",        "bookName" : "Thinking in Java",        "author" : "Bruce Eckel"      }    },    {      "_index" : "my_index",      "_type" : "_doc",      "_id" : "1",      "_version" : 1,      "_seq_no" : 0,      "_primary_term" : 1,      "found" : true,      "_source" : {        "firstName" : "nie",        "lastName" : "wj"      }    }  ]}

3.msearch批量查问多个索引

3.1 查问多个索引的多个条件(缩小IO次数)

POST _msearch{"index":"movies"}{"query":{"match_all":{}},"from":0,"size":3}{"index":"users"}{"query":{"match_phrase":{"name.keyword":"nie"}}}{"index":"books"}{"query":{"match":{"bookName":"中国通史"}}}

3.2 查问后果

{  "took" : 0,  "responses" : [    {      "took" : 0,      "timed_out" : false,      "_shards" : {        "total" : 1,        "successful" : 1,        "skipped" : 0,        "failed" : 0      },      "hits" : {        "total" : {          "value" : 9743,          "relation" : "eq"        },        "max_score" : 1.0,        "hits" : [          {            "_index" : "movies",            "_type" : "_doc",            "_id" : "982",            "_score" : 1.0,            "_source" : {              "id" : "982",              "genre" : [                "Drama"              ],              "title" : "Picnic",              "@version" : "1",              "year" : 1955            }          },          {            "_index" : "movies",            "_type" : "_doc",            "_id" : "984",            "_score" : 1.0,            "_source" : {              "id" : "984",              "genre" : [                "Comedy",                "Drama"              ],              "title" : "Pompatus of Love, The",              "@version" : "1",              "year" : 1996            }          },          {            "_index" : "movies",            "_type" : "_doc",            "_id" : "986",            "_score" : 1.0,            "_source" : {              "id" : "986",              "genre" : [                "Adventure",                "Children"              ],              "title" : "Fly Away Home",              "@version" : "1",              "year" : 1996            }          }        ]      },      "status" : 200    },    {      "took" : 0,      "timed_out" : false,      "_shards" : {        "total" : 1,        "successful" : 1,        "skipped" : 0,        "failed" : 0      },      "hits" : {        "total" : {          "value" : 1,          "relation" : "eq"        },        "max_score" : 0.87546873,        "hits" : [          {            "_index" : "users",            "_type" : "_doc",            "_id" : "2",            "_score" : 0.87546873,            "_source" : {              "name" : "nie",              "age" : 26,              "gender" : "male",              "birth" : "1995-02-02"            }          }        ]      },      "status" : 200    },    {      "took" : 0,      "timed_out" : false,      "_shards" : {        "total" : 1,        "successful" : 1,        "skipped" : 0,        "failed" : 0      },      "hits" : {        "total" : {          "value" : 2,          "relation" : "eq"        },        "max_score" : 1.8126037,        "hits" : [          {            "_index" : "books",            "_type" : "_doc",            "_id" : "2",            "_score" : 1.8126037,            "_source" : {              "bookId" : "2",              "bookName" : "中国通史",              "author" : "吕思勉"            }          },          {            "_index" : "books",            "_type" : "_doc",            "_id" : "3",            "_score" : 1.8126037,            "_source" : {              "bookId" : "3",              "bookName" : "中国通史",              "author" : "吕思勉"            }          }        ]      },      "status" : 200    }  ]}
每个索引的查问都返回了本人的后果