MySQL FULLTEXT search returns only exact match - mysql

I added a FULLTEXT index to a table like so:
ALTER TABLE TEST ADD FULLTEXT(name, descrip, notes);
The TEST table has 100 rows. I updated the descrip column with 'branch' in one row and 'Branches' in another row.
Then I run
SELECT * FROM TEST WHERE MATCH(name, descrip, notes) AGAINST ('branch' IN BOOLEAN MODE);
The result returned only rows that contain 'branch'. 'Branches' is not included in the result, even though it is comprised of the word that was searched. How to include 'Branches'? I want to avoid using LIKE.

In boolean mode you can use a wildcard:
AGAINST ('branch*' IN BOOLEAN MODE);

Here goes your query. You need to use wildcard.
SELECT * FROM TEST WHERE MATCH(name, descrip, notes) AGAINST ('branch*' IN BOOLEAN MODE);

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.

relevance score of MATCH..AGAINST is not working (MySql)

Relevance score of MATCH..AGAINST is not working.
Created one dummy table which has 2 rows.
Dummy Table
Row1=> 'Leela Hayat Marriot'
Row2=> 'Americas Best Value'
Query1:
SELECT MATCH (col1) AGAINST ('Leela* Hayat*' IN BOOLEAN MODE) AS relevance
FROM table1
WHERE MATCH (col1) AGAINST ('Leela* Hayat*' IN BOOLEAN MODE);
Result:
relevance
2
Query2:
SELECT MATCH (col1) AGAINST ('Americas* Best*' IN BOOLEAN MODE) AS relevance
FROM table1
WHERE MATCH (col1) AGAINST ('Americas* Best*' IN BOOLEAN MODE);
Result:
relevance
1
Query1 is working fine but why is query 2 not working?
Why am I getting relevance 1 instead of 2 in Query2 as Americas and Best both are present in column.
Thanks
http://dev.mysql.com/doc/refman/5.7/en/fulltext-stopwords.html
'BEST' is listed in stopword list.
http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_ft_stopword_file
ft_stopword_file:
The file from which to read the list of stopwords for full-text searches on MyISAM tables. The server looks for the file in the data directory unless an absolute path name is given to specify a different directory. All the words from the file are used; comments are not honored. By default, a built-in list of stopwords is used (as defined in the storage/myisam/ft_static.c file). Setting this variable to the empty string ('') disables stopword filtering.
I disabled stopwords list and now query 2 is working fine.
Thanks for help.

Mysql MATCH AGAINST options: exact phrase along with extra word

I am trying to enhance a third part (awesome) django framework named django-watson and I currently need to make my way through a so far unknown mysql option, the MATCH (...) AGAINST (...).
So, I already know how to retrieve an exact phrase, which is doing:
SELECT *
FROM patient_db
WHERE MATCH ( Name, id_number )
AGAINST ('"exact phrase"' IN BOOLEAN MODE);
An I also know how to retrieve results that contain words from a list:
SELECT *
FROM patient_db
WHERE MATCH ( Name, id_number )
AGAINST ('+keyword1 +keyword2' IN BOOLEAN MODE);
But I need a third option, which is mixing the two above quoted. I'd like to do something like the google search: "exact phrase" +keyword1 +keyword2.
_PS: when I search for "exact phrase" -keyword1 it works exactly as desired _
Any Ideas?
Try this.
SELECT *
FROM patient_db
WHERE MATCH ( Name, id_number )
AGAINST ('+keyword1 +keyword2' IN BOOLEAN MODE)
OR MATCH ( Name, id_number )
AGAINST ('"exact phrase"' IN BOOLEAN MODE)

MySQL not using index when using "OR" in WHERE

I have a problem with the query:
Select * From table Where MATCH(col1) AGAINST('mystring' IN BOOLEAN MODE) OR MATCH(col2) AGAINST('mystring' IN BOOLEAN MODE)
because it is not using the fulltext index, as the query:
Select * From table Where MATCH(col1) AGAINST('mystring' IN BOOLEAN MODE) AND MATCH(col2) AGAINST('mystring' IN BOOLEAN MODE)
does.
Both col1 and col2 has a fulltext index, but when using OR instead of AND, mysql performs a full table search instead of using one of the indexes.
How can I select records with (at least) one of the 2 columns matching 'mystring' using indexes?
Just try to select each set by isolated query (with only one filter) and than intersect results with UNION.
Another form:
SELECT * FROM table WHERE MATCH (col1, col2) AGAINST ('mystring' IN BOOLEAN MODE)

Search and Replace Columns using Full Text Search Mysql

Is there a way of running a Mysql Query using Full Text Search that will search a column for example (hayes) and replace the rest of the string with "hayes" once matched?:
Column1:
london, hayes, MD15 736
WHERE MATCH (COLUMN1) AGAINST ('hayes' IN BOOLEAN MODE)
Column1:
hayes
So since column 1 has matched "hayes" its removed the rest of the string. It needs to be done by full text as like for example takes too long on large data sets.
And there is no set order where the keyword maybe so i cannot use find_in_set
Also i will running this with a large number of keywords like the below:
WHERE MATCH (COLUMN1) AGAINST ('hayes' IN BOOLEAN MODE)
OR MATCH (COLUMN1) AGAINST ('+WEST +BROMWICH' IN BOOLEAN MODE)
OR MATCH (COLUMN1) AGAINST ('London' IN BOOLEAN MODE)
So if i run the above query on a column1 which contains the following string:
london, m456, hayes <<< it would match hayes ONLY and not both london and hayes.
you can try Find rows that contain at least one of the two words.
AGAINST ('+hayes +London' IN BOOLEAN MODE)
if you want to find row that contain both words then use this
AGAINST ('+hayes London' IN BOOLEAN MODE)
EDIT:
UPDATE table
SET COLUMN1 =
'HAYES'
WHERE
COLUMN1
LIKE
'%hayes%';
if you have more keywords use like that
UPDATE table
SET COLUMN1 =if(
COLUMN1 LIKE '%hayes%', 'HAYAS', if(
COLUMN1 LIKE '%derby%' ,'DERBY' , if(
COLUMN1 LIKE '%wawwaw%' ,'WAWAWA', Column1)))