使用现有数据库的Flask-WhooshAlchemy

2 投票
2 回答
1940 浏览
提问于 2025-04-18 01:24

我该怎么做才能让Flask-WhooshAlchemy为一个已经存在并且有记录的数据库创建.seg文件呢?

with app.app_context():
    whooshalchemy.whoosh_index(app, MappedClass)

我可以得到.toc文件,但.seg文件只有在我通过Flask-WhooshAlchemy接口直接插入记录时才会被创建。所以,所有已经存在的记录都不会被包含在whoosh搜索中。

2 个回答

3

Flask-WhooshAlchemy这个项目好像没有人维护了。

你也可以试试我的一个分支,链接在这里:https://github.com/Revolution1/Flask-WhooshAlchemyPlus

使用起来很简单:

pip install flask-whooshalchemyplus

from flask-whooshalchemyplus import index_all

index_all(app)

我还添加了一些新功能,并修复了很多bug。

谢谢!:)

2

这里有一个脚本,用来给一个已经存在的数据库建立索引。顺便提一下,Whoosh把这个过程称为“批量索引”。

这个脚本有点粗糙,但能正常工作:

#!/usr/bin/env python2

import os
import sys
import app
from models import YourModel as Model
from flask.ext.whooshalchemy import whoosh_index

sys.stdout  = os.fdopen(sys.stdout.fileno(), 'w', 0)
atatime     = 512

with app.app_context():
    index       = whoosh_index(app, Model)
    searchable  = Model.__searchable__
    print 'counting rows...'
    total       = int(Model.query.order_by(None).count())
    done        = 0
    print 'total rows: {}'.format(total)
    writer = index.writer(limitmb=10000, procs=16, multisegment=True)
    for p in Model.query.yield_per( atatime ):
        record = dict([(s, p.__dict__[s]) for s in searchable])
        record.update({'id' : unicode(p.id)}) # id is mandatory, or whoosh won't work
        writer.add_document(**record)
        done += 1
        if done % atatime == 0:
            print 'c {}/{} ({}%)'.format(done, total, round((float(done)/total)*100,2) ),

    print '{}/{} ({}%)'.format(done, total, round((float(done)/total)*100,2) )
    writer.commit()

你可能想调整一下参数:

  • atatime - 一次从数据库中提取的记录数量
  • limitmb - 使用的最大兆字节数
  • procs - 并行使用的核心数

我用这个脚本在一个8核的AWS实例上索引了大约360,000条记录。整个过程大约花了4分钟,其中大部分时间是在等待单线程的commit()操作。

撰写回答