我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
当前回答
使用python包elasticsearch-dsl的简单解决方案:
from elasticsearch_dsl import Search
from elasticsearch_dsl import connections
connections.create_connection(hosts=['localhost'])
s = Search(index="foo")
response = s.scan()
count = 0
for hit in response:
# print(hit.to_dict()) # be careful, it will printout every hit in your index
count += 1
print(count)
参见https://elasticsearch-dsl.readthedocs.io/en/latest/api.html#elasticsearch_dsl.Search.scan。
其他回答
简单!你可以使用size和from参数!
http://localhost:9200/[your index name]/_search?size=1000&from=0
然后逐渐改变,直到你得到所有的数据。
elasticsearch(ES)既支持GET请求,也支持POST请求,以便从ES集群索引中获取数据。
当我们执行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": {}
}
}
我建议使用elasticsearch http://mobz.github.io/elasticsearch-head/的UI插件 这将帮助您更好地了解您创建的索引,并测试您的索引。
如果你想提取成千上万的记录,那么……少数人给出了使用“scroll”的正确答案(注意:一些人还建议使用“search_type=scan”。这已被弃用,并在v5.0中被移除。你不需要它)
从一个“search”查询开始,但指定一个“scroll”参数(这里我使用了1分钟的超时):
curl -XGET 'http://ip1:9200/myindex/_search?scroll=1m' -d '
{
"query": {
"match_all" : {}
}
}
'
这包括你的第一批热门作品。但这还没完。上面curl命令的输出是这样的:
{"_scroll_id":"c2Nhbjs1OzUyNjE6NU4tU3BrWi1UWkNIWVNBZW43bXV3Zzs1Mzc3OkhUQ0g3VGllU2FhemJVNlM5d2t0alE7NTI2Mjo1Ti1TcGtaLVRaQ0hZU0FlbjdtdXdnOzUzNzg6SFRDSDdUaWVTYWF6YlU2Uzl3a3RqUTs1MjYzOjVOLVNwa1otVFpDSFlTQWVuN211d2c7MTt0b3RhbF9oaXRzOjIyNjAxMzU3Ow==","took":109,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":22601357,"max_score":0.0,"hits":[]}}
重要的是要有_scroll_id方便,接下来你应该运行以下命令:
curl -XGET 'localhost:9200/_search/scroll' -d'
{
"scroll" : "1m",
"scroll_id" : "c2Nhbjs2OzM0NDg1ODpzRlBLc0FXNlNyNm5JWUc1"
}
'
然而,传递scroll_id并不是设计为手动完成的。最好的办法是编写代码来实现它。例如,在java中:
private TransportClient client = null;
private Settings settings = ImmutableSettings.settingsBuilder()
.put(CLUSTER_NAME,"cluster-test").build();
private SearchResponse scrollResp = null;
this.client = new TransportClient(settings);
this.client.addTransportAddress(new InetSocketTransportAddress("ip", port));
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
scrollResp = client.prepareSearch(index).setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
.setQuery(queryBuilder)
.setSize(100).execute().actionGet();
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(timeVal))
.execute()
.actionGet();
现在在最后一个命令上使用LOOP来提取数据。
注意:答案与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类型
这是我使用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