简体   繁体   English

MySQL-搜索/选择按相关性排序?

[英]MySQL - Search/SELECT which is ordered by relevance?

I have a table setup as follows: 我的表设置如下:

userid year  model  color  transmission  interiorcolor wheels
-------------------------------------------------------------------
   1   2005  Fiesta   Red   MT             Black        Stock
   2   2008  Monaro   Red   AT             Black        Stock
   3   2005  F-150S   Blue   AT            Black + Red  Stock
   4   2008  Fiesta   Green   MT           Black        Stock

Now I am building an SQL search (no standard build in full text search), and I want to results to be somewhat relevant. 现在,我正在构建一个SQL搜索(全文搜索中没有标准构建),并且我希望结果具有一定的相关性。

What i mean is that for example if someone types in "2008 Fiesta", right now with a basic query the results come out in this order 我的意思是,例如,如果有人键入“ 2008 Fiesta”,则现在使用基本查询按此顺序显示结果

id #1, id #2, id #4 ID#1,ID#2,ID#4

But really I would like #4 to be on the top of the search in that case because it matches my search on two fields instead of just one field. 但是,在这种情况下,我真的希望#4处于搜索的顶部,因为它与我在两个字段而不是一个字段上的搜索匹配。

Thank you! 谢谢!

You may want to take a look at using Lucene (or SOLR on top of Lucene), instead of relying on MySQL for something like this. 您可能想看看使用Lucene(或在Lucene之上的SOLR),而不是依靠MySQL这样的东西。 You can still store your real data in MySQL. 您仍然可以将实际数据存储在MySQL中。 The general setup I use is SOLR/Lucene to do full text/relevance searches to get the ids of the entities I'm interested in, then hit MySQL to grab the actual data for those entities. 我使用的常规设置是SOLR / Lucene进行全文搜索/相关性搜索,以获得我感兴趣的实体的ID,然后点击MySQL来获取这些实体的实际数据。 Using SOLR/Lucene gives you a full text search and adds a lot more power in the ways you can search/order the data, plus it gives things like stemming and the like. 使用SOLR / Lucene可以为您提供全文搜索,并以搜索/排序数据的方式增加了很多功能,此外还提供了词干等功能。

I think your select statement is like: 我认为您的select语句如下:

LIKE %2008 Fiesta%

Try taking the last % , it will work 尝试服用最后的% ,它将起作用

This is crude but it should work for you: 这很粗糙,但它应该为您工作:

SELECT id, SUM(Relevance) as SearchRelevance
FROM(
SELECT id, 1 as Relevance
FROM cars
WHERE year = 2008
UNION ALL
SELECT id, 1 as Relevance
FROM cars
WHERE model='Fiesta') AS t
GROUP BY id
ORDER BY SUM(Relevance) DESC

Basically it has a search for each criteria, UNIONs them all together and then groups them and orders by the SUM of relevance (where you get one point per matching item). 基本上,它会搜索每个条件,将它们全部合并在一起,然后按相关性总和对它们进行分组和排序(每个匹配项可得1分)。

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

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