Beginners Guide to CRUD with Elasticsearch in Python

{
“name” : “2537e85ac29c”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “5sC_5Eh5TeegUcF7n5GJKQ,
“version” : {
“number” : “7.5.2”,
“build_hash” : “8bec50e1e0ad29dad5653712cf3bb580cd1afcdf”,
“build_date” : “2020–01–15T12:11:52.313576Z”,
“build_snapshot” : false,
“lucene_version” : “8.3.0”,
“minimum_wire_compatibility_version” : “6.8.0”,
“minimum_index_compatibility_version” : “6.0.0-beta1”
},
“tagline” : “You Know, for Search”
}
pip install elasticsearch
from elasticsearch import Elasticsearch
url = 'http://root:root@localhost:9200'
es = Elasticsearch(url)
# es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
mapping = '''
{
"mappings": {
"properties": {
"item_name": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}'''
index_exists = es.indices.exists(index = index_name)
index_name = 'retail_store'if not index_exists:
es.indices.create(index = index_name, body = mapping)
curl -XGET http://localhost:9200/retail_store#output
{
"retail_store": {
"aliases": {},
"mappings": {
"properties": {
"item_name": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
},
"settings": {
"index": {
"creation_date": "1649414470792",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "EMYS2SQvQbanRT4rJQyfmA",
"version": {
"created": "7050299"
},
"provided_name": "retail_store"
}
}
}
}
doc = {"item_name": "orange", "price": 200}
es.index(index=index_name, doc_type="_doc", body=doc)
from elasticsearch import helpersdocs = [{"item_name": "apple", "price": 100}, {"item_name": "mango", "price": 150}, {"item_name": "cherry", "price": 200}, {"item_name": "litchi", "price": 250}, {"item_name": "chips", "price": 300}, {"item_name": "cream", "price": 350}, {"item_name": "plum", "price": 400}, {"item_name": "cake", "price": 450}, {"item_name": "biscuit", "price": 500}, {"item_name": "chocolate", "price": 550}]helpers.bulk(es, docs, index=index_name, doc_type='_doc')
curl -XGET http://localhost:9200/retail_store/_count# output
{"count":11,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0}}
query = {
"query" : {
"bool" : {
"must" : {
"term" : {
"item_name" : "apple"
}
}
}
}
}
results = es.search(index=index_name, body=query)# output
{
"took":31,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":1,
"relation":"eq"
},
"max_score":0.9808292,
"hits":[
{
"_index":"retail_store",
"_type":"_doc",
"_id":"7nLOCIABFhzSMyNPvUmZ",
"_score":0.9808292,
"_source":{
"item_name":"apple",
"price":100
}
}
]
}
}
results = es.search(index=index_name, body=query, size = 1000)
helpers.scan(client=es, query=query, index=index_name)
query = {
"query":{
"range":{
"price":{
"gte":100
}
}
}
}
doc = es.search(index=index_name, body=query)# iterate through all the hits to get the document idfor i in range(len(doc["hits"]["hits"])):
doc_body = doc["hits"]["hits"][i]["_source"]
doc_id = doc["hits"]["hits"][i]["_id"]
# Once you have the id, make an update request
doc_body["price"] = 50
es.update(index=index_name, id=doc_id, body={"doc": doc_body})
es.delete(index = index_name, id = doc_id)
query = {
"query": {
"range": {
"price": {
"gte": 400
}
}
}
}
es.delete_by_query(index = index_name, query = query)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store