【发布时间】:2018-02-20 22:08:47
【问题描述】:
问题:
文本输入将是 3 或 4 个单词, 我想显示至少包含这些词之一的字段。
例如,如果单词是“alpha bravo charlie delta”,我希望允许结果
CHARLIE BRAVO
my name is CHARLIE
what is ALPHAness
ALPHA and DELTA
adDELTAs
BRAVO
DELTA and ALPHA and BRAVO
bbbBRAVOooo CHARLIEeeee
到这里为止没问题,我使用查询:
select * from subject where name like '%alpha%'
or name like '%bravo%' or name like '%charlie%'
or name like '%delta%
但我想以特定的顺序显示结果, 当
- 更多的词出现更相关的结果应该是, 所以“CHARLIE BRAVO”出现在“BRAVO”之前
我找到了解决方案
select *
, (
(char_length(col1) - char_length(replace(col1,'alpha','')))
/ char_length('alpha')
+
(char_length(col1) - char_length(replace(col1,'bravo','')))
/ char_length('bravo')
+
(char_length(col1) - char_length(replace(col1,'delta','')))
/ char_length('delta')
+
(char_length(col1) - char_length(replace(col1,'charlie','')))
/ char_length('charlie')
) as Occurances
from YourTable
order by
Occurances desc
但我需要其他订单规则:
- 如果记录以搜索词开头更相关 es."ALPHA and..."
- 如果记录中以搜索词开头的词更相关 es.“什么是 ALPHAness”
- 在记录 es."adDELTAs" 中搜索到的单词
我也找到了解决这些订单问题的方法,但是, 如何将两者结合起来?
select id, name
from subjects
where name like '%alpha%'
order by
name like 'alpha%' desc,
ifnull(nullif(instr(name, ' alpha'), 0), 99999),
ifnull(nullif(instr(name, 'alpha'), 0), 99999),
name;
因此,如果我搜索“alpha bravo”,结果应该是:
DELTA and ALPHA and BRAVO (contain both words so is the first)
ALPHA and DELTA (begin with the first word searched)
BRAVO (begin with the second word searched)
what is ALPHAness (has the first word searched as begin of a word)
CHARLIE BRAVO (has the second word searched as begin of a word)
bbbBRAVOooo charlieeee (has the second word searched inside)
PS 我需要不区分大小写并且不区分重音字母 òàùèìé 所以 è = e
【问题讨论】: