我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
当前回答
除了@Akira Sendoh,没有人回答如何实际获得所有文档。但是即使是这个解决方案也会使我的ES 6.3服务在没有日志的情况下崩溃。对我来说,使用底层elasticsearch-py库唯一有效的是通过使用scroll() api的扫描助手:
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
然而,现在更简洁的方法似乎是通过elasticsearch-dsl库,它提供了更抽象、更简洁的调用,例如:http://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#hits
其他回答
curl -XGET '{{IP/localhost}}:9200/{{Index name}}/{{type}}/_search?scroll=10m&pretty' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
}}'
简单!你可以使用size和from参数!
http://localhost:9200/[your index name]/_search?size=1000&from=0
然后逐渐改变,直到你得到所有的数据。
除了@Akira Sendoh,没有人回答如何实际获得所有文档。但是即使是这个解决方案也会使我的ES 6.3服务在没有日志的情况下崩溃。对我来说,使用底层elasticsearch-py库唯一有效的是通过使用scroll() api的扫描助手:
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
然而,现在更简洁的方法似乎是通过elasticsearch-dsl库,它提供了更抽象、更简洁的调用,例如:http://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#hits
我认为lucene语法是受支持的,所以:
http://localhost: 9200 / foo / _search ?漂亮=真的,q = *: *
size默认为10,因此您可能还需要&size=BIGNUMBER来获取超过10个项目。(其中BIGNUMBER等于一个你认为比你的数据集大的数字)
但是,elasticsearch文档建议对于较大的结果集,使用扫描搜索类型。
EG:
curl -XGET 'localhost:9200/foo/_search?search_type=scan&scroll=10m&size=50' -d '
{
"query" : {
"match_all" : {}
}
}'
然后按照上面建议的文档链接继续请求。
EDIT: scan 2.1.0中已弃用。
与按_doc排序的常规滚动请求相比,Scan并没有提供任何好处。弹性文档链接(由@christophe-roussy提供)
如果你只是添加一些大的数字作为大小,Elasticsearch会变得非常慢,一种获取所有文档的方法是使用scan和scroll id。
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
在Elasticsearch v7.2中,您可以这样做:
POST /foo/_search?scroll=1m
{
"size": 100,
"query": {
"match_all": {}
}
}
这样的结果将包含一个_scroll_id,您必须查询它来获得下一个100块。
POST /_search/scroll
{
"scroll" : "1m",
"scroll_id" : "<YOUR SCROLL ID>"
}