我在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
其他回答
这是我使用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
你可以使用_count API来获取size参数的值:
http://localhost:9200/foo/_count?q=<your query>
返回{count:X,…}。提取值'X',然后执行实际查询:
http://localhost:9200/foo/_search?q=<your query>&size=X
使用kibana控制台和my_index作为搜索索引,可以提供以下内容。要求索引只返回索引的4个字段,您还可以添加大小来指示您希望由索引返回多少文档。从ES 7.6开始,你应该使用_source而不是filter,它会响应更快。
GET /address/_search
{
"_source": ["streetaddress","city","state","postcode"],
"size": 100,
"query":{
"match_all":{ }
}
}
注意:答案与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 RESULTS值的好方法
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 '
在所有索引中搜索user和tweet类型
您可以使用size=0,这将返回您所有的文档 例子
curl -XGET 'localhost:9200/index/type/_search' -d '
{
size:0,
"query" : {
"match_all" : {}
}
}'