mysql match against multiple words - mysql

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

Related

query that returns rows that do not contain a word in MySql

I am trying to make a query where I select all the rows that do not contain a specific word, for this I have a fulltext type index in this column, try the following bolt works:
SELECT *
FROM products
WHERE MATCH(title) AGAINST(' -Dolo' IN BOOLEAN MODE)
So how can I perform this search?
If I have understood you correctly you want to find all the rows from the table that do not contain a word'Dolo'.
Well you can use NOT operator for that.
SELECT *
FROM products
WHERE NOT MATCH(title) AGAINST('Dolo');
Here is a DEMO.
Also, you can use it like this(because as the OP has asked: "if the whole word is "dolorem", would this query work?"):
SELECT title as Title
, MATCH(title) AGAINST('Dolo*' IN BOOLEAN MODE) as Score
FROM products
WHERE MATCH(title) AGAINST('Dolo*' IN BOOLEAN MODE) = 0;
* is a wildcard.
Other signs are described here: https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html
Here is the DEMO for the second example.

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");

Fulltext search doesn't exclude -xxxword

We use fulltext search on a french CMS ( SPIP ). The resultat of a search with a minus word doesn't seems correct. We tried the sql request generated by the CMS in phpMyAdmin :
SELECT t.id_zotspip, t.titre, t.resume,
MATCH(t.`titre`) AGAINST ('+corrosion -bacteria' IN BOOLEAN MODE) * 10
+ MATCH(t.`titre`,t.`resume`) AGAINST ('+corrosion -bacteria' IN BOOLEAN MODE) * 2.2 AS score
FROM `spip_zotero311`.spip_zitems AS t
WHERE ((MATCH(t.`titre`) AGAINST ('+corrosion -bacteria' IN BOOLEAN MODE))
OR (MATCH(t.`titre`,t.`resume`) AGAINST ('+corrosion -bacteria' IN BOOLEAN MODE)))
GROUP BY t.id_zotspip ORDER BY score DESC LIMIT 0,500
We get results with bacteria in the resume...
The type of the table is MyISAM but the type of the base is InnoDB.
Any suggestions ?
Thank you.
The problem is in OR clause inside where. You should replace OR inside WHERE clause with AND.

match against, sort by relevance

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?

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