match against, sort by relevance - mysql

My Query
$query = selectQuery('SELECT * FROM page_info WHERE MATCH(content)
AGAINST("reactie klanten" WITH QUERY EXPANSION)');
how can I sort by relevance? i get back about 10 rows, but the row that actually contains the words 'reactie' and 'klanten' is somewhere in the middle. the rest of the results is somehow relevant with the words :S
MySQL documentation about 'WITH QUERY EXPANSION'
EDIT
changed by query to:
SELECT *, MATCH(content) AGAINST("reactie klanten") AS relevance FROM page_info WHERE MATCH(content) AGAINST("reactie klanten" WITH QUERY EXPANSION) ORDER BY relevance DESC
this seems promising, because now the field is on top of the list.
still how are the others related?
example:
$text = This page is being tested by our tester
how is $text relevant to reactie klanten?

Related

Irrelevant results in mysql full text search

I have a Mysql table set up for full text search across both the title and the content (body) columns.
I'm trying to bring the most relevant results to the top but I get a lot of garbage.
I have 3 full text indexes, one for the title, one for the body and one for both the title and the body so I can execute the following query:
SELECT id, url, title, body, earliestCapture, responseYear, urlScore,
MATCH (title) AGAINST ("jurassic park" IN BOOLEAN MODE) AS titleScore,
MATCH (body) AGAINST ("jurassic park" IN BOOLEAN MODE) AS bodyScore,
(SELECT (titleScore * 100 + bodyScore)) AS finalscore
FROM Entries
WHERE MATCH (title,body) AGAINST ("jurassic park" IN BOOLEAN MODE)
ORDER BY finalScore DESC LIMIT 0,1000;
I'm trying to multiply the score of the title by 100 to bring instances where the term is in the title to the top.
This does help, but if the body has the word park repeated many times even without the word Jurassic appearing a single time, that row is propelled to the top of the search results.
A great example of that is when I search for "intel pentium". There are a few rows with bodies that use the word intel in the context of intelligence/information and not the company name, that word is repeated hundreds of times and even though there are no instances of the word pentium, those pages are always on the top.
I'm getting really annoyed by this. Does anyone know how to improve the search results?
Thank you!
you ahev to add a + to both search terms so that only results are shown that have both see manual
SELECT id, url, title, body, earliestCapture, responseYear, urlScore,
MATCH (title) AGAINST ("+jurassic +park" IN BOOLEAN MODE) AS titleScore,
MATCH (body) AGAINST ("+jurassic +park" IN BOOLEAN MODE) AS bodyScore,
(SELECT (titleScore * 100 + bodyScore)) AS finalscore
FROM Entries
WHERE MATCH (title,body) AGAINST ("+jurassic +park" IN BOOLEAN MODE)
ORDER BY finalScore DESC LIMIT 0,1000;

Standard ORDER BY's doesnt work on my query

I Want to Order the search results by relevancy but standard techniques doesn't work on my code because it is doing double search check in the query to find the ones with 'Match' and 'Like' as well. How can I perform it with the code below;
$qry = mysql_query("
SELECT *
FROM workdb
WHERE publish = 'Yes'
AND MATCH (workname, keyword1, keyword2, keyword3) AGAINST ('*$searched*' IN BOOLEAN
MODE)
OR publish = 'Yes'
AND (workname LIKE '%$searched%'
OR keyword1 LIKE '%$searched%'
OR keyword2 LIKE '%$searched%'
OR keyword3 LIKE '%$searched%')
LIMIT $offset, $rowsperpage
");
EDIT: I want to display the result from most relevant to least relevant. The reason I use Math and LIKE both is to not miss any matched elements from the search.
EDIT2: OK lets make it clear, I have a workname and 3 keywords in database rows so I want to get the ones with the Best match to my searched text/word and put them into an order to most LIKE to least LIKE/ Most matches to Least Matches.
The answer is here if anyone have the same problem. Nobody understand me instead they downvote.
mysql_query("SELECT *,
(MATCH (workname) AGAINST ('*$searched*' IN BOOLEAN MODE)
AND
(workname LIKE '%$searched%'))
AS relevancy
FROM workdb
WHERE publish='Yes'
AND
(MATCH (workname, keyword1, keyword2, keyword3) AGAINST ('*$searched*' IN BOOLEAN MODE)
OR
(workname LIKE '%$searched%' OR keyword1 LIKE '%$searched%' OR keyword2 LIKE '%$searched%' OR keyword3 LIKE '%$searched%'))
ORDER BY relevancy DESC
LIMIT $offset, $rowsperpage");

MySQL Full text search AND operator

Is it possible to use full test search in InnoDB engine with AND operator for natural language mode? I mean query like below but with all words as required:
SELECT *, MATCH (body) AGAINST ('mysql database') AS score FROM post ORDER BY score DESC;
For this query i want return all records which match both words:'mysql' AND 'database'. I know i can use + and BOOLEAN mode, but it causes another problems like other operators which i want to ignore (*,-,"'..)
Add a WHERE clause that tests for each word separately:
SELECT *, MATCH (body) AGAINST ('mysql database') AS score
FROM post
WHERE MATCH(body) AGAINST ('mysql') AND MATCH(body) AGAINST('database')
ORDER BY score DESC;

mysql match against multiple words

Below is an example sql I am stuck with, it will not return a product named "iphone 4s", It returns 10 other result. Any help would be great thanks
1st sql example
SELECT * FROM products
WHERE match(desc) against('+iphone +4s' IN BOOLEAN MODE) LIMIT 10";
result: contains the words 'iphone' and '4s'
2nd sql example
SELECT * FROM products
WHERE match(desc) against('+iphone 4s' IN BOOLEAN MODE) LIMIT 10";
result: contains the words 'iphone', but rank rows higher if they also contain '4s'
3rd sql example
SELECT * FROM products
WHERE match(desc) against('iphone 4s' IN BOOLEAN MODE) LIMIT 10";
result: contains the words 'iphone' or '4s'
What I want to search is 'iphone 4s', but it comes with other result, like 'iphone is good, but 4s...', 'new iphone 5 has published...',...
Can anyone help me to solve it? thanks.
To match an exact phrase, just use double quotes to surround the phrase to match;
SELECT *
FROM products
WHERE MATCH(desc)
AGAINST('"iphone 4s"' IN BOOLEAN MODE)
LIMIT 10
More info at the manual pages.
Use REGEXP
SELECT * FROM products
WHERE desc REGEXP 'iphone[[. .]]*4s'
LIMIT 10;
SQLFiddle demo

MySQL: How to do basic search by relevance?

I read the official page: http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
but it's not providing an example
I have 1 col named "mycol", and suppose user input is "keyword1 keyword2". I want to display results ordered by relevance. I tried something like this:
select id,mycol,match(mycol) against('keyword1 keyword2' in boolean mode) as relevance from mytable
It's returning all records relevance is 0. What am I doing wrong?
try
select id,mycol,match(mycol) against('keyword1 keyword2') as relevance from mytable order by relevance desc