FTS3 中的多列匹配

发布于 2024-12-14 00:55:13 字数 575 浏览 1 评论 0原文

我有一个包含三列的简单数据库:标题、数据、信息。我将它们存储在 SQLite FTS3 数据库中,以便我可以轻松执行全文搜索。我的应用程序基于 Android 的可搜索词典示例。我不知道该怎么做的一件事是如何跨两列搜索(匹配)。我基本上想接受查询并在标题和数据列中搜索它。以下是准备查询的方法:

public Cursor searchMatches(String query, String[] columns) {
    String selection = KEY_TITLE + " MATCH ?";
    String[] selectionArguements = new String[] {query + "*"};
    return query(selection, selectionArguements, columns);
}

如果我不遵循精确的格式 KEY_TITLE + " MATCH ?" 进行选择,则其他任何方法都不起作用,尽管 FTS3 文档说其他方法可以工作。如何在 Android 中设置此选择以跨两列进行搜索?我尝试添加 OR 但只是出现 SQL 逻辑错误。多谢!

I have a simple database of three columns: title, data, info. I'm storing them in a SQLite FTS3 database so I can easily perform full-text searches. I've been basing my app off the Searchable Dictionary example from Android. The one thing I can't figure out how to do is how to search (match) across two columns. I basically want to take the query and search it in both the title and data columns. The following is the method that prepares the query:

public Cursor searchMatches(String query, String[] columns) {
    String selection = KEY_TITLE + " MATCH ?";
    String[] selectionArguements = new String[] {query + "*"};
    return query(selection, selectionArguements, columns);
}

If I do not follow that exact format KEY_TITLE + " MATCH ?" for the selection, nothing else will work despite the fact that the FTS3 document says other ways can work. How can set up this selection in Android to search across two columns? I've tried adding OR but I simply get a SQL logic error. Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒中人 2024-12-21 00:55:13

好吧,我找到了一个可行的解决方案,尽管它不是最漂亮的。它涉及使用 UNION 简单地组合两个 MATCH 查询。

    String wordQuery = builder.buildQuery(null, KEY_WORD + " MATCH '" + query + "*'", null, null, null, null, null);
    String definitionQuery = builder.buildQuery(null, KEY_DEFINITION + " MATCH '" + query + "*'", null, null, null, null, null);
    String unionQuery = builder.buildUnionQuery(new String[] {wordQuery, definitionQuery}, null, null);
    Cursor result = helper.getReadableDatabase().rawQuery(unionQuery, null);

Alright, I found a workable solution although it isn't the prettiest. It involves using UNION to simply combine two MATCH queries.

    String wordQuery = builder.buildQuery(null, KEY_WORD + " MATCH '" + query + "*'", null, null, null, null, null);
    String definitionQuery = builder.buildQuery(null, KEY_DEFINITION + " MATCH '" + query + "*'", null, null, null, null, null);
    String unionQuery = builder.buildUnionQuery(new String[] {wordQuery, definitionQuery}, null, null);
    Cursor result = helper.getReadableDatabase().rawQuery(unionQuery, null);
乱世争霸 2024-12-21 00:55:13

您将必须编写自己的查询函数。您将希望您的查询看起来像这样:

 select * from table_name where title equals ? or info equals ?

您不能简单地将 or 添加到语句的 equals 部分(一个常见错误)。

You're going to have to write your own query function. You'll want your query to look something like:

 select * from table_name where title equals ? or info equals ?

You can't simply add an or to the equals part of the statement (a common mistake).

人疚 2024-12-21 00:55:13

我就是这样做的

select * from table_name where table_name match 'YOURSEARCHSTRING'

This is how I do it

select * from table_name where table_name match 'YOURSEARCHSTRING'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文