简体   繁体   English

多词搜索查询

[英]Multiple term search query

I have a crawler which scans all the words in a web page. 我有一个搜寻器,可以扫描网页中的所有单词。 it then inserts each word into a mysql database along with what url it belongs in. The search is then ranked by the amount of words found in the document. 然后,它将每个单词及其所属的URL插入到mysql数据库中。然后,根据文档中找到的单词数量对搜索进行排名。 The problem is...how do i add multiple term query into my existing query. 问题是...如何将多个术语查询添加到现有查询中。

It is perfect for single term querying, but i want my query to try and find words together in the same web page, and if neither word appears in the web page, return results for the terms as normal. 它非常适合单项查询,但是我希望我的查询尝试在同一网页中一起查找单词,并且如果网页中都没有单词出现,则可以正常返回这些单词的结果。

My query is below: 我的查询如下:

         $results = addslashes( $_POST['results'] );

               " SELECT p.page_url AS url,
                       COUNT(*) AS occurrences 
                       FROM page p, word w, occurrence o
                       WHERE p.page_id = o.page_id AND
                       w.word_id = o.word_id AND
                       w.word_word = \"$keyword\"
                       GROUP BY p.page_id
                       ORDER BY occurrences DESC
                       LIMIT $results"

Use COUNT(DISTINCT ...) to count the number of different words found on each page, and use IN to find any of a list of words: 使用COUNT(DISTINCT ...)来计算在每个页面上找到的不同单词的数量,并使用IN查找任何单词列表:

SELECT
    p.page_url AS url,
    COUNT(DISTINCT w.word_word) AS words_found
    COUNT(*) AS occurrences 
FROM page p
JOIN occurrence o ON p.page_id = o.page_id
JOIN word w ON w.word_id = o.word_id
WHERE w.word_word IN ('foo', 'bar')
GROUP BY p.page_id
ORDER BY occurrences DESC

If you want to ensure that at least n of the search terms are on the page then add a HAVING clause: 如果要确保页面上至少有n个搜索字词,请添加HAVING子句:

GROUP BY p.page_id
HAVING COUNT(DISTINCT w.word_word) >= 2
ORDER BY occurrences DESC

You can do sub-selects if the DB engine supports it. 如果数据库引擎支持,则可以进行子选择。 Example: 例:

SELECT 
  url, 
  (select count(*) from table where conditions1) as count1, 
  (select count(*) from table where conditions2) as count2 
 FROM table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM