முக்கிய பிரச்சனை என்னவென்றால், கொடுக்கப்பட்ட குறியீட்டில் உள்ள அனைத்து ஆவணங்களின் பட்டியலையும் பெறுவதற்கு 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!) <--- this is what you want! <--- this is what you want! <--- this is what you want! <--- this is what you want! <--- this is what you want! len(response) # number of total hits found by query (slow!) <--- this is what you want! <--- this is what you want! <--- this is what you want! <--- 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.'