我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
我在Elasticsearch中有一个小数据库,出于测试目的,我想把所有记录拉回来。我正在尝试使用表单的URL…
http://localhost:9200/foo/_search?pretty=true&q={'matchAll':{''}}
有人能给我你要用来完成这个的URL吗?
当前回答
通过提供大小,elasticSearch将返回的最大结果是10000
curl -XGET 'localhost:9200/index/type/_search?scroll=1m' -d '
{
"size":10000,
"query" : {
"match_all" : {}
}
}'
在此之后,您必须使用Scroll API来获取结果并获得_scroll_id值,并将此值放入scroll_id中
curl -XGET 'localhost:9200/_search/scroll' -d'
{
"scroll" : "1m",
"scroll_id" : ""
}'
其他回答
使用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。
注意:答案与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类型
curl -XGET '{{IP/localhost}}:9200/{{Index name}}/{{type}}/_search?scroll=10m&pretty' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
}}'
官方文档提供了这个问题的答案!你可以在这里找到它。
{
"query": { "match_all": {} },
"size": 1
}
您只需将size(1)替换为您想要看到的结果的数量!
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": [
{