【问题标题】:mysql search words in text column sorted by # world matchedmysql 在文本列中搜索单词,按世界匹配排序
【发布时间】: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

【问题讨论】:

    标签: mysql sql string


    【解决方案1】:

    我发现这个解决方案并不优雅.. 但它的作品

     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

    【讨论】:

      【解决方案2】:

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

      例如初始权重为 0。

      如果在字段 weight+=1000 中找到单词

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

      如果单词是从单词的开头 weight+=10

      weight+=(词数-词索引)词的顺序

      所以通过搜索“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)
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 2021-09-12
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多