文本列中的mysql搜索词按#world matched

时间:2017-09-12 12:32:26

标签: mysql sql string

问题:

文字输入为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%

但我希望在特定的ORDER中显示结果,

时结果更相关
  • 更多单词出现更多相关结果应该是, 所以“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

但我需要其他订单规则:

  • 如果记录以搜索词开头,则更为相关。“ALPHA和......”
  • 如果记录中的单词以搜索的单词开头是更相关的es。“什么是ALPHAness”
  • 在记录中查找单词。“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

2 个答案:

答案 0 :(得分:0)

看起来您需要一个存储函数来计算订购中使用的权重。

E.g。最初的体重是0。

如果在字段权重+ = 1000

中找到单词

如果单词是从记录重量的开头+ = 100

如果单词来自单词weight + = 10

的开头

重量+ =(单词的数量 - 单词索引)单词的顺序

所以传递搜索“alpha bravo”返回

1000+10+1 + 1000+10 DELTA and ALPHA and BRAVO (contain both words so is the first)
1000+100+1 ALPHA and DELTA (begin with the first word searched)
1000+100 BRAVO (begin with the second word searched)
1000+10+1 what is ALPHAness (has the first word searched as begin of a word)
1000+10 CHARLIE BRAVO (has the second word searched as begin of a word)
1000 bbbBRAVOooo charlieeee (has the second word searched inside)

答案 1 :(得分:0)

我找到了这个解决方案,并不优雅..但它的作品

 select  *
,      
(
  (10*(col1 like 'alpha%'))+
  (8*(col1 like '% alpha%'))+
  (3*(col1 like '%alpha%' and col1 not like "% alpha%" and col1 not like 'alpha%'))+
   (9*(col1 like 'bravo%'))+
  (7*(col1 like '% bravo%'))+
  (3*(col1 like '%bravo%' and col1 not like "% bravo%" and col1 not like 'bravo%'))
 ) as score
from    YourTable where col1 like '%alpha%' or col1 like '%bravo%'
order by
        score desc

http://sqlfiddle.com/#!9/91971/4