2

我有一个使用 fts5 创建的虚拟表:

import sqlite3
# create a db in memory
con = sqlite3.connect(':memory:')
con.execute('create virtual table operators using fts5(family, operator, label, summary, tokenize=porter)')

# some sample data
samples = {'insideTOP':
              {'label':'Inside',
               'family':'TOP',
               'summary':'The Inside TOP places Input1 inside Input2.'
              },
           'inTOP':
              {'label':'In',
               'family':'TOP',
               'summary':'The In TOP is used to create a TOP input.'
              },
           'fileinSOP':
              {'label':'File In',
               'family':'SOP',
               'summary':'The File In SOP allows you to read a file'
              }
          }

# fill db with those values
for operator in samples.keys():
    opDescr = samples[operator]
    con.executescript("insert into operators (family, operator, label, summary) values ('{0}','{1}','{2}','{3}');".format(opDescr['family'],operator,opDescr['label'],opDescr['summary']))

以下列

+--------+-----------+------------+----------------------------------------------+
| family | operator  |   label    |            summary                           |
+--------+-----------+------------+----------------------------------------------+
| TOP    | insideTOP | Inside     | The Inside TOP places Input1 inside Input2.|
| TOP    | inTOP     | In         | The In TOP is used to create a TOP input.    |
| SOP    | fileinSOP | File In    | The File In SOP allows you to read a file    |
+--------+-----------+------------+----------------------------------------------+

一个示例查询是:

# query the db
query = "select operator from operators where operators match 'operator:In*' or operators match 'label:In*' order by family, bm25(operators)"
result = con.execute(query)

for row in result:
    print(row)

结果我得到

  • 文件SOP
  • 里面顶部
  • 在顶部

不过,对于这种特殊情况,我实际上希望“inTOP”出现在“insideTOP”之前,因为标签是完美匹配的。

什么是能够以我想要的方式按摩这些结果的好技术?

非常感谢

马库斯

4

1 回答 1

0

也许您可以将您的订单规则放在问题中。

如果您使用 bm25 排序结果,则无法达到您想要的结果我建议您可以使用自定义排名函数,如下面的 sql:

query = "select operator from operators where operators match 'operator:In*' or operators match 'label:In*' order by myrank(family, operators)"

在 fts5 中定义自定义排名功能非常简单,您可以按照 fts5 网站中的指南进行操作。

如果你还想把bm25的结果作为排名分数,你可以在rank方法中得到分数可以计算你的最终分数。

于 2019-03-20T03:51:53.643 回答