我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
当前回答
这是我使用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=0,这将返回您所有的文档 例子
curl -XGET 'localhost:9200/index/type/_search' -d '
{
size:0,
"query" : {
"match_all" : {}
}
}'
如果仍然有人像我一样寻找从Elasticsearch中检索的所有数据,下面是我所做的。此外,所有的数据意味着,所有的索引和所有的文档类型。我使用的是Elasticsearch 6.3
curl -X GET "localhost:9200/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
Elasticsearch参考
http://127.0.0.1:9200/foo/_search/?size=1000&pretty=1
^
请注意size参数,它将每个分片显示的命中数从默认值(10)增加到1000。
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html
elasticsearch(ES)既支持GET请求,也支持POST请求,以便从ES集群索引中获取数据。
当我们执行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插件 这将帮助您更好地了解您创建的索引,并测试您的索引。
来自Kibana DevTools的:
GET my_index_name/_search
{
"query": {
"match_all": {}
}
}