1

任何人都可以帮助我如何按以下标准的相关性对行进行排序?

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

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

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

Name
School
College
University

在哪里学校学院和大学是可选的..

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

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

谢谢您的帮助 :)

4

4 回答 4

5

好的,我在 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');

tbluser如果和之间的关系tbleduc是1:1 ,则查询可以简化一点。

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

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

于 2010-03-21T20:02:31.120 回答
1

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

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

不幸的是,您没有说明是什么让一个记录比另一个记录“更相关”或“更不相关”,所以这就是我们目前可以告诉你的。

于 2010-03-11T12:25:14.250 回答
1

您是否在您的桌子上尝试过 Match - Against 查询。结果集按相关性 y 默认排序。此外,它还可以进行全文搜索。

于 2010-03-24T06:28:57.160 回答
0

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

于 2010-03-23T20:57:09.770 回答