A limitation of Sqlite3's full text search doesn't allow ORs with MATCHes. Workaround?

那年仲夏 提交于 2020-01-04 13:37:16

问题


Sqlite3's full text search facility - FTS3 - allows to use MATCH operator for fast full-text search:

SELECT ItemId FROM docs WHERE docs.text MATCH 'linux'

However, it does not support OR operator anywhere in an SQL query where there's a MATCH (source: 1, 2):

SELECT ItemId FROM docs WHERE docs.text MATCH 'linux' OR column=value
error: unable to use function MATCH in the requested context

(Not to be confused with OR operator in the FTS3 query itself, i.e. SELECT ItemId FROM docs WHERE docs.text MATCH 'linux OR unix'. This works fine.)

Is there a way to rewrite the query so that it works (even if it's somewhat slower)?


回答1:


Rewriting the query using temporary views will work as expected:

CREATE TEMP VIEW view1 AS SELECT ItemId FROM docs WHERE docs.text MATCH 'linux' 
SELECT * FROM docs WHERE ItemId IN view1 OR column=value
DROP VIEW view1

The speed will be comparable to that of a "direct" query (without the temporary view) if the temporary view is not "sweeping", i.e. it does not generate a lot of rows.



来源:https://stackoverflow.com/questions/11764357/a-limitation-of-sqlite3s-full-text-search-doesnt-allow-ors-with-matches-worka

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!