简体   繁体   English

PHP MySQL搜索和按相关性排序

[英]PHP MySQL Search And Order By Relevancy

I know how to do a regular php mysql search and display the results. 我知道如何做一个常规的php mysql搜索并显示结果。 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: 让我更好地解释一下:

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”将使用%apple iphone application%搜索数据库,但是如果没有按照确切顺序显示该短语的记录,则搜索将不会产生任何结果。

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. 我基本上需要做的是分别搜索'apple','iphone'和'applications'然后将结果合并为一个,然后我需要根据记录中找到的单词的多少个实例来评估相关性。 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

They would rank as follows: 他们的排名如下:

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. 我希望我能够很好地解释这一点,如果有人能给我任何指示,我将非常感激。

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 看看MySQL FULLTEXT搜索功能 ,它们应该通过相关性自动返回结果,并让您更好地控制搜索

The only potential issue with using fulltext indexes is that they aren't supported by InnoDB tables. 使用全文索引的唯一潜在问题是InnoDB表不支持它们。

A quick google gave me this link . 一个快速的谷歌给了我这个链接

Example: 例:

select title, match (title,content) against (”internet”) as score 
from cont 
where match (title,content) against (”internet”) limit 10;

Maybe this might help someone (order by relevance and keep word count): 也许这可以帮助某人(按相关性排序并保持字数):

None full-text: 无全文:

SELECT *, ( (value_column LIKE '%rusten%') + (value_column LIKE '%dagen%') + (value_column LIKE '%bezoek%') + (value_column LIKE '%moeten%') ) as count_words
FROM data_table
WHERE (value_column LIKE '%dagen%' OR value_column LIKE '%rusten%' OR value_column LIKE '%bezoek%' OR value_column LIKE '%moeten%')
ORDER BY count_words DESC

Full-text: 全文:

SELECT * FROM data_table
WHERE MATCH(value_column) AGAINST('+dagen +rusten +bezoek +moeten' IN BOOLEAN MODE)
ORDER BY MATCH(value_column) AGAINST('+dagen +rusten +bezoek +moeten' IN BOOLEAN MODE) DESC;
SELECT field2, field3, ..., MATCH(field1, field2) AGAINST ("search string") AS relevance WHERE MATCH(field1, field2) AGAINST "search string" ORDER BY relevance DESC LIMIT 0,10

在结果集中,将存在字段“相关性”,此处用于对结果进行排序。

I Don't What exactly you want but the following code definitely work for you. 我不知道你想要什么,但以下代码肯定适合你。

SELECT ("some text here" or `column_name`) RLIKE "Apple|Iphone|Application" AS Result ORDER BY Result DESC;

Separate all words with Bar(|) but results will be 1 or 0 founded or not resp. 用Bar(|)分隔所有单词,但结果将是1或0成立或不成立。 If you want to get founded rows see below. 如果您想获得成立的行,请参见下文。

SELECT * FROM "table_name" WHERE `column_name` RLIKE "Apple|Iphone|Application";

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

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