一、基本介紹
_msearch就是multi search API使用的末端,它可以在一個相同的api中去執行多個查詢請求。
請求的格式類似于大部API的格式,它的請求格式如下:
header\n
body\n
header\n
body\n
header可以包含要查詢的索引(可以是多個索引),可選的映射類型,還有search_type,preference和routing。
body可以包含指定的搜索請求(包括:query、aggregations、from、size等等)。
例子見官網:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html#search-multi-search
$ cat requests{}{"query":{"match_all":{}},"from":0,"size":10}{}{"query":{"match_all":{}}}{"index":"test2"}{"query":{"match_all":{}}}$ curl-XGET localhost:9200/test/_msearch--data-binary@requests;echo
例子里有一個requests的文件,用curl的二進制流把它的內容傳輸過去。
下面以實際例子來介紹傳文件和json數據:
在當前目錄下建立文件requests,vim requests,寫入:
保存退出。
curl -XPOST 'http://xxx.xxx.xxx:9200/_msearch?pretty' ?--data-binary @requests
會出現數據,說明成功了:
下面是傳輸json參數的例子:
以curl -XPOST 'http://xxx.xxx.xxx:9200/_msearch?pretty' -H 'Content-type:application/json' --data-binary 'json參數' 的格式請求:
同樣會得到結果,注意json的格式,一定要有換行,建議在文本編輯器里寫好再copy進來。
二、_msearch在ruby on rails里的使用
這里主要依賴兩個gem包:elasticsearch-ruby和elasticsearch-rails。
這兩個包的基本使用可以去github上學習,這里主要看一下msearch的使用,在github里搜索msearch:
看一下前兩篇,一個是測試、一個是使用說明。使用說明如下:
它在這里說的很明白了,它把我們之前的傳入的參數以數組的形式傳給:body參數,注意這里的body是指名參數。