Exemple #1
0
def search(keywords, start, limit):
    """搜索接口

    Argumenst:
      keywords: 关键词语
      start: 开始位置
      limit: 返回个数

    Returns:
      tuple: (total_count, results)
    """
    fts_searcher = AliFTSSearcher(config.ALI_SEARCH_HOST, config.ALI_SEARCH_APP,
                                  config.ACCESS_KEY, config.ACCESS_SECRET)

    db = DailyDao(config.DB_HOST, config.DB_PORT, config.DB_USER,
                  config.DB_PASS, config.DB_NAME)
    try:
        hits = []
        results = fts_searcher.search(keywords, start=start, limit=limit)

        for hit in results:
            news = db.get_news(hit['news_id'])
            title = hit['title']
            summary = hit['content']
            hits.append(dict(
                image_public_url=news[8],
                share_url=news[3],
                date=news[4],
                title=title,
                summary=summary,
            ))

        return results.total_count, hits
    finally:
        db.close()
        fts_searcher.close()