我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
当前回答
如果你想提取成千上万的记录,那么……少数人给出了使用“scroll”的正确答案(注意:一些人还建议使用“search_type=scan”。这已被弃用,并在v5.0中被移除。你不需要它)
从一个“search”查询开始,但指定一个“scroll”参数(这里我使用了1分钟的超时):
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方便,接下来你应该运行以下命令:
curl -XGET 'localhost:9200/_search/scroll' -d'
{
"scroll" : "1m",
"scroll_id" : "c2Nhbjs2OzM0NDg1ODpzRlBLc0FXNlNyNm5JWUc1"
}
'
然而,传递scroll_id并不是设计为手动完成的。最好的办法是编写代码来实现它。例如,在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来提取数据。
其他回答
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
要从所有索引中返回所有记录,您可以执行:
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,
...
通过提供大小,elasticSearch将返回的最大结果是10000
curl -XGET 'localhost:9200/index/type/_search?scroll=1m' -d '
{
"size":10000,
"query" : {
"match_all" : {}
}
}'
在此之后,您必须使用Scroll API来获取结果并获得_scroll_id值,并将此值放入scroll_id中
curl -XGET 'localhost:9200/_search/scroll' -d'
{
"scroll" : "1m",
"scroll_id" : ""
}'
Elasticsearch 6.x
请求:GET /foo/_search?漂亮= true
Response:在Hits-> total中,给出文档的计数
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1001,
"max_score": 1,
"hits": [
{
您可以使用size=0,这将返回您所有的文档 例子
curl -XGET 'localhost:9200/index/type/_search' -d '
{
size:0,
"query" : {
"match_all" : {}
}
}'