I’m working on a web application and need some help with a query. I’m using fulltext, this is how it’s looking:
SELECT id, name, city, state, country, date
FROM events_search
WHERE MATCH(name, city, state, country)
AGAINST ('$str')
As you can see I’m using an Index that contains the name, city, state and country and matching it with a string that the user enters. This string can contain any combination of the fields I just mentioned. So I need to query the db, find the most relevant results and then show them to the user. So far it’s working good but not perfect.
For example: if the user enters Miami Florida, the first 5 results are in Miami and in other cities in Florida. The next 5 are in Miami Arizona (I don’t want to show this one), and after I’ll get more results that are in Florida and Miami (Florida). So what I want to do is determine the field that has been found more times and show the results that have this field on top. In this case it’ll be Florida, since 70% of the results have Florida in the state field.
Include your fulltext search in the
SELECTclause and you will get a score, how relevant MySQL thinks it is.That doesn’t make the query slower, the optimizer checks, that it’s two times the same search criteria.
With the
ORDER BYclause your “miami Arizona” should show up later than “miami(florida)”.