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

StackOverflow https://stackoverflow.com/questions/11764357

  •  24-06-2021
  •  | 
  •  

Domanda

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)?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top