Joh*_*ore 463 database bigdata query-string elasticsearch elasticsearch-dsl
我在Elasticsearch中有一个小型数据库,出于测试目的,我希望将所有记录拉回来.我试图使用表单的URL ...
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
Run Code Online (Sandbox Code Playgroud)
有人可以给我你用来完成这个的URL吗?
Ste*_*sey 718
我认为支持lucene语法,所以:
http://localhost:9200/foo/_search?pretty=true&q=*:*
大小默认为10,因此您可能还需要&size=BIGNUMBER
获得10个以上的项目.(其中BIGNUMBER等于您认为比您的数据集大的数字)
但是,elasticsearch文档建议使用扫描搜索类型来获取大型结果集.
例如:
curl -XGET 'localhost:9200/foo/_search?search_type=scan&scroll=10m&size=50' -d '
{
"query" : {
"match_all" : {}
}
}'
Run Code Online (Sandbox Code Playgroud)
然后根据上面的文档链接继续请求建议.
编辑:scan
在2.1.0中弃用.
scan
与scroll
按常规请求排序相比,不提供任何好处_doc
.链接到弹性文档(由@ christophe-roussy发现)
lfe*_*445 131
http://127.0.0.1:9200/foo/_search/?size=1000&pretty=1
^
Run Code Online (Sandbox Code Playgroud)
请注意size param,它会将默认值(10)显示的匹配数增加到每个分片1000个.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html
小智 32
elasticsearch(ES)支持从ES集群索引获取数据的GET或POST请求.
当我们做GET时:
http://localhost:9200/[your index name]/_search?size=[no of records you want]&q=*:*
Run Code Online (Sandbox Code Playgroud)
当我们做POST时:
http://localhost:9200/[your_index_name]/_search
{
"size": [your value] //default 10
"from": [your start index] //default 0
"query":
{
"match_all": {}
}
}
Run Code Online (Sandbox Code Playgroud)
我建议使用带弹性搜索的UI插件http://mobz.github.io/elasticsearch-head/ 这将帮助您更好地了解您创建的索引并测试索引.
vjp*_*ian 26
下面的查询将返回您想要返回的NO_OF_RESULTS.
curl -XGET 'localhost:9200/foo/_search?size=NO_OF_RESULTS' -d '
{
"query" : {
"match_all" : {}
}
}'
Run Code Online (Sandbox Code Playgroud)
现在,这里的问题是你想要返回所有记录.很自然地,在编写查询之前,您不会知道NO_OF_RESULTS的值.
我们如何知道您的文档中存在多少条记录?只需在下面输入查询即可
curl -XGET 'localhost:9200/foo/_search' -d '
Run Code Online (Sandbox Code Playgroud)
这会给你一个看起来像下面的结果
{
hits" : {
"total" : 2357,
"hits" : [
{
..................
Run Code Online (Sandbox Code Playgroud)
结果总计告诉您文档中有多少记录可用.所以,这是了解NO_OF结果值的好方法
curl -XGET 'localhost:9200/_search' -d '
Run Code Online (Sandbox Code Playgroud)
搜索所有索引中的所有类型
curl -XGET 'localhost:9200/foo/_search' -d '
Run Code Online (Sandbox Code Playgroud)
搜索foo索引中的所有类型
curl -XGET 'localhost:9200/foo1,foo2/_search' -d '
Run Code Online (Sandbox Code Playgroud)
搜索foo1和foo2索引中的所有类型
curl -XGET 'localhost:9200/f*/_search
Run Code Online (Sandbox Code Playgroud)
搜索以f开头的任何索引中的所有类型
curl -XGET 'localhost:9200/_all/type1,type2/_search' -d '
Run Code Online (Sandbox Code Playgroud)
在所有索引中搜索类型用户和推文
Hun*_*orn 18
这是我使用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
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html
The*_*hMe 11
使用server:9200/_stats
也得到统计您所有的别名..喜欢的大小和每别名元素的数量,这是非常有用的,并提供有用的信息
Som*_*mum 10
如果你想要提取数千条记录,那么......有些人给出了使用"滚动"的正确答案(注意:有些人还建议使用"search_type = scan".这已被弃用,并且已删除v5.0.你不需要它)
从"搜索"查询开始,但指定"滚动"参数(此处我使用1分钟超时):
curl -XGET 'http://ip1:9200/myindex/_search?scroll=1m' -d '
{
"query": {
"match_all" : {}
}
}
'
Run Code Online (Sandbox Code Playgroud)
这包括你的第一批"点击".但我们没有在这里完成.上面的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"
}
'
Run Code Online (Sandbox Code Playgroud)
但是,传递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();
Run Code Online (Sandbox Code Playgroud)
现在最后一个命令的LOOP使用SearchResponse来提取数据.
小智 10
如果它是一个小数据集(例如 1K 记录),您可以简单地指定size
:
curl localhost:9200/foo_index/_search?size=1000
Run Code Online (Sandbox Code Playgroud)
该比赛所有的查询是不需要的,因为它是隐含的。
如果你有一个中等大小的数据集,比如 1M 记录,你可能没有足够的内存来加载它,所以你需要一个scroll。
滚动就像数据库中的游标。在 Elasticsearch 中,它会记住您离开的位置并保持索引的相同视图(即防止搜索器在刷新时离开,防止段合并)。
在 API 方面,您必须向第一个请求添加滚动参数:
curl 'localhost:9200/foo_index/_search?size=100&scroll=1m&pretty'
Run Code Online (Sandbox Code Playgroud)
您将返回第一页和滚动 ID:
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAADEWbmJlSmxjb2hSU0tMZk12aEx2c0EzUQ==",
"took" : 0,
...
Run Code Online (Sandbox Code Playgroud)
请记住,您返回的滚动 ID 和超时对于下一页都是有效的。这里的一个常见错误是指定一个非常大的超时(值scroll
),这将涵盖处理整个数据集(例如 1M 记录)而不是一页(例如 100 条记录)。
要获取下一页,请填写最后一个滚动 ID 和应该持续到获取以下页面的超时时间:
curl -XPOST -H 'Content-Type: application/json' 'localhost:9200/_search/scroll' -d '{
"scroll": "1m",
"scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAADAWbmJlSmxjb2hSU0tMZk12aEx2c0EzUQ=="
}'
Run Code Online (Sandbox Code Playgroud)
如果您有很多要导出(例如 1B 文档),您将需要并行化。这可以通过切片滚动来完成。假设您要导出 10 个线程。第一个线程会发出这样的请求:
curl -XPOST -H 'Content-Type: application/json' 'localhost:9200/test/_search?scroll=1m&size=100' -d '{
"slice": {
"id": 0,
"max": 10
}
}'
Run Code Online (Sandbox Code Playgroud)
您将返回第一页和滚动 ID,这与正常的滚动请求完全一样。您可以像普通滚动一样使用它,只是您获得了 1/10 的数据。
其他线程会做同样的事情,除了id
1, 2, 3 ...
Kra*_*ken 10
实际上,您不需要将正文传递给match_all
,可以通过对以下 URL 的 GET 请求来完成。这是最简单的形式。
http://localhost:9200/foo/_search
Elasticsearch将得到显著慢,如果你只需要添加一些大的数量大小,一个方法用来获取所有文档是使用扫描和滚动标识.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
简单!你可以使用size
和from
参数!
http://localhost:9200/[your index name]/_search?size=1000&from=0
Run Code Online (Sandbox Code Playgroud)
然后from
逐渐改变,直到获得所有数据.
小智 6
调整大小的最佳方法是在URL前面使用size = number
Curl -XGET "http://localhost:9200/logstash-*/_search?size=50&pretty"
Run Code Online (Sandbox Code Playgroud)
注意:可以在此大小中定义的最大值为10000.对于任何超过一万的值,它希望您使用滚动功能,这将最大限度地减少对性能的影响.
您可以使用_count
API获取size
参数的值:
http://localhost:9200/foo/_count?q=<your query>
Run Code Online (Sandbox Code Playgroud)
退货{count:X, ...}
.提取值"X"然后执行实际查询:
http://localhost:9200/foo/_search?q=<your query>&size=X
Run Code Online (Sandbox Code Playgroud)
来自 Kibana DevTools:
GET my_index_name/_search
{
"query": {
"match_all": {}
}
}
Run Code Online (Sandbox Code Playgroud)