【问题标题】:Pewee index search with SQLite使用 SQLite 进行 Pewee 索引搜索
【发布时间】:2017-06-11 19:09:54
【问题描述】:

我正在使用以下查询来使用 SQLite 执行文本搜索。

docs = DocumentIndex.search(
    'search term',
    weights={'title': 2.0, 'content': 1.0},
    with_score=True,
    score_alias='search_score')

for result in docs:
    print result.title, result.search_score

结果是DocumentIndex 的排名列表。如何将此结果与 Document 表连接起来,以便返回 Document 列表并保留原始排名顺序?

我尝试了以下方法:

return Document.select().where(docs)

但是说only a single result allowed是行不通的

【问题讨论】:

  • 你看到我的回答了吗?如果它对您有用,请说明为什么或为什么不?

标签: python sqlite peewee


【解决方案1】:

我更喜欢处理这种事情的方式是确保全文搜索表的 docid 与正在搜索的任何内容的主键相同。

所以我有:

class Document(Model):
    content = TextField()

class DocumentIndex(FTSModel):
    docid = DocIDField()
    content = TextField()

    class Meta:
        extension_options = {'tokenize': 'porter'}

document = Document.create(content='something')
DocumentIndex.create(content=document.content, docid=document.id)

然后在搜索时,我会这样做:

query = (Document
         .select(Document, DocumentIndex.bm25().alias('score'))
         .join(DocumentIndex, on=(Document.id == DocumentIndex.docid))
         .where(DocumentIndex.match(search_term))
         .order_by(SQL('score'))

注意:FTS3 和 FTS5 使用“rowid”而不是“docid”。

【讨论】:

    猜你喜欢
    • 2012-03-09
    • 1970-01-01
    • 2014-11-29
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多