def save(self): """ Overwrite save method to add to Elasticsearch. """ # TODO-2 should we validate the save worked before going to ES? from models.topic import Topic from models.user import User data = json_prep(self.deliver()) topic = Topic.get(id=self['topic_id']) if topic: data['topic'] = json_prep(topic.deliver()) user = User.get(id=self['user_id']) if user: data['user'] = json_prep(user.deliver()) es.index( index='entity', doc_type='post', body=data, id=self['id'], ) return super().save()
def save(self, db_conn): """ Overwrite save method to add to Elasticsearch. """ # TODO-2 should we validate the save worked before going to ES? from database.topic import get_topic, deliver_topic from database.user import get_user, deliver_user data = json_prep(self.deliver()) topic = get_topic({'id': self['topic_id']}, db_conn) if topic: data['topic'] = json_prep(deliver_topic(topic)) user = get_user({'id': self['user_id']}, db_conn) if user: data['user'] = json_prep(deliver_user(user)) es.index( index='entity', doc_type='post', body=data, id=self['id'], ) return super().save(db_conn)
def add_topic_to_es(topic): """ Add the topic to ElasticSearch. """ data = json_prep(deliver_topic(topic)) es.index( index='entity', doc_type='topic', body=data, id=data['id'], )
def save(self): """ Overwrite save method to add to Elasticsearch. """ # TODO-2 should we validate the save worked before going to ES? data = json_prep(self.deliver()) data["avatar"] = self.get_avatar() es.index(index="entity", doc_type="user", body=data, id=self["id"]) return super().save()
def save(self): """ Overwrite save method to add to Elasticsearch. """ # TODO-2 should we validate the save worked before going to ES? es.index( index='entity', doc_type='topic', body=json_prep(self.deliver()), id=self['id'], ) return super().save()
def save(self, db_conn): """ Overwrite save method to add to Elasticsearch. """ # TODO-2 should we validate the save worked before going to ES? es.index( index='entity', doc_type='topic', body=json_prep(self.deliver()), id=self['id'], ) return super().save(db_conn)
def save(self, db_conn): """ Overwrite save method to add to Elasticsearch. """ # TODO-2 should we validate the save worked before going to ES? doc_type = self.__class__.__name__.lower() if 'card' in doc_type: doc_type = 'card' if self['status'] == 'accepted': es.index( index='entity', doc_type=doc_type, body=json_prep(self.deliver()), id=self['entity_id'], ) return super().save(db_conn)
def add_user_to_es(user): """ Add the user to Elasticsearch. """ data = json_prep(deliver_user(user)) data['avatar'] = get_avatar(user['email']) return es.index( index='entity', doc_type='user', body=data, id=data['id'], )
def add_post_to_es(post, db_conn): """ Upsert the post data into elasticsearch. """ from database.topic import get_topic, deliver_topic from database.user import get_user, deliver_user data = json_prep(deliver_post(post)) topic = get_topic({'id': post['topic_id']}, db_conn) if topic: data['topic'] = json_prep(deliver_topic(topic)) user = get_user({'id': post['user_id']}, db_conn) if user: data['user'] = json_prep(deliver_user(user)) return es.index( index='entity', doc_type='post', body=data, id=post['id'], )
'email': '*****@*****.**', 'password': bcrypt.encrypt('example1'), 'settings': { 'email_frequency': 'daily', 'view_sets': 'public', 'view_follows': 'public', } }]).run(database.db_conn)) users = database.db.table('users').run(database.db_conn) for user in users: data = pick(json_prep(user), ('id', 'name')) data['avatar'] = get_avatar(user['email']) es.index( index='entity', doc_type='user', body=data, id=user['id'], ) (database.db.table('units').insert([{ 'id': 'plus-1', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'entity_id': 'plus', 'previous_id': None, 'language': 'en', 'name': 'Adding two number together.', 'status': 'accepted', 'available': True, 'tags': ['math'], 'body': 'The joy and pleasure of adding numbers.',
'password': bcrypt.encrypt('example1'), 'settings': { 'email_frequency': 'daily', 'view_sets': 'public', 'view_follows': 'public', } }]) .run(database.db_conn)) users = database.db.table('users').run(database.db_conn) for user in users: data = pick(json_prep(user), ('id', 'name')) data['avatar'] = get_avatar(user['email']) es.index( index='entity', doc_type='user', body=data, id=user['id'], ) (database.db.table('units') .insert([{ 'id': 'plus-1', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'entity_id': 'plus', 'previous_id': None, 'language': 'en', 'name': 'Adding two number together.', 'status': 'accepted', 'available': True, 'tags': ['math'],
def es_populate(): setup_db() db_conn = make_db_connection() # Empty the database es.indices.delete(index='entity', ignore=[400, 404]) # Add users users = r.table('users').run(db_conn) for user in users: data = pick(json_prep(user), ('id', 'name')) data['avatar'] = get_avatar(user['email']) es.index( index='entity', doc_type='user', body=data, id=user['id'], ) # Add units units = (r.table('units') .filter(r.row['status'].eq('accepted')) .group('entity_id') .max('created') .default(None) .ungroup() .map(r.row['reduction']) .run(db_conn)) for unit in units: es.index( index='entity', doc_type='unit', body=json_prep(unit), id=unit['entity_id'], ) # Add cards cards = (r.table('cards') .filter(r.row['status'].eq('accepted')) .group('entity_id') .max('created') .default(None) .ungroup() .map(r.row['reduction']) .run(db_conn)) for card in cards: es.index( index='entity', doc_type='card', body=json_prep(card), id=card['entity_id'], ) # Add subjects subjects = (r.table('subjects') .filter(r.row['status'].eq('accepted')) .group('entity_id') .max('created') .default(None) .ungroup() .map(r.row['reduction']) .run(db_conn)) for subject in subjects: es.index( index='entity', doc_type='subject', body=json_prep(subject), id=subject['entity_id'], ) # Add topics topics = r.table('topics').run(db_conn) for topic in topics: es.index( index='entity', doc_type='topic', body=json_prep(topic), id=topic['id'], ) # Add posts posts = r.table('posts').run(db_conn) for post in posts: data = json_prep(post) topic = (r.table('topics') .get(data['topic_id']) .run(db_conn)) user = (r.table('users') .get(data['user_id']) .run(db_conn)) data['topic'] = json_prep(topic) data['user'] = pick(json_prep(user), ('id', 'name')) es.index( index='entity', doc_type='post', body=data, id=post['id'], ) close_db_connection(db_conn)