我在Elasticsearch中有一個小型數據庫,並且爲了測試目的想要將所有記錄都拉回來。我試圖使用的形式的URL ...Elasticsearch查詢返回所有記錄
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人可以給我的URL你會用來完成這個,請?
我在Elasticsearch中有一個小型數據庫,並且爲了測試目的想要將所有記錄都拉回來。我試圖使用的形式的URL ...Elasticsearch查詢返回所有記錄
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人可以給我的URL你會用來完成這個,請?
我認爲Lucene的語法支撐:
http://localhost:9200/foo/_search?pretty=true&q=*:*
大小默認爲10,所以您可能還需要&size=BIGNUMBER
獲得10餘項。 (其中BIGNUMBER等於你認爲比你的數據集大的數字)
但是,elasticsearch文檔suggests對於大型結果集,使用掃描搜索類型。
EG:
curl -XGET 'localhost:9200/foo/_search?search_type=scan&scroll=10m&size=50' -d '
{
"query" : {
"match_all" : {}
}
}'
,然後繼續要求按照上面的文檔鏈接提示。
編輯:scan
在2.1.0中不推薦使用。
scan
與按照_doc
排序的常規scroll
請求相比不提供任何優惠。 link to elastic docs(由@克里斯托夫 - 魯西看準)
查詢下面將返回NO_OF_RESULTS你想返回..
curl -XGET 'localhost:9200/foo/_search?size=NO_OF_RESULTS' -d '
{
"query" : {
"match_all" : {}
}
}'
現在,這裏的問題是,你要所有的記錄被退回。所以很自然地,在寫一個查詢之前,你不知道的值NO_OF_RESULTS。
我們如何知道文檔中有多少條記錄?只需輸入下面
curl -XGET 'localhost:9200/foo/_search' -d '
該查詢會給你一個結果,看起來像一個下面
{
hits" : {
"total" : 2357,
"hits" : [
{
..................
結果總告訴你有多少條記錄在文檔中使用。所以,這是一個很好的辦法知道NO_OF的數值結果
curl -XGET 'localhost:9200/_search' -d '
搜索中的所有指標各類
curl -XGET 'localhost:9200/foo/_search' -d '
搜尋所有類型的富指數
curl -XGET 'localhost:9200/foo1,foo2/_search' -d '
搜索foo1和foo2指數中的所有類型
curl -XGET 'localhost:9200/f*/_search
搜索任何指數的所有類型以f。開頭
curl -XGET 'localhost:9200/_all/type1,type2/_search' -d '
搜索類型的用戶,並在所有指數鳴叫
默認情況下,ES將返回10個結果,除非基本查詢中包含大小參數。 – lfender6445 2014-04-07 03:11:39
以前的回答是三歲。將其更新爲當前版本。 – vjpan8564 2015-07-11 18:19:59
http://127.0.0.1:9200/foo/_search/?size=1000&pretty=1
^
注意大小參數,增加從默認顯示的點擊( 10)到每個碎片1000。
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html
雖然有一點需要記住(來自Elasticsearch文檔):請注意,從+大小不能超過index.max_result_window索引設置,默認值爲10,000。 – user3078523 2018-02-14 11:47:32
使用server:9200/_stats
也讓你所有的別名統計..喜歡的大小和每別名元素的數量,這是非常有用的,並提供有用的信息
但是,從我記得,ES只允許每個請求獲得16000個數據。所以如果數據超過16000,這個解決方案是不夠的。 – 2016-04-23 22:48:23
elasticsearch(ES)同時支持GET或從ES集羣索引獲取數據的POST請求。
當我們做一個GET:
http://localhost:9200/[your index name]/_search?size=[no of records you want]&q=*:*
當我們做一個POST:
http://localhost:9200/[your_index_name]/_search
{
"size": [your value] //default 10
"from": [your start index] //default 0
"query":
{
"match_all": {}
}
}
我會建議使用帶有elasticsearch http://mobz.github.io/elasticsearch-head/ 一個UI插件,這將有助於你得到一個更好的感覺您創建的指數,並測試您的指數。
Elasticsearch將獲得顯着較慢,如果你只是添加一個大號作爲大小,一種方法來使用獲取所有文件正在使用掃描和滾動ID。
所以,你的電話是:
GET /foo/_search?search_type=scan&scroll=1m
{
"query": { "match_all": {}},
"size": 1000
}
這將返回一個_scroll_id,你現在可以用它來得到第一批文件。
https://www.elastic.co/guide/en/elasticsearch/guide/current/scan-scroll.html
簡單!您可以使用size
和from
參數!
http://localhost:9200/[your index name]/_search?size=1000&from=0
然後您逐漸更改from
,直到您獲取所有數據。
如果數據中包含很多文檔,請不要使用此方法...每次進入「下一頁」彈性將變得越來越慢!使用SearchAfter代替 – Joshlo 2017-07-20 13:25:57
他們中的一些人給出了使用掃描和滾動的正確答案,顯然,我無法得到一個可以神奇地工作的完整答案。當有人想要拉記錄時,必須按照curl命令運行。
curl -XGET 'http://ip1:9200/myindex/_search?scroll=1m' -d '
{
"query": {
"match_all" : {}
}
}
'
但是我們沒有在這裏完成。上述curl命令的輸出會是這樣的
{"_scroll_id":"c2Nhbjs1OzUyNjE6NU4tU3BrWi1UWkNIWVNBZW43bXV3Zzs1Mzc3OkhUQ0g3VGllU2FhemJVNlM5d2t0alE7NTI2Mjo1Ti1TcGtaLVRaQ0hZU0FlbjdtdXdnOzUzNzg6SFRDSDdUaWVTYWF6YlU2Uzl3a3RqUTs1MjYzOjVOLVNwa1otVFpDSFlTQWVuN211d2c7MTt0b3RhbF9oaXRzOjIyNjAxMzU3Ow==","took":109,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":22601357,"max_score":0.0,"hits":[]}}
其重要的是擁有_scroll_id方便,因爲第二天你SHD運行以下命令
curl -XGET 'localhost:9200/_search/scroll' -d'
{
"scroll" : "1m",
"scroll_id" : "c2Nhbjs2OzM0NDg1ODpzRlBLc0FXNlNyNm5JWUc1"
}
'
不過,我不認爲它容易手動運行。你最好的選擇是編寫一個java代碼來做同樣的事情。
private TransportClient client = null;
private Settings settings = ImmutableSettings.settingsBuilder()
.put(CLUSTER_NAME,"cluster-test").build();
private SearchResponse scrollResp = null;
this.client = new TransportClient(settings);
this.client.addTransportAddress(new InetSocketTransportAddress("ip", port));
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
scrollResp = client.prepareSearch(index).setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
.setQuery(queryBuilder)
.setSize(100).execute().actionGet();
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(timeVal))
.execute()
.actionGet();
現在LOOP上一個命令使用SearchResponse來提取數據。
這是最好的解決方案,我發現使用Python客戶端
# Initialize the scroll
page = es.search(
index = 'yourIndex',
doc_type = 'yourType',
scroll = '2m',
search_type = 'scan',
size = 1000,
body = {
# Your query's body
})
sid = page['_scroll_id']
scroll_size = page['hits']['total']
# Start scrolling
while (scroll_size > 0):
print "Scrolling..."
page = es.scroll(scroll_id = sid, scroll = '2m')
# Update the scroll ID
sid = page['_scroll_id']
# Get the number of results that we returned in the last scroll
scroll_size = len(page['hits']['hits'])
print "scroll size: " + str(scroll_size)
# Do something with the obtained page
https://gist.github.com/drorata/146ce50807d16fd4a6aa
使用Java客戶端
import static org.elasticsearch.index.query.QueryBuilders.*;
QueryBuilder qb = termQuery("multi", "test");
SearchResponse scrollResp = client.prepareSearch(test)
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
//Scroll until no hits are returned
do {
for (SearchHit hit : scrollResp.getHits().getHits()) {
//Handle the hit...
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
} while(scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html
的最佳方式來調整大小使用size = numbe [R在URL
Curl -XGET "http://localhost:9200/logstash-*/_search?size=50&pretty"
注面前:最大值可以在這個尺寸可以定義爲10000一萬以上的任何值,預計您使用滾動功能這將最大限度地減少影響到性能的任何機會。
由於哪個版本的最大尺寸發生了? – WoodyDRN 2017-02-04 01:10:51
您可以使用尺寸= 0這將返回所有的文件 例如
curl -XGET 'localhost:9200/index/type/_search' -d '
{
size:0,
"query" : {
"match_all" : {}
}
}'
這將返回一個累積的信息,但不是點擊自己 – user732456 2017-03-30 13:17:18
對不起,我必須downvote誤導 – 2017-07-13 06:40:21
http://localhost:9200/foo/_search/? 大小 = 1000 &漂亮= 1
你需要指定大小的查詢參數的默認值是10
它的工作謝謝 – 2017-06-06 15:00:52
歡迎您@ hamzeh.hanandeh .....很高興這有助於別人 – 2017-06-06 16:20:04
您可以使用_count
API來獲取價值,爲size
參數:
http://localhost:9200/foo/_count?q=<your query>
返回{count:X, ...}
。提取值 'X',然後執行實際查詢:
http://localhost:9200/foo/_search?q=<your query>&size=X
大小 PARAM增加從默認顯示的點擊數(10)500
http://localhost:9200/[indexName]/_search?pretty=true&size=500&q=*:*
更改從步逐步獲取所有數據。
http://localhost:9200/[indexName]/_search?size=500&from=0
要從你可以做所有的索引返回所有記錄:
curl -XGET http://35.195.120.21:9200/_all/_search?size=50&pretty
輸出:
"took" : 866,
"timed_out" : false,
"_shards" : {
"total" : 25,
"successful" : 25,
"failed" : 0
},
"hits" : {
"total" : 512034694,
"max_score" : 1.0,
"hits" : [ {
"_index" : "grafana-dash",
"_type" : "dashboard",
"_id" : "test",
"_score" : 1.0,
...
感謝。這是最後我想出了,返回我現在需要的... http:// localhost:9200/foo/_search?size = 50&pretty = true&q = *:* – 2012-01-12 09:41:09
添加到@ Steve的答案,你可以找到一個elasticsearch在此鏈接中可以理解的參數列表http://www.elasticsearch.org/guide/reference/api/search/uri-request/ – 2013-07-31 08:36:51
是否有可能使用match_all查詢以外的查詢運行掃描搜索? – Churro 2013-09-10 20:30:23