Esempio n. 1
0
class AppSearchConnector:
    def __init__(self, url, key, engine):
        self.url = url
        self.key = key
        self.engine = engine
        try:
            self.app_search = AppSearch(url, http_auth=self.key)
        except:
            LOGGER.critical("Couldn't create AppSerch Client")
            raise

    def insert_new_document(self, body):
        try:
            self.app_search.index_documents(engine_name=self.engine,
                                            documents=body)
        # ToDo Error Handling for response
        except:
            LOGGER.critical("Something went wrong with elastic for domain: %s",
                            str(body))

    def update_existing_document(self, body):
        try:
            self.app_search.put_documents(engine_name=self.engine,
                                          documents=body)
            LOGGER.debug("Updated document: %s", str(body))
        # ToDo Error Handling for response
        except:
            LOGGER.critical("Something went wrong with elastic for domain: %s",
                            str(body))

    def search_document(self, body):
        try:
            return self.app_search.search(engine_name=self.engine, body=body)
        except:
            LOGGER.critical("Something went wrong with elastic for domain: %s",
                            str(body))
Esempio n. 2
0
from elastic_enterprise_search import AppSearch
import glob, os
import json

app_search = AppSearch(
    "app_search_api_endpoint",
    http_auth="api_private_key"
)

response = []

print("Uploading movies to App Search...")

os.chdir("movies_directory")
for file in glob.glob("*.json"):
  with open(file, 'r') as json_file:
    try:
      response = app_search.index_documents(engine_name="movies",documents=json.load(json_file))
      print(".", end='', flush=True)
    except:
      print("Fail!")
      print(response)
      break