【问题标题】:MySQL Search (Sort by Relevance)MySQL 搜索(按相关性排序)
【发布时间】:2011-01-26 08:18:13
【问题描述】:

谁能帮我按照以下标准按相关性对行进行排序?

`tbluser`
- - - - - - -
First Name
Last Name

`tbleduc`
- - - - - - -
School
College
University

在搜索表单上,用户有以下字段

Name
School
College
University

学校学院和大学是可选的..

并将Name分成2个单词(中间其他单词省略),第一个单词为第一个anme,最后一个单词为last name..

现在我想实现基于相关性的搜索。

感谢您的帮助:)

【问题讨论】:

    标签: mysql search join relevance


    【解决方案1】:

    您是否尝试过对您的表执行匹配 - 反对查询。结果集按相关性 y 默认排序。此外,它还可以进行全文搜索。

    【讨论】:

      【解决方案2】:

      您是否有时间和资源尝试实施Solr?它对于相关性排序的搜索要好得多,而且非常容易上手。

      【讨论】:

        【解决方案3】:

        好的,我在 Postgres 上为您尝试过这个(我这里没有 MySQL,所以可能有点不同):

        select matches.id,
              (matches.tfirstname
               + matches.tlastname 
               + matches.tschool
               + matches.tcollege
               + matches.tuniversity) as total
        from (
           select u.id,
                  (case when u.firstname like '%a%' then 1 else 0 end) as tfirstname,
                  (case when u.lastname like '%b%' then 1 else 0 end) as tlastname,
                  sum(e2.nschool) as tschool,
                  sum(e2.ncollege) as tcollege,
                  sum(e2.nuniversity) as tuniversity
           from tbluser u left outer join (
              select e.usr,
                     (case when e.school like '%c%' then 1 else 0 end) as nschool,
                     (case when e.college like '%d%' then 1 else 0 end) as ncollege,
                     (case when e.university like '%e%' then 1 else 0 end) as nuniversity
              from tbleduc e
           ) e2 on u.id=e2.usr
           group by u.id, u.firstname, u.lastname
        ) as matches
        

        我使用这些 DDL 语句来创建表:

        create table tbluser (
          id int primary key,
          firstname varchar(255),
          lastname varchar(255)
        )
        
        
        create table tbleduc (
          id int primary key,
          usr int references tbluser,
          school varchar(255),
          college varchar(255),
          university varchar(255)
        )
        

        还有一点示例数据:

        insert into tbluser(id, firstname, lastname)
                    values (1, 'Jason', 'Bourne');
        insert into tbleduc(id, usr, school, college, university)
                    values (1, 1, 'SomeSchool', 'SomeCollege', 'SomeUniversity');
        insert into tbleduc(id, usr, school, college, university)
                    values (2, 1, 'MoreSchool', 'MoreCollege', 'MoreUniversity');
        

        如果tblusertbleduc 的关系是1:1,查询可以简化一点。

        不要忘记将%a%%b、...替换为您的变量(我建议使用准备好的语句)。

        我希望这个模板作为一个基本解决方案有所帮助 - 您可以随意调整它:-) 您还可以删除最外面的 select 语句,以获取各个结果的计数器。

        【讨论】:

          【解决方案4】:

          第 1 步:定义计算“相关性”的方法。

          第 2 步:编写一个查询,该查询使用第 1 步的计算来确定其结果的顺序。

          很遗憾,您没有说明是什么让一条记录比另一条记录“更相关”或“更不相关”,所以我们现在可以告诉你的就这么多了。

          【讨论】:

          • 相关性首先通过更多匹配的词来计算。例如带有nameschoolcollege 的搜索词应该出现在与nameschool 匹配的字段的顶部
          猜你喜欢
          • 2010-09-25
          • 2012-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-26
          • 2020-03-28
          相关资源
          最近更新 更多