def put_info_into_es(): root = os.path.dirname(os.path.abspath(__file__)) config_path = os.path.join(root, 'config', 'database.yaml') yaml = pyaml.yaml.safe_load(open(config_path)) config = yaml[yaml['stage']] client = MongoClient(config['mongo']['host']) blogtb = client['simpleblog']['simpleblog'] es = ES.connect_host() for blog in blogtb.find(): # print blog['title'], keep_only_words(blog['content']) try: data = { 'id': unicode(blog['_id']), 'content': keep_only_words(blog['content']), 'author': unicode(blog['author']), 'title': blog['title'], 'create_time': format_datetime(blog['create_time'], '%Y-%m-%d'), # 'create_time': format_datetime(blog['create_time'], '%Y/%m/%d %H:%M:%S'), 'update_time': format_datetime(blog['update_time'], '%Y-%m-%d'), # 'update_time': format_datetime(blog['update_time'], '%Y/%m/%d %H:%M:%S'), 'visible': blog['visible'], } except Exception, e: print blog['title'] print e continue # print format_datetime(blog['create_time'], '%Y/%m/%d %H:%M:%S') es.index('simpleblog', 'blogpost', body=json.dumps(data), id=unicode(blog['_id']))
def post_save(cls, sender, document, **kwargs): data = { 'id': unicode(document.id), 'content': keep_only_words(document.content), 'author': unicode(document.author), 'title': document.title, 'create_time': format_datetime(document.create_time, '%Y-%m-%d'), 'update_time': format_datetime(document.update_time, '%Y-%m-%d'), 'visible': document.visible, 'tags': document.tags } try: es = ES.connect_host() es.index('simpleblog', 'blogpost', body=json.dumps(data), id=unicode(document.id)) except Exception, e: print 'es connect failed, %s' % e