实践002-elasticsearch的批量操作API-bulk、mget、msearch

[toc]


一. 数据准备

现在有下面几个index:books,my_index, users

1.books索引内容:

_index _type _id ▲_score bookId bookName author
books _doc 1 1 1 Thinking in Java Bruce Eckel
books _doc 2 1 2 中国通史 吕思勉
books _doc 3 1 3 中国通史 吕思勉
books _doc NCvOY4ABRxSL2QPxNNHf 1 4 中国通史4 吕思勉
books _doc NSvOY4ABRxSL2QPxPdEy 1 4 中国通史4 吕思勉
books _doc NivOY4ABRxSL2QPxQdEo 1 4 中国通史4 吕思勉
books _doc NyvOY4ABRxSL2QPxRNHC 1 4 中国通史4 吕思勉
books _doc OCvOY4ABRxSL2QPxSdHA 1 4 中国通史4 吕思勉
books _doc OSvOY4ABRxSL2QPxTNHb 1 4 中国通史4 吕思勉
books _doc OivOY4ABRxSL2QPxYNGs 1 4 鲁迅散文 鲁迅

2. my_index索引内容:

_index _type _id _score firstName lastName
my_index _doc 1 1 nie wj
my_index _doc 2 1

3.users索引内容

_index _type _id _score name age gender birth
users _doc 1 1 niewj 36 male 1985-01-01
users _doc 2 1 nie 26 female 1995-02-02
users _doc 3 1 lifubo 33 male
users _doc 4 1 weibinbin 32 male

二、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 _score firstName lastName
my_index _doc 1 1 nie wj
my_index _doc 2 1
my_index _doc 3 1 大千
my_index _doc 4 1 太白
my_index _doc 5 1

1.6 执行完结果: my_index

_index _type _id _score name age gender birth
users _doc 1 1 niewj 36 male 1985-01-01
users _doc 3 1 lifubo 33 male
users _doc 4 1 weibinbin 32 male
users _doc 2 1 nie 26 male 1995-02-02

1.7 执行完结果: books

_index _type _id ▲_score bookId bookName author
books _doc 1 1 1 Thinking in Java Bruce Eckel
books _doc 2 1 2 中国通史 吕思勉
books _doc 3 1 3 中国通史 吕思勉

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
    }
  ]
}
每个索引的查询都返回了自己的结果

你可能感兴趣的:(elasticsearch)