V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
honmaple
V2EX  ›  Python

一个简单的 flask 全文搜索插件

  •  
  •   honmaple · 2017-04-16 18:17:55 +08:00 · 2539 次点击
    这是一个创建于 2577 天前的主题,其中的信息可能已经有所发展或是发生改变。

    flask 貌似很少全文搜索的插件,有一个Flask-WhooshAlchemy,但试了几次都用不了,所以参考 Flask-WhooshAlchemy 自己写了一个

    插件基于 whoosh,纯 python 编写,使用上很简单

    from flask_msearch import Search
    [...]
    search = Search()
    search.init_app(app)
    
    # models.py
    class Post(db.Model):
        __tablename__ = 'post'
        __searchable__ = ['title', 'content']
    
    # views.py
    @app.route("/search")
    def w_search():
        keyword = request.args.get('keyword')
        results = search.whoosh_search(Post,query=keyword,fields=['title'],limit=20)
        return ''
    

    如果要对已存在的数据创建索引

    search.create_index()
    

    自定义 analyzer

    from jieba.analyse import ChineseAnalyzer
    search = Search(analyzer=ChineseAnalyzer())
    

    项目地址:https://github.com/honmaple/flask-msearch

    可以查看演示:demo

    (还有更多 whoosh 的功能还没加上)

    10 条回复    2017-04-17 11:22:07 +08:00
    pathbox
        1
    pathbox  
       2017-04-16 19:32:47 +08:00
    简单的项目 可以用用。 其他 还是上 ES 吧
    clino
        2
    clino  
       2017-04-16 20:06:42 +08:00 via Android
    然而搜标题美国并没有结果
    awanabe
        3
    awanabe  
       2017-04-16 20:14:11 +08:00
    分词 建议用 jieba
    ChineseAnalyzer 很弱
    awanabe
        4
    awanabe  
       2017-04-16 20:14:36 +08:00
    @clino 分词引擎没有选好
    honmaple
        5
    honmaple  
    OP
       2017-04-16 21:27:26 +08:00
    @pathbox 嗯, ES 需要 java 环境,纯 python 的就 whoosh,简单方便
    honmaple
        6
    honmaple  
    OP
       2017-04-16 21:30:49 +08:00
    @clino @awanabe 是的,demo 上我使用默认的 StemmingAnalyzer 作为分词引擎,并没有使用 ChineseAnalyzer,后来我在本地上试过了,如果使用 ChineseAnalyzer 创建索引后是可以搜索到的
    honmaple
        7
    honmaple  
    OP
       2017-04-16 21:36:57 +08:00
    @awanabe ChineseAnalyzer 就是使用 jieba 实现的啊,不过使用 jieba 检索速度会有所下降
    clino
        8
    clino  
       2017-04-17 10:29:58 +08:00
    @honmaple 用 jieba 检索速度会下降是什么意思?
    如果说做索引的速度会下降我还比较理解
    honmaple
        9
    honmaple  
    OP
       2017-04-17 11:11:34 +08:00
    @clino 因为 jieba 是在每次使用时加载,而不是保存到内存中,使用时可以看出有明显的停顿

    ```
    Building prefix dict from the default dictionary ...
    Loading model from cache /tmp/jieba.cache
    Loading model cost 1.450 seconds.
    Prefix dict has been built succesfully.
    ```
    honmaple
        10
    honmaple  
    OP
       2017-04-17 11:22:07 +08:00
    不好意思,我刚才再次验证了一下, jieba 只在第一次使用时会从 cache 中加载,之后就保存到内存中了,使用 jieba 对检索速度没有太大影响
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4811 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:37 · PVG 13:37 · LAX 22:37 · JFK 01:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.