コード例 #1
0
ファイル: home.py プロジェクト: zhaofl2015/blogforfun
def get_tags():
    res_list = list()
    for item in BlogTag.objects(delete_time=None).order_by('-count'):
        res_list.append(item.as_dict())

    # 更新实际使用的tag的数量,之前的数量已经不准确
    es = ES.connect_host()
    ret = es.search('simpleblog', 'blogpost', {
        'aggs': {
            'all_tags': {
                'terms': {
                    'field': 'tags',
                    'size': 0
                }
            }
        }
    }, size=1000)

    name_count = dict([(b['key'], b['doc_count']) for b in ret['aggregations']['all_tags']['buckets']])

    for item in res_list:
        if item['name'] in name_count:
            item['count'] = name_count.get(item['name'])
            item['name_count'] = '%s (%d)' % (item['name'], item['count'])
        else:
            item['count'] = 0
            item['name_count'] = '%s (%d)' % (item['name'], 0)

    return res_list
コード例 #2
0
ファイル: blog_model.py プロジェクト: zhaofl2015/blogforfun
    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