Skip to content

Instantly share code, notes, and snippets.

@Verina-Armanyous
Created December 5, 2021 23:23
Show Gist options
  • Save Verina-Armanyous/1656e315d98e7e807149d97e21d35163 to your computer and use it in GitHub Desktop.
Save Verina-Armanyous/1656e315d98e7e807149d97e21d35163 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"from elasticsearch import Elasticsearch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Connect to the cluster\n",
"es = Elasticsearch([{'host': 'localhost', 'port': 9200}])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check the status of the cluster \n",
"print(es.cluster.health())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"es.indices.create(index='pokemon_characters', body={\"number_of_replicas\": 2, \"number_of_shards\":3})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = requests.get('http://localhost:9200')\n",
"i = 1\n",
"while i <= 898:\n",
" r = requests.get('https://pokeapi.co/api/v2/pokemon/'+ str(i))\n",
" es.index(index='pokemon_characters', doc_type='pokemon', id=i, body=json.loads(r.content))\n",
" i=i+1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# return 3 documents \n",
"res = es.search(index=\"pokemon_characters\", query = {\n",
" 'match_all' : {}}, size = '3')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"documents = res['hits']['hits']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for num, doc in enumerate(documents):\n",
" for key, value in doc.items():\n",
" print (key, \":\", value)\n",
" \n",
" print (\"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# return the character with id = 5\n",
"es.get(index='pokemon_characters', doc_type='pokemon', id=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"es.search(index=\"pokemon_characters\", body={\"query\": {\"match\" : { \"name\" : 'cat'}}})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment