해결됨: django-elasticsearch-dsl의 모든 문서 목록 가져오기

주요 문제는 Elasticsearch가 주어진 인덱스의 모든 문서 목록을 가져오는 기본 방법이 없다는 것입니다. 검색 API를 사용할 수 있지만 이렇게 하면 한 번에 하나의 문서만 반환됩니다.

나는 django-elasticsearch-dsl을 사용하고 있고 인덱스의 모든 문서 목록을 얻고 싶습니다. 어떻게 할 수 있습니까?

A:

당신은을 사용할 수 있습니다 Search 의 개체 django_elasticsearch_dsl. 의 하위 클래스입니다. ElasticsearchDSL 개체를 검색하므로 거기에서 모든 방법을 사용할 수도 있습니다. 이에 대한 문서는 다음과 같습니다. https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#the-search-object

모든 문서 가져오기

<code>from django_elasticsearch_dsl import Search, Index

s = Search(index='blog')
.query('match', title='python')

response = s.execute()

for hit in response: # iterate over hits from response object
print(hit) # print each hit as a dict (default)

for hit in s: # iterate over hits from search query directly
print(hit) # print each hit as a dict (default)

for hit in s[0:10]: # slice results to get only first 10 hits
print(hit) # print each hit as a dict (default)

len(s) # number of total hits found by query (slow!) &lt;--- this is what you want! &lt;--- this is what you want! &lt;--- this is what you want! &lt;--- this is what you want! &lt;--- this is what you want! len(response) # number of total hits found by query (slow!) &lt;--- this is what you want! &lt;--- this is what you want! &lt;--- this is what you want! &lt;--- this is what you want! list(s)[0] # first result as a Python dictionary list(response)[0] # first result as a Python dictionary response[0] # first result as an ElasticSearch Hit response[0].meta # metadata associated with the Hit response[0].meta.score # score associated with the Hit response[0].title # title field value list(response)[1]['title'] ## second result's 'title' field value<;/pre>;<;br />;>;br />;The above code will return all documents matching your query, but it will not return any fields other than _id and _type unless they are explicitly requested via source(). To retrieve more fields, use source():<;br />;from django_elasticsearch_dsl import Search, Index, F ;from elasticsearch_dsl import Q ;import json ;import pprint ;pp = pprint.PrettyPrinter();pprint = pp.pprint ;s = Search().query('match', title='python').source([ 'title', 'body' ]) ;for i in range((len(s))): pprint((json.loads((str)(s[i]).replace("'", """)))) ;## or simply do it like below :## [{'body': 'Python and Django go together like peanut butter and jelly.'

Related posts:

Leave a Comment