使用更多关键字使用Levenshetein算法进行搜索

时间:2014-05-25 16:04:41

标签: mysql search group-by having

我正在尝试编写一个简单的搜索引擎,这些是我的表格:

wordlist
-----------------
id   |   word
 1   |   telecamera
 2   |   outside

wordoccurrence
---------------------------------------
id   |   wordlist_id |  product_id
 1   |      1        |      1
 2   |      1        |      2
 3   |      2        |      1
 4   |      2        |      2


product
---------------------------------------
  id   |  description             | type
   1   |   telecamera outside     |   1
   2   |   telecamera outside     |   2 

现在,这是我糟糕的疑问:

SELECT p.* , levenshtein(w.word,  "telecamera") AS score_1, 
levenshtein(w2.word,  "outside") AS score_2, 
w.word as myword, w2.word as myword2
FROM product p, wordlist w, wordlist w2, wordoccurrence o
WHERE (o.wordlist_id = w.id or o.wordlist_id = w2.id) and w.word != w2.word
GROUP BY score_1, score_2, p.id
HAVING score_1 <6 AND score_2 < 6

目前,不要考虑levenshtein关于术语的长度。 (6是一个例子)

如果用户搜索“telecamera outside”,我希望有2行(因为有两种不同的类型)。

我想要一个AND,而不是搜索词的OR。

非常感谢你。

阿尔贝托

1 个答案:

答案 0 :(得分:0)

CREATE TABLE Table1
    (`id` int, `score_1` int, `score_2` int)
;

INSERT INTO Table1
    (`id`, `score_1`, `score_2`)
VALUES
(1, 0.000568, 0.043426),
(2, 0.000983, 0.179062)
;

这是所需的结果集。 我还希望结果集中有最低得分_1和得分_2。

我希望你的意思是这个草莓。非常感谢。