elasticsearch學習筆記(十六)——Elasticsearch mget批量查詢api實戰

首先說明一下爲何須要批量查詢操做?
假設一下好比說咱們沒有批量查詢的操做,那麼當咱們獲取數據的時候,就是一條一條的查詢。設想一下咱們要獲取100條數據,那麼就要發送100次請求,這個開銷時很大的。可是有了批量查詢的話,查詢100條數據,就只須要發送一次網絡請求就能夠了,網絡請求的性能開銷縮減100倍。網絡

下面是實戰部分,演示一下一條一條查詢與批量查詢:
(1)一條一條查詢性能

GET /test_index/_doc/1
{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 8,
  "_seq_no" : 7,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_field" : "test test",
    "name" : "test1"
  }
}
GET /test_index/_doc/2
{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 4,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_field" : "test client 1",
    "name" : "test1"
  }
}

(2)mget批量查詢code

GET /_mget
{
  "docs": [
    {
      "_index": "test_index",
      "_id": 1
    },
    {
      "_index": "test_index",
      "_id": 2
    }
  ]
}
{
  "docs" : [
    {
      "_index" : "test_index",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 8,
      "_seq_no" : 7,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "test_field" : "test test",
        "name" : "test1"
      }
    },
    {
      "_index" : "test_index",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 4,
      "_seq_no" : 3,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "test_field" : "test client 1",
        "name" : "test1"
      }
    }
  ]
}
相關文章
相關標籤/搜索