如何在mysql中创建分面搜索?

时间:2015-04-17 10:40:53

标签: mysql sql faceted-search

我有以下表格,我试图创建分面搜索

facets

facet_id | facet_name
--------------------
 1       | LANGUAGES
 2       | INDUSTRIES
 3       | JOB TYPES
 4       | SALARIES
 5       | LOCATIONS

job_facts (Note: this is a view)

job_id | facet_id | value
----------------------------------------
 1     | 1        | French
 1     | 2        | Sales
 1     | 3        | Permanent
 1     | 4        | 15000-20000
 1     | 5        | New York
 2     | 1        | French
 2     | 1        | Dutch
 2     | 2        | Sales
 2     | 2        | Media
 2     | 3        | Temporary
 2     | 4        | 20000-25000
 2     | 4        | 25000-30000
 2     | 5        | New York
 3     | 1        | German
 3     | 2        | Accounts
 3     | 3        | Permanent
 3     | 4        | 10000-15000
 3     | 5        | Paris
 4     | 1        | Spanish
 4     | 2        | Marketing
 4     | 3        | Permanent
 4     | 4        | 15000-20000
 4     | 5        | London

我可以使用下面的SQL生成初始结果和分面导航:

# SQL To retrieve job data: 

SELECT * FROM jobs

# SQL To construct the intial HTML faceted navigation:

SELECT t2.facet_name
        , t1.value
        , count(*) AS c
FROM job_facts t1
INNER JOIN facets t2 ON t1.facet_id = t2.facet_id
GROUP BY t2.facet_name, t1.value

html面向导航将显示如下:

> LANGUAGES
    French (2)
    German (1)
    Spanish (1)
    Dutch (1)

> INDUSTRIES
    Sales (2)
    Accounts (1)
    Media (1)
    Marketing (1)

> JOB TYPES
    permanent (3)
    temporary (1)

> SALARIES
    10000-15000 (1)
    15000-20000 (2)
    20000-25000 (1)
    25000-30000 (1)

> LOCATIONS
    New York (2)
    Paris (1)
    London (1)

我坚持如何构建SQL来生成构面导航,并在用户开始选择多个构面时动态地生成作业结果。例如,如果用户点击方面'法语',导航应显示如下:

> LANGUAGES
    French X

> INDUSTRIES
    Sales (2)
    Media (1)

> JOB TYPES
    permanent (1)
    temporary (1) 

> SALARIES
    15000-20000 (1)
    20000-25000 (1)
    25000-30000 (1)

> LOCATIONS
    New York (2)


#The results would list:

job_id | title                           | jobtype_id | location_id | post_dt    | exp_dt
---------------------------------------------------------------------------------------------
 1     | French Sales Job                | 1          | 1           |   2015-04-01 | 2015-05-01
 2     | French & Dutch Sales Media Job  | 2          | 1           | 2015-04-01 | 2015-05-01

如果用户进一步向下钻取并选择方面'临时'导航和结果应显示如下:

> LANGUAGES
   French X

> INDUSTRIES
   Sales (1)
   Media (1)

> JOB TYPES
   temporary X

> SALARIES
   20000-25000 (1)
   25000-30000 (1)

> LOCATIONS
   New York (1)

#the results would list:

job_id | title                           | jobtype_id | location_id | post_dt    | exp_dt
---------------------------------------------------------------------------------------------
 2     | French & Dutch Sales Media Job  | 2          | 1           | 2015-04-01 | 2015-05-01

修改作业结果和构面导航的SQL是什么?有没有人知道更优雅的方式来实现这一目标?不幸的是,由于在托管平台上,我无法使用Solr。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

EAV架构是代码的熊。并且它不能很好地扩展。你的情况更糟,因为每个方面都可以是多值的。

你需要做一个“自我加入”。 SELECT的主要部分与您的查询类似。然后JOIN USING(job_id)回到同一对表格,但这次有AND facet_id = 1 AND value = 'French'。像

这样的东西
SELECT  t2.facet_name , t1.value , count(*) AS c
    FROM  job_facts t1
    JOIN  facets t2 ON t1.facet_id = t2.facet_id
    JOIN  job_facts t3 ON t1.job_id = t3.job_id
    WHERE  facet_id = 1  AND  value = 'French' -- what they selected
    GROUP BY  t2.facet_name, t1.value