PHP MySQL Search And Order By Relevancy

后端 未结 5 589
[愿得一人]
[愿得一人] 2020-12-09 19:12

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.

相关标签:
5条回答
  • 2020-12-09 19:46

    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;
    
    0 讨论(0)
  • 2020-12-09 19:46

    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. If you want to get founded rows see below.

    SELECT * FROM "table_name" WHERE `column_name` RLIKE "Apple|Iphone|Application";
    
    0 讨论(0)
  • 2020-12-09 19:54

    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

    The only potential issue with using fulltext indexes is that they aren't supported by InnoDB tables.

    0 讨论(0)
  • 2020-12-09 19:59

    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;
    
    0 讨论(0)
  • 2020-12-09 20:03
    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
    

    In the result set, there will be a field "relevance", which is used here to sort the results.

    0 讨论(0)
提交回复
热议问题