Created
September 12, 2021 21:21
-
-
Save cemdrk/47202299faa0e60676e2ca21ef5c58d2 to your computer and use it in GitHub Desktop.
Python Search Template Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
def get_sorted_contents(params): | |
contents = [] | |
es_url = "http://localhost:9200/contents/_search/template" | |
with requests.Session() as session: | |
response = session.post(es_url, data=json.dumps(params), headers={"Content-Type":"application/json"}) | |
for hit in response.json()["hits"]["hits"]: | |
contents.append(hit['_source']) | |
return contents | |
def main(): | |
es_params = { | |
"id": "sort-contents", | |
"params": { | |
"content_type": "movie", | |
"genre": "action", | |
"sort": "view_count", | |
"order": "desc" | |
} | |
} | |
contents = get_sorted_contents(es_params) | |
for content in contents: | |
print(content['title'], content['view_count']) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment