http://localhost:9200/[your index name]/_search?size=1000&from=0
然后逐渐更改 from ,直到获得所有数据 .
3
这是我使用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
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.
from elasticsearch.helpers import scan
doc_generator = scan(
es_obj,
query={"query": {"match_all": {}}},
index="my-index",
)
# use the generator to iterate, dont try to make a list or you will get out of RAM
for doc in doc_generator:
# use it somehow
23 回答
elasticsearch(ES)支持从ES集群索引获取数据的GET或POST请求 .
当我们做GET时:
当我们做POST时:
我建议使用带有elasticsearch的UI插件http://mobz.github.io/elasticsearch-head/这将帮助您更好地了解您创建的索引并测试索引 .
下面的查询将返回您想要返回的NO_OF_RESULTS .
现在,这里的问题是你想要 all 返回记录 . 很自然地,在编写查询之前,您不会知道 NO_OF_RESULTS 的值 .
我们如何知道您的文档中存在多少条记录?只需在下面输入查询即可
这会给你一个看起来像下面的结果
结果 total 告诉您文档中有多少记录可用 . 所以,这是一个很好的方式来了解 NO_OF RESULTS 的 Value
搜索所有索引中的所有类型
搜索foo索引中的所有类型
搜索foo1和foo2索引中的所有类型
搜索以f开头的任何索引中的所有类型
所有索引中的搜索类型用户和推文
简单!你可以使用
size
和from
参数!然后逐渐更改
from
,直到获得所有数据 .这是我使用python客户端找到的最佳解决方案
https://gist.github.com/drorata/146ce50807d16fd4a6aa
使用java客户端
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html
要返回所有索引的所有记录,您可以执行以下操作
curl -XGET http://35.195.120.21:9200/_all/_search?size=50&pretty
输出:
使用
server:9200/_stats
也可以获取有关所有别名的统计信息..比如每个别名的大小和元素数量,这非常有用,并提供有用的信息size param将显示的命中数从默认值(10)增加到500 .
逐步更改 from 以获取所有数据 .
Note the size param ,它将默认值(10)显示的命中数增加到每个分片1000个 .
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html
http://localhost:9200/foo/_search/? size = 1000&pretty = 1
您需要指定大小查询参数,因为默认值为10
对于Elasticsearch 6.x
要求:
GET /foo/_search?pretty=true
回复:在Hits-> total中,给出文档的计数
默认情况下,Elasticsearch返回10条记录,因此应明确提供大小 .
根据请求添加大小以获得所需的记录数 .
http:// :9200 / / _search?pretty = true&size =(记录数)
注意:最大页面大小不能超过index.max_result_window索引设置,默认为10,000 .
官方文档提供了这个问题的答案!你可以找到here .
您只需将size(1)替换为您想要查看的结果数量!
我认为支持lucene语法,所以:
http://localhost:9200/foo/_search?pretty=true&q=*:*
大小默认为10,因此您可能还需要
&size=BIGNUMBER
才能获得超过10个项目 . (其中BIGNUMBER等于您认为比数据集大的数字)但是,对于大型结果集,使用扫描搜索类型的elasticsearch文档suggests .
例如:
然后根据上面的文档链接继续请求建议 .
编辑:
scan
在2.1.0中已弃用 .scan
与_doc
排序的常规scroll
请求相比没有任何好处 . link to elastic docs(由@ christophe-roussy发现)您可以使用_count API获取
size
参数的值:返回
{count:X, ...}
. 提取值'X',然后执行实际查询:如果您只是添加一些大数字作为大小,Elasticsearch将会慢得多 significant ,使用一种方法来获取所有文档都使用扫描和滚动ID .
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
通过提供大小,elasticSearch返回的最大结果为10000
之后,您必须使用Scroll API获取结果并获取_scroll_id值并将此值放在scroll_id中
如果你想要提取数千条记录,那么...有些人给出了使用“滚动”的正确答案(注意:有些人还建议使用“search_type = scan” . 这已被弃用,并在v5.0中被删除 . 你不需要它)
从'搜索'查询开始,但指定'scroll'参数(这里我使用1分钟超时):
这包括你的第一批“点击” . 但我们不是在这里完成的 . 上面的curl命令的输出将是这样的:
使用_scroll_id非常重要,接下来应运行以下命令:
但是,传递scroll_id并不是设计为手动完成的 . 你最好的选择是编写代码来完成它 . 例如在java中:
现在,最后一个命令的LOOP使用SearchResponse来提取数据 .
如果有人正在寻找像我一样从Elasticsearch中检索的所有数据用于某些用例,我就是这样做的 . 而且,所有数据均指,所有索引和所有文档类型 . 我正在使用Elasticsearch 6.3
Elasticsearch reference
没有,除了@Akira Sendoh已经回答了如何实际获得所有文档 . 但即使是那个解决方案也会在没有日志的情况下崩溃我的 ES 6.3 服务 . 使用低级
elasticsearch-py
库对我有用的唯一方法是使用scroll()
api的scan helper:然而,现在更清洁的方式似乎是通过
elasticsearch-dsl
库,提供更抽象,更清晰的调用,例如:http://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#hits调整大小的最佳方法是在URL前面使用size = number
注意:可以在此大小中定义的最大值为10000.对于任何超过一万的值,它希望您使用滚动功能,这将最大限度地减少对性能的影响 .
您可以使用size = 0这将返回所有文档示例