Saturday, January 18, 2020

Getting started with Elastic Search and Python

Getting started with Elastic Search

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

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))
    
All Modules Loaded ! 

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()
Yupiee  Connected 

Creating a index

In [8]:
es.indices.create(index='test-index', ignore=400)
es.indices.create(index='test-index1', ignore=400)
Out[8]:
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'test-index1'}

Getting all Index

In [62]:
res =  es.indices.get_alias("*")
for Name in res:
    print(Name)
.kibana_task_manager_1
.kibana_1
house
.apm-agent-configuration
profile
In [14]:
indices=es.indices.get_alias().keys()
for Name in indices:
    print(Name)
.apm-agent-configuration
people
test-index1
.kibana_task_manager_1
kibana_sample_data_flights
.kibana_1
test-index

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])
Deleted person 
Deleted profile 
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))
RES1 : {'_index': 'person', '_type': 'people', '_id': 'kURRtW8B6cld-aTgNM-L', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}
RES2 : {'_index': 'person', '_type': 'people', '_id': 'kkRRtW8B6cld-aTgNM-w', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}

Get all elements

In [32]:
res =  es.indices.get_alias("*")
for Name in res:
    print(Name)
person
In [42]:
query={"query" : {
        "match_all" : {}
    }}

res = es.search(index="person", body=query, size=1000)

7 comments:

  1. Replies
    1. Pythonist: Getting Started With Elastic Search And Python >>>>> Download Now

      >>>>> Download Full

      Pythonist: Getting Started With Elastic Search And Python >>>>> Download LINK

      >>>>> Download Now

      Pythonist: Getting Started With Elastic Search And Python >>>>> Download Full

      >>>>> Download LINK ho

      Delete
  2. hi, getting error for this line,
    es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
    TypeError: __init__() missing 1 required positional argument: 'scheme'

    ReplyDelete
    Replies
    1. Hi i am getting the same error. were you able to resolve it?

      Delete
    2. Hey, unistall elasticsearch in your anaconda env currently it is 8.2.3: (pip uninstall elasticsearch) then install the older version: (pip install elasticsearch==7.9.1) this will fix your problem.

      Delete
  3. Pythonist: Getting Started With Elastic Search And Python >>>>> Download Now

    >>>>> Download Full

    Pythonist: Getting Started With Elastic Search And Python >>>>> Download LINK

    >>>>> Download Now

    Pythonist: Getting Started With Elastic Search And Python >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete

Learn How to Connect to the Glue Data Catalog using AWS Glue Iceberg REST endpoint

gluecat Learn How to Connect to the Glue Data Catalog using AWS Glue Iceberg REST e...