Skip to content

Instantly share code, notes, and snippets.

@kishorek
Created March 16, 2023 13:26
Show Gist options
  • Save kishorek/7b45b5dec39d61670307f8437b713180 to your computer and use it in GitHub Desktop.
Save kishorek/7b45b5dec39d61670307f8437b713180 to your computer and use it in GitHub Desktop.
Elastic Search + Mongo in Python
from pymongo import MongoClient
from elasticsearch import Elasticsearch
class MongoElasticCache:
def __init__(self, mongo_uri, mongo_db, mongo_collection, es_host, es_index):
self.mongo_client = MongoClient(mongo_uri)
self.mongo_db = self.mongo_client[mongo_db]
self.mongo_collection = self.mongo_db[mongo_collection]
self.es_client = Elasticsearch([es_host])
self.es_index = es_index
def save_and_cache(self, data):
# Save data to MongoDB
mongo_id = self.mongo_collection.insert_one(data).inserted_id
data["_id"] = mongo_id
# Cache data in Elasticsearch
self.es_client.index(index=self.es_index, id=str(mongo_id), body=data)
def search(self, query):
# Search Elasticsearch
res = self.es_client.search(index=self.es_index, body=query)
return res['hits']['hits']
mongo_uri = "mongodb://username:password@localhost:27017"
mongo_db = "test_db"
mongo_collection = "test_collection"
es_host = "localhost:9200"
es_index = "test_index"
cache = MongoElasticCache(mongo_uri, mongo_db, mongo_collection, es_host, es_index)
# Save and cache data
data = {
"title": "Example",
"description": "This is an example document."
}
cache.save_and_cache(data)
# Search cached data
query = {
"query": {
"match": {
"title": "Example"
}
}
}
results = cache.search(query)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment