Getting started with Elastic Search and Python¶
- Hello! I’m Soumil Nitin Shah, a Software and Hardware Developer based in New York City. I have completed by Bachelor in Electronic Engineering and my Double master’s in Computer and Electrical Engineering. I Develop Python Based Cross Platform Desktop Application , Webpages , Software, REST API, Database and much more I have more than 2 Years of Experience in Python
- Website: http://soumilshah.herokuapp.com/
- Blog: https://soumilshah1995.blogspot.com/
- Youtube : https://www.youtube.com/channel/UC_eOodxvwS_H7x2uLQa-svw
- Linkedin : https://www.linkedin.com/in/shah-soumil/
- FaceBook : http://soumilshah.herokuapp.com/#!
- Github : https://github.com/soumilshah1995
- Email : shahsoumil519@gmail.com
Step 1: Define the Imports¶
In [3]:
try:
import os
import sys
import elasticsearch
from elasticsearch import Elasticsearch
import pandas as pd
print("All Modules Loaded ! ")
except Exception as e:
print("Some Modules are Missing {}".format(e))
Terminology¶
what is Index ? Well Index is similar to Database Name
what is Type ? Type is similar to Table Name
In [6]:
def connect_elasticsearch():
es = None
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
if es.ping():
print('Yupiee Connected ')
else:
print('Awww it could not connect!')
return es
es = connect_elasticsearch()
Creating a index¶
In [8]:
es.indices.create(index='test-index', ignore=400)
es.indices.create(index='test-index1', ignore=400)
Out[8]:
Getting all Index¶
In [62]:
res = es.indices.get_alias("*")
for Name in res:
print(Name)
In [14]:
indices=es.indices.get_alias().keys()
for Name in indices:
print(Name)
Deleting all Index¶
In [69]:
indices=es.indices.get_alias().keys()
for Name in indices:
print("Deleted {} ".format(Name))
es.indices.delete(index=Name, ignore=[400, 404])
In [16]:
res = es.indices.get_alias("*")
for Name in res:
print(Name)
- Hence proved we deleted all the index we had quite easy lets move foreward
Lets create a Database and a Table and insert a Data¶
In [22]:
e1={
"first_name":"Soumil",
"last_name":"Shah",
"age": 24,
"about": "Full stack Software Developers ",
"interests": ['Youtube','music'],
}
e2={
"first_name":"nitin",
"last_name":"Shah",
"age": 58,
"about": "Soumil father ",
"interests": ['Stock','Relax'],
}
In [23]:
es.indices.create(index='person', ignore=400)
res1 = es.index(index='person',doc_type='people', body=e1)
res2 = es.index(index='person',doc_type='people', body=e2)
print("RES1 : {}".format(res1))
print("RES2 : {}".format(res2))
Get all elements¶
In [32]:
res = es.indices.get_alias("*")
for Name in res:
print(Name)
In [42]:
query={"query" : {
"match_all" : {}
}}
res = es.search(index="person", body=query, size=1000)