Search and Replace Columns using Full Text Search Mysql - 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)))

Related

MySQL FULLTEXT search returns only exact match

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

MySQL - Full Text Search - reverse

I know how to get all results which contain a few words:
SELECT * FROM `table` WHERE MATCH (`row`) AGAINST ('+word1 +word2' IN BOOLEAN MODE)
But how I can all results which not contain words: "word1", "word2" ?? I need operator, something like "NOT IN". So how I can get from database everything records which not contain specific words in query using full text search?
Thanks.
You can just use NOT to negate the condition:
SELECT * FROM `table` WHERE NOT MATCH (`row`) AGAINST ('+word1 +word2' IN BOOLEAN MODE)
The MATCH condition is true on rows where it finds the words, and false where it does not find the words. Use NOT reverses the true/false result on each row.
Just like:
SELECT * FROM `table` WHERE NOT row = 'abc123'
would be true on all rows that are not the specific value 'abc123'.

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 In Boolean Mode returns nothing on middle word

I've got a problem using Match Against in my MySQL database, and I'm hoping someone can help.
This is the examples of the data in my database:
id name
1 really bitter chocolate
2 soft cheese
When I run this query:
SELECT * FROM food WHERE (name) LIKE "%bitter%"
This bring back the first result:
1 really bitter chocolate
However its part of a much larger query, and when I run the Match Against code, I don't get anything returned from either of these queries:
SELECT * FROM food WHERE MATCH (name) AGAINST ("bitter")
SELECT * FROM food WHERE MATCH (name) AGAINST ("bitter", IN BOOLEAN MODE)
I have full text searches turned on, and it works when I search the start of the name:
SELECT * FROM food WHERE MATCH (name) AGAINST ("really")
SELECT * FROM food WHERE MATCH (name) AGAINST ("really", IN BOOLEAN MODE)
Both of which returns:
1 really bitter chocolate
I've read through this for solutions: http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html
And I've looked here: mysql WHERE MATCH AGAINST
Can someone please see where I am going wrong or point me in the right direction?
Thanks!
EDIT
Ok as per Woot4Moo great answer below I've changed my code to remove the comma which shouldn't have been there. I've also added in the + and putting it in single quotes but still no luck.
My current query now looks like this:
SELECT * FROM food WHERE MATCH (name) AGAINST ('+bitter' IN BOOLEAN MODE)
But it's returning no results in query browser, and not returning any errors or warnings.
If this are the only two rows in your table then you have in 50% of the records the searched string and it will be ignored.
SELECT * FROM food WHERE MATCH (name) AGAINST ("bitter", IN BOOLEAN MODE)
looks like it should be:
SELECT * FROM food WHERE MATCH (name) AGAINST ('+bitter' IN BOOLEAN MODE)
notice how mine has the plus sign + and NO comma ,
Basing it off of the example here
MySQL can perform boolean full-text searches using the IN BOOLEAN
MODE modifier. With this modifier, certain characters have special
meaning at the beginning or end of words in the search string. In the
following query, the + and - operators indicate that a word is
required to be present or absent, respectively, for a match to occur.
Thus, the query retrieves all the rows that contain the word “MySQL”
but that do not contain the word “YourSQL”:
mysql> SELECT * FROM articles WHERE MATCH (title,body)
-> AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);

MySQL REGEXP usage within Boolean Match/Against

I have the following MySQL query:
SELECT title, description
FROM some_table
WHERE MATCH (title,description) AGAINST ('+denver (REGEXP "[[:<:]]colorado[s]*[[:>:]]")' IN BOOLEAN MODE);
the "regexp" here looks for a "complete word" colorado (with or without the ending "s").
I want to actually select only those rows that have ("denver") AND ("colorado" or "colorados"). But I cannot put a "+" for the REGEXP. I tried but got 0 results, although there are rows in the table that match the requirement.
Any ideas on how I can get the "+" to work within against using a REGEXP?
I am constructing this from within a PHP script where "denver" and "colorado" are values of variables I use to construct the select statement.
My PHP/MySQL script would look somewhat like this:
SELECT title, description
FROM some_table
WHERE MATCH (title,description) AGAINST ('+$var1 (REGEXP "[[:<:]]$var2[s]*[[:>:]]")' IN BOOLEAN MODE);
I don't think it's possible to combine regular expressions and MATCH ... IN BOOLEAN MODE. You need to use the syntax for writing boolean expressions.
Boolean Full-Text Searches
Try something like this:
SELECT title, description
FROM some_table
WHERE MATCH (title,description)
AGAINST ('+denver +(colorado colorados)' IN BOOLEAN MODE);