問題描述
我知道如何進行常規的 php mysql 搜索并顯示結果.但是,由于我要完成的工作的性質,我需要能夠按相關性進行排序.讓我更好地解釋一下:
I know how to do a regular php mysql search and display the results. However, because of the nature of what I'm trying to accomplish I need to be able to sort by relevancy. Let me explain this better:
普通查詢apple iphone applications"將使用 %apple iphone application% 搜索數據庫,但如果沒有以該確切順序顯示該短語的記錄,則搜索將不會產生任何結果.
Normal Query "apple iphone applications" will search the database using %apple iphone application%, but if there aren't records which display that phrase in that exact order the search will produce nothing.
我基本上需要做的是分別搜索 'apple'、'iphone' 和 'applications',然后將結果合并為一個,然后我需要根據找到的單詞實例的數量來對相關性進行評分在記錄中.例如,如果我做了我想做的事情并且它返回了以下內容:
What I basically need to do is search for 'apple', 'iphone' and 'applications' all separately and then merge the results into one, and then I need to grade the relevancy by how many instances of the word are found in the records. For example if I did what I wanted to do and it returned them following:
Iphone Applications From Apple
Apple Make The Best Apple Iphone Applications
Iphone Applications
他們的排名如下:
Apple Make The Best Apple Iphone Applications
Iphone Applications From Apple
Iphone Applications
因為找到了多少個搜索詞實例.請參閱突出顯示:
Because of how many instances of the search terms are found. See highlighted:
[Apple] Make The Best [Apple] [Iphone] [Applications]
[Iphone] [Applications] From [Apple]
[Iphone] [Applications]
我希望我已經解釋得足夠好,如果有人能給我任何指點,我將不勝感激.
I hope I have explained this well enough and I would be extremely grateful if anyone could give me any pointers.
推薦答案
查看MySQL FULLTEXT 搜索功能,這些應該會自動按相關性返回結果,并讓您更好地控制您的搜索
take a look at the MySQL FULLTEXT search functions, These should automatically return results by relevancy, and give you much more control over your searches
使用全文索引的唯一潛在問題是 InnoDB 表不支持它們.
The only potential issue with using fulltext indexes is that they aren't supported by InnoDB tables.
這篇關于PHP MySQL 按相關性搜索和排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!