我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
当前回答
这是完成你想要的查询, (我建议使用Kibana,因为它有助于更好地理解查询)
GET my_index_name/my_type_name/_search
{
"query":{
"match_all":{}
},
size : 20,
from : 3
}
要获得所有的记录,你必须使用“match_all”查询。
Size是你想要获取的记录的数目(一种限制)。 默认情况下,ES只返回10条记录
从就像跳过,跳过前3个记录。
如果您想获取所有的记录,只需使用“total”字段的值 一旦你从Kibana命中这个查询的结果,并使用“大小”。
其他回答
要从所有索引中返回所有记录,您可以执行:
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 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": [
{
如果它是一个小数据集(例如1K记录),你可以简单地指定大小:
curl localhost:9200/foo_index/_search?size=1000
不需要match all查询,因为它是隐式的。
如果你有一个中等规模的数据集,比如1M的记录,你可能没有足够的内存来加载它,所以你需要滚动。
滚动就像数据库中的游标。在Elasticsearch中,它会记住你离开的地方,并保持相同的索引视图(即防止搜索器随着刷新而离开,防止段合并)。
api方面,你必须添加一个滚动参数到第一个请求:
curl 'localhost:9200/foo_index/_search?size=100&scroll=1m&pretty'
你会得到第一页和一个滚动ID:
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAADEWbmJlSmxjb2hSU0tMZk12aEx2c0EzUQ==",
"took" : 0,
...
请记住,您获得的滚动ID和超时都对下一页有效。这里一个常见的错误是指定了一个非常大的超时(scroll的值),这将涵盖处理整个数据集(例如1M条记录)而不是一个页面(例如100条记录)。
要获取下一页,请填写最后一个滚动ID和一个超时,该超时应该持续到获取以下页面:
curl -XPOST -H 'Content-Type: application/json' 'localhost:9200/_search/scroll' -d '{
"scroll": "1m",
"scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAADAWbmJlSmxjb2hSU0tMZk12aEx2c0EzUQ=="
}'
如果你有很多东西要导出(例如1B文档),你会想要并行化。这可以通过切片滚动来实现。假设您想在10个线程上导出。第一个线程会发出这样的请求:
curl -XPOST -H 'Content-Type: application/json' 'localhost:9200/test/_search?scroll=1m&size=100' -d '{
"slice": {
"id": 0,
"max": 10
}
}'
您将返回第一页和一个滚动ID,与正常的滚动请求完全相同。你会像普通的滚动一样使用它,除了你得到的数据只有十分之一。
其他线程也会做同样的事情,除了id是1,2,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
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
使用kibana控制台和my_index作为搜索索引,可以提供以下内容。要求索引只返回索引的4个字段,您还可以添加大小来指示您希望由索引返回多少文档。从ES 7.6开始,你应该使用_source而不是filter,它会响应更快。
GET /address/_search
{
"_source": ["streetaddress","city","state","postcode"],
"size": 100,
"query":{
"match_all":{ }
}
}