我有一个名为categories
columns的 SQLite FTS3 虚拟表category, id, header, content
。我想搜索它,并让结果同时包含列数据和snippet()
结果。
最终期望的行为是显示搜索词的每个匹配项的类别和 ID,然后如果在内容列中找到搜索词,则还显示每个片段。像这样:
Search term "virus":
Category: Coughing
ID: Coughing Symptoms
Snippet 1: "the common cold, which is spread by the cold <b>virus</b> blah blah etc"
Snippet 2: "lorem ipsum some other things <b>virus</b> and then some other stuff"
Category: Coughing
ID: Coughing treatment
Snippet 1: "...coughing can be treated by managing the symptoms. If a <b>virus</b> blah etc"
Category: Headache
ID: Headache Symptoms
Snippet: "I think you get the idea now <b>virus</b> more things"
我现在可以将搜索行结果和片段作为单独的函数获取。这将搜索表格并正确打印出 row.elements:
function SearchValueInDB() {
db.transaction(function(transaction) {
var search = $('#txSearch').val(),
query = "SELECT * FROM guidelines WHERE guidelines MATCH '" + search + "*';";
transaction.executeSql(query,[], function(transaction, result) {
if (result != null && result.rows != null) {
if (result.rows.length == 0) {
//no results message
} else {
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
$('#lbResult').append('<br/> Search result:' + row.category + ' ' + row.id + ' ' + row.header + '<br/>');
}
}
}
},nullHandler,errorHandler);
});
}
这将搜索content
列中的片段,并打印出它们的列表:
function SearchForSnippet() {
db.transaction(function(transaction) {
var search = $('#txSearch').val(),
query = "SELECT snippet(guidelines) FROM guidelines WHERE content MATCH '" + search + "*';";
transaction.executeSql(query,[], function(transaction, result) {
if (result != null && result.rows != null) {
$('#lbResult').html('');
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
for(var key in row) {
var value = row[key];
$('#lbResult').append('<br/> ' + i + value );
}
}
}
},nullHandler,errorHandler);
});
}
到目前为止,我可以想象两种可能的方法:要么我可以以某种方式组合 SELECT 查询——尽管我找不到任何使用片段()函数的 JOINing 查询示例。或者,我可以创建一个新函数findSnippet()
,并在每次遍历result.rows
数组后调用它。这些方法中的任何一种都可能奏效,还是有更好的方法来处理这个问题?