def index()

in src/qldb_streaming_to_es_sample/clients/elasticsearch.py [0:0]


    def index(self, index, id, body, version):
        """
        Indexes documents to elasticsearch.
        Uses external version support to handle duplicates.
        https://www.elastic.co/blog/elasticsearch-versioning-support
        https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-version-types
        """
        try:
            response = self.es_client.index(index=index, id=id,
                                            body=body, version=version, version_type="external")

            print("Indexed document with id: {id}, body: {body}"
                  " and version: {version}".format(id=id, body=body,
                                                   version=version))

            return response

        except (SerializationError, ConflictError,
                RequestError) as e:  # https://elasticsearch-py.readthedocs.io/en/master/exceptions.html#elasticsearch.ElasticsearchException
            print("Elasticsearch Exception occured while indexing id={id}, body={body} and"
                  "version={version}. Error: {error}".format(id=id, body=body, version=version,
                                                             error=str(e)))
            return None