Fetch all the rows using elasticsearch_dsl

▼魔方 西西 提交于 2019-12-04 05:45:31

问题


Currently i am using the following program to extract the id and its severity information from elastic search .

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q

client = Elasticsearch(
    [
        #'http://user:secret@10.x.x.11:9200/',
        'http://10.x.x.11:9200/',
    ],
    verify_certs=True
)

s = Search(using=client, index="test")

response = s.execute()

for hit in response:
  print hit.message_id, hit.severity,  "\n\n"

i believe by default the query returns 10 rows. I am having more than 10000 rows in elastic search. I need to fetch all the information.

Can some one guide me how to run the same query to fetch all records ?


回答1:


You can use the scan() helper function in order to retrieve all docs from your test index:

from elasticsearch import Elasticsearch, helpers

client = Elasticsearch(
    [
        #'http://user:secret@10.x.x.11:9200/',
        'http://10.x.x.11:9200/',
    ],
    verify_certs=True
)

docs = list(helpers.scan(client, index="test", query={"query": {"match_all": {}}}))

for hit in docs:
  print hit.message_id, hit.severity,  "\n\n"


来源:https://stackoverflow.com/questions/38719162/fetch-all-the-rows-using-elasticsearch-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!