Python - Index & Search documents in ElasticSearch

Gist Page : example-python-read-and-write-from-elasticsearch

Common part

Libraries dependency

import pandas as pd
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import os

ElasticSearch Connection

Default port is 9200

Connection

# ====== Connection ====== #
# Connection to ElasticSearch
es = Elasticsearch(['http://'+os.environ['ELASTICSEARCH_IP'] +':'+ os.environ['ELASTICSEARCH_PORT']],timeout=600)
# Simple index creation with no particular mapping
es.indices.create(index='helloworld',body={})

How to insert a document in ElasticSearch with Python ?

Code example

# ====== Inserting Documents ====== #
# Creating a simple Pandas DataFrame
liste_hello = ['hello1','hello2']
liste_world = ['world1','world2']
df = pd.DataFrame(data = {'hello' : liste_hello, 'world': liste_world})

# Bulk inserting documents. Each row in the DataFrame will be a document in ElasticSearch
documents = df.to_dict(orient='records')
bulk(es, documents, index='helloworld',doc_type='foo', raise_on_error=True)

How to search a document in ElasticSearch with Python ?

Code example

# ====== Searching Documents ====== #
# Retrieving all documents in index (no query given)
documents = es.search(index='helloworld',body={})['hits']['hits']
df = pd.DataFrame(documents)

# Retrieving documents in index that match a query
documents2 = es.search(index='helloworld',body={"query":{"term":{"hello" : "hello1" }}})['hits']['hits']
df2 = pd.DataFrame(documents2)