1

给定下表:

create table index(name text, docid int);
create virtual table docs using fts4();

以下查询在查询单个令牌时按预期工作(例如:marchbad):

select name from index where docid in (select docid from docs where docs match ?)

但是我如何查询多个令牌(例如,bad bed)?直接绑定字符串bad bed不起作用(总是不选择任何内容),既不能用双引号括住占位符或字符串,也不能单独使用AND每个MATCH标记(最后一个会引发错误)。

4

3 回答 3

0

Usingintersect确实有效,但在搜索许多令牌时它很笨重且效率低下:

select name from index where docid in (
    select docid from docs where docs match ?
    intersect
    select docid from docs where docs match ?
    intersect
    ...
)

每个?都与一个令牌配对。

于 2012-07-03T20:54:14.377 回答
0

您可以||在 sqlite 中使用连接运算符。''将是空字符串

SELECT * FROM table WHERE docs MATCH '' || ? || ' ' || ? || ' ' || ? || ''

Make sure there is a space between every token or an ' AND '.

Update

Actually it doesn't work. It seems there are tokenator issues with this approach. Its better to concatenate all the tokens with the space and bind the resulting string with a single '?'

于 2014-01-08T16:04:38.063 回答
0

There are operators within the match syntax in FTS so you can use AND, OR and NOT.

See here for documentation

e.g.

-- Return the docid values associated with all documents that contain the
-- two terms "sqlite" and "database", and/or contain the term "library".
SELECT docid FROM docs WHERE docs MATCH 'sqlite AND database OR library';
于 2015-03-10T18:54:45.213 回答