Mysql MATCH AGAINST options: exact phrase along with extra word - mysql

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)

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'.

Match statement spaced words return results from multiple columns

In this situation, I want to find all records that contain the name steve in one column and email#email.com in another. I know Im missing an operator, but i dont know which
SELECT firstname,lastname,middlename,company_name,
primary_emailaddress,alternate_emailaddress,personal_address_line1,
personal_address_line2,personal_address_city,facebook_username,
twitter_username,googleplus_username,linkedin_username,
personal_website_url,birthday_month,notes,personal_address_zipcode,
company_address_zipcode,home_phonenumber,company_phonenumber,
cell_phonenumber,birthday_day,birthday_year,hash,image_file
FROM contacts
WHERE (
MATCH(
firstname,lastname,
primary_emailaddress,alternate_emailaddress,personal_address_line1,
personal_address_city,company_name,
company_address_line1,company_address_city,
facebook_username,twitter_username,googleplus_username,linkedin_username,
personal_website_url
)
AGAINST ('Steve email#email.com' IN BOOLEAN MODE))
Match is faster than a like comparison. I think you can do it twice in a where clause:
SELECT firstname,lastname,middlename,company_name,
primary_emailaddress,alternate_emailaddress,personal_address_line1,
personal_address_line2,personal_address_city,facebook_username,
twitter_username,googleplus_username,linkedin_username,
personal_website_url,birthday_month,notes,personal_address_zipcode,
company_address_zipcode,home_phonenumber,company_phonenumber,
cell_phonenumber,birthday_day,birthday_year,hash,image_file
FROM contacts
WHERE MATCH (firstname) AGAINST ('Steve' IN BOOLEAN MODE) > 0 and
MATCH (primary_emailaddress, alternate_emailaddress)
AGAINST ('email#email.com' IN BOOLEAN MODE) > 0;
Unless you are really looking through every field, (i.e. John Stevenson would be considered a match on 'Steve') you can do this much simpler...
select *
from contacts
where firstname like '%Steve%'
and (primary_emailaddress like '%email#email.com%' or alternate_emailaddress like '%email#email.com%')

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

mysql fulltext multiple words from one form input?

I've heared alot of similar discussion but I havent seen a direct solution.
SELECT * FROM patient_db WHERE
MATCH ( Name, id_number ) AGAINST ('%$term%' IN BOOLEAN MODE);
Isn't there something simple around my '%$term%' I need to do to enable multiple-word searching?
Unfortunately, there is no way to directly say, "I want one of these n words" in a MySQL fulltext query. Your only real option is
SELECT * FROM patient_db WHERE
MATCH ( Name, id_number ) AGAINST ('%$term%' IN BOOLEAN MODE)
OR MATCH ( Name, id_number ) AGAINST ('%$term2%' IN BOOLEAN MODE)
OR MATCH ( Name, id_number ) AGAINST ('%$term3%' IN BOOLEAN MODE)
;
Just curious, but why are you searching a column named id_number for text?