Skip to content

Instantly share code, notes, and snippets.

@rebeccabilbro
Last active February 25, 2021 08:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebeccabilbro/5eae62bd85a8e270309d34e75047320b to your computer and use it in GitHub Desktop.
Save rebeccabilbro/5eae62bd85a8e270309d34e75047320b to your computer and use it in GitHub Desktop.
Create an ElasticSearch instance, and given a list of documents, index the documents into ElasticSearch.
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch
class ElasticIndexer(object):
"""
Create an ElasticSearch instance, and given a list of documents,
index the documents into ElasticSearch.
"""
def __init__(self):
self.elastic_search = Elasticsearch()
def make_documents(self, textdict):
"""
A textdict is a dictionary of documents where each key corresponds
to a document category and each value is a list of documents
"""
for category, docs in textdict:
for document in docs:
yield {
"_index": category,
"_type": "_doc",
"description": document
}
def index(self, textdict):
bulk(self.elastic_search, self.make_documents(textdict))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment