| id | caption | offset |
|----|-----------------|--------|
| 1 | The quick fox | 0 |
| 2 | jumped over the | 14 |
| 3 | lazy fox. The | 30 |
| 4 | cow jumped over | 44 |
| 5 | the moon | 52 |
上表包含与我要搜索的文本块相关的数据。
我正在使用弹性查询的查询返回整个文本块的偏移量 串联在一起,例如:
{
searchQuery: 'over the lazy fox.',
beginning: 21,
end: 34
}
必须通过偏移量检查开始和结束;介于两者之间的任何东西 返回数据中应包含两个数字。为了使事情更棘手,我还希望文本的匹配部分。重要的是,每行都必须分开,因为这里省略了多余的行信息。
| id | caption | highlight |
|----|-----------------|-----------|
| 2 | jumped over the | over the |
| 3 | lazy fox. The | lazy fox. |
我还希望能够一次突出显示多个搜索,如果提供了一系列起点和终点,则返回所有匹配的行和突出显示。
答案 0 :(得分:0)
对于一次搜索的情况,
SELECT
*
,IF(@beginning >= offset
,RIGHT(caption,len(caption)-(@beginning-offset))
,LEFT(caption,@end-offset)) as highlight
from @table
WHERE offset >= (select MAX(offset) from @table where offset <= @beginning)
and offset <= @end
对于数组搜索方案,
SELECT
t.*
,IIF(_beginning >= offset
,RIGHT(caption,len(caption)-(_beginning-offset))
,LEFT(caption,_end-offset)) as highlight
,a.id
from @table t
join @array a ON offset >= (select MAX(offset) from @table where offset <= _beginning)
and offset <= _end
order by a.id asc
在运行第二个查询之前,将要搜索的值插入下表(变量)中,
declare @array as table(id int, _beginning int, _end int)
答案 1 :(得分:0)
对于单个搜索查询来说就是这样。
django-private-chat
也许可以使用UNION ALL将多个搜索组合到一个结果集中。
NB 我确实用我想的偏移量修改了您的示例,并将SELECT
id,
caption,
LEFT(RIGHT(caption, offset + LENGTH(caption) - @beginning), @end - offset) AS highlight
FROM some_table
WHERE offset + LENGTH(caption) >= @beginning
AND offset <= @end;
设置为38,以获得预期的结果集。
@end