ElasticSearch(三)检索多个文档

检索多个文档之Multi-get(mget) API

如果我们需要检索多个文档,那么我们不需要单独一次一次的请求ES集群,这样对网络开销比较大,我们可以利用Multi-get API实现一个请求检索多个文档。
Multi-get API的参数时候docs数组,数组的每个节点定义了 _index、_type、_id,如果你需要检索一个或者多个字段,也可以在节点中定义_source参数,里边是需要检索的字段。

请求

GET /_mget 
{   
	"docs" : [      
		{         
			"_index" : "website",        
			"_type" :  "blog",        
			"_id" :    2      
	  	},     
	   	{        
			"_index" : "website",       
			"_type" :  "pageviews",       
			"_id" :    1,         
			"_source": "views"     
         }  
     ]
 }

响应

响应回来的数据是一个docs数组,这个数组里就是包含了请求回来的数据。如果其中一个_index没有查询到数据,并不影响其他整体的查询,只不过没有查询到数据的返回来的结果中found字段值为false

{   
	"docs" : [     
		 {        
			"_index" :   "website",        
			"_id" :      "2",         
			"_type" :    "blog",         
			"found" :    true,        
		    "_source" : {            
		    	"text" :  "This is a piece of cake...",           
		    	"title" : "My first external blog entry"         
	    	},         
		    	"_version" : 10      
   		},     
   	 	{        
			"_index" :   "website",       
			"_id" :      "1",        
			"_type" :    "pageviews",         
			"found" :    true,         
			"_version" : 2,         
			"_source" : {            
				"views" : 2         
			}     
		}  
	]
}

检索多个文档之Bulk API

Bulk API类似于Multi-Get API,一个请求可以检索多个文档,但是Bulk API相对于Multi-Get API更加强大,Bulk API可以使用单一的请求对多个文档进行Create,Update,Delete,Index操作。

发布了94 篇原创文章 · 获赞 55 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Suubyy/article/details/86095363