2015-04-17 49 views
0

我有,我想創建一個面搜索如何在mysql中創建分面搜索?

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的INTIAL結果和分面導航:

# 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產生面航行和作業結果DYN當用戶開始選擇多個方面時,這很有趣。例如,如果用戶點擊了小「法國」,該導航應該顯示如下:

> 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來修改作業結果和facet導航?有沒有人知道一個更優雅的方式來實現這一目標?不幸的是,由於在託管平臺上,我無法使用Solr。

任何幫助,將不勝感激。

回答

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