我在 Elasticsearch 中有一个小型数据库,出于测试目的,我想拉回所有记录。我正在尝试使用表单的 URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人可以给我你用来完成此任务的 URL,好吗?
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 文档):请注意,from + size 不能超过 index.max_result_window 索引设置,默认为 10,000。
这会返回1000,不是全部,user3078523是对的,这个方法有一个限制max_result_window
它有一个最大值,而且(如果你有数千条记录要获取)这是一个相当笨重的方法来达到这个最大值。相反,您应该使用“滚动”查询。
你应该通过pretty
参数为布尔值:curl -XGET 'localhost:9200/logs/_search/?size=1000&pretty=true'
这是我正在寻找的答案。没有传递请求参数的那个q
.谢谢你!
elasticsearch(ES) 支持从 ES 集群索引获取数据的 GET 或 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": {}
}
}
我建议使用带有弹性搜索的 UI 插件http://mobz.github.io/elasticsearch-head/这将帮助您更好地了解您创建的索引并测试您的索引。
正如另一位用户提到的:from
+size
不能超过index.max_result_window
默认为 10,000 的索引设置
这种方法有一个最大值,而且(如果你有成千上万的记录要获取)这是一个相当笨重的方法来达到这个最大值。相反,您应该使用“滚动”查询
奇怪的是,官方文档显示curl -XGET ... -d '{...}'
这是一个un
官方混合风格的要求。感谢您显示正确的 GET 和 POST 格式。
笔记:答案与旧版本的 Elasticsearch 有关
0.90
.此后发布的版本具有更新的语法。请参考其他答案,这些答案可能会为您正在寻找的最新答案提供更准确的答案。
下面的查询将返回您希望返回的 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 '
搜索 foo 索引中的所有类型
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 个结果。
之前的回复是三年前的。将其更新为当前版本。
这是我使用 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
谢谢马克,这正是我想要的!在我的情况下(ELK 6.2.1,python 3),search_type 参数无效,并且自 ELK 6.0 以来不再需要 document_type
完美的解决方案!谢谢。我正在使用elasticsearch_dsl==5.4.0
它没有search_type = 'scan',
.
ES 6.3。这个例子使我的 Elasticsearch 服务崩溃,试图滚动 110k 文档size=10000
,在第 5-7 次迭代之间的某个地方。和status=127
,main ERROR Null object returned for RollingFile in Appenders
,main ERROR Unable to locate appender "rolling" for logger config "root"
没有登录/var/log/elasticsearch/elasticsearch.log
作为记录,python客户端实现了一个scan
在引擎盖下滚动的助手`(至少从版本 5.xx 开始)
search_type = 'scan'
已弃用。没有它,类似的代码也可以工作,尽管在旧文档中隐藏了一些有趣的差异。elastic.co/guide/en/elasticsearch/reference/1.4/…特别是,当迁移到不使用 search_type=scan 时,第一个“搜索”查询将与第一批要处理的结果一起出现。
Elasticsearch 将获得重要的如果您只是添加一些大数字作为大小,则速度较慢,一种用于获取所有文档的方法是使用扫描和滚动 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>"
}
这个答案需要更多更新。search_type=scan
现在已弃用。所以你应该删除它,但是行为发生了一些变化。第一批数据来自初始搜索调用。您提供的链接确实显示了正确的方法。
我的评论是真的要注意你不能只添加任何数字作为大小,因为它会慢很多。所以我删除了代码示例,人们可以按照链接获取正确的代码。
@WoodyDRN最好在您的答案中包含代码(即使它变旧了),这样在链接失效时它仍然可用。
…其中“foo”是要显示所有记录的索引的名称。
所有答案仅使用
size
查询参数不正确。不考虑价值size
在查询中,ES 将返回最大值index.max_result_window
docs(默认为 10k)作为响应。参考scroll
和search_after
.