ends with (suffix) and contains string search usin

2020-02-26 01:24发布

I am using SQLite FTS extension in my iOS application. It performs well but the problem is that it matches only string prefixes (or starts with keyword search).

i.e.

SELECT FROM tablename WHERE columnname MATCH 'searchterm*'

works but

SELECT FROM tablename WHERE columnname MATCH '*searchterm'

or

SELECT FROM tablename WHERE columnname MATCH '*searchterm*'

does not.

Is there any workaround for this or any way to use FTS to build a query similar to "LIKE '%searchterm%'" query.

EDIT:

As pointed out by Retterdesdialogs, storing the entire text in reverse order and running a prefix search on reverse string is a possible solution for ends with/suffix search problem, which was my original question, but it wont work for 'contains' search. I have updated the question accordingly.

3条回答
爷的心禁止访问
2楼-- · 2020-02-26 01:49

The workaround is to store the reverse string in an extra column. See this link (its not exactly the same it should give a idea):

Search Suffix using Full Text Search

查看更多
爷的心禁止访问
3楼-- · 2020-02-26 01:53

In my iOS and Android applications, I have shied away from FTS search for exactly the reason that it doesn't support substring matches due to lack of suffix queries.

The workarounds seem complicated.

I have resorted to using LIKE queries, which while being less performant than MATCH, served my needs.

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-26 01:56

To get it to work for contains queries, you need to store all suffixes of the terms you want to be able to search. This has the downside of making the database really large, but that can be avoided by compressing the data.

SQLite FTS contains and suffix matches

查看更多
登录 后发表回答