Last active
February 25, 2021 08:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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