MySQL match against +(search query) not working but +search +query is - mysql

I have a textinput field where users can enter a search query which I am performing a match against on in MySQL. The user can enter multiple words or single words however when I use +(search query) I get no results however when I use +search +query I get the desired results.
Works
MATCH (title) AGAINST ('+new +painting' IN BOOLEAN MODE)
Does not work
MATCH (title) AGAINST ('+(new painting)' IN BOOLEAN MODE)
First time user of MATCH AGAINST so not sure what I am doing wrong.
Thanks

For all titles with the string "new painting":
MATCH (title) AGAINST ('+"new painting"' IN BOOLEAN MODE)
For all titles with boths the strings "new" and "painting":
MATCH (title) AGAINST ('+new +painting' in BOOLEAN MODE)
For more options, and different operators, consult the documentation:
http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

Related

How to search for exact phrases using the MySQL MeCab Full-Text Parser Plugin?

I'm using MySQL 8.0.27.
MATCH (column) AGAINST ('その猫' IN BOOLEAN MODE)
MATCH (column) AGAINST ('"その猫"' IN BOOLEAN MODE)
Both statements show "その小さな猫" in the results.
The documentation says:
For boolean mode search, the search term is converted to a search phrase.
So I would expect to not need the quoation marks, but it does not work in either case.

MySQL - Match Against not search on single string [duplicate]

I understand that the asterisk is a wildcard that can be appended to the end of fulltext search words, but what if my searched keyword is a suffix? For example, I want to be able to search for "ames" and have a result that contains the name "james" returned. Here is my current query which does not work because you cannot prepend asterisks to fulltext searches.
SELECT * FROM table WHERE MATCH(name, about, address) AGAINST ("*$key*" IN BOOLEAN MODE)
I would simply switch to using LIKE, but it would be way too slow for the size of my database.
What you could do is create another column in your database with full-text search index, this new column should have the reversed string of the column you are trying to search on, and you will reverse the search query and use it to search on the reversed column, here is how the query will look like:
SELECT * FROM table WHERE MATCH(column1) AGAINST ("$key*" IN BOOLEAN MODE) OR MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
the first condition
MATCH(column1) AGAINST ("$key*" IN BOOLEAN MODE)
example:
reversedColumn1==>Jmaes $reveresedkey*==>ames*
will search for words that start with ames ==> no match
the seconds condition
MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
example:
reversedColumn1==>semaJ $reveresedkey*==>sema*
will search for words that end with ames ==> we have a match
This might not be a bad idea if your text is short:
Can't be done due to limitation of MySQL. Values are indexed left-to-right, not right-to-left. You'll have to stick with LIKE if you want wildcards prepended to search string.

Prepending an * (asterisk) to a Fulltext Search in MySQL

I understand that the asterisk is a wildcard that can be appended to the end of fulltext search words, but what if my searched keyword is a suffix? For example, I want to be able to search for "ames" and have a result that contains the name "james" returned. Here is my current query which does not work because you cannot prepend asterisks to fulltext searches.
SELECT * FROM table WHERE MATCH(name, about, address) AGAINST ("*$key*" IN BOOLEAN MODE)
I would simply switch to using LIKE, but it would be way too slow for the size of my database.
What you could do is create another column in your database with full-text search index, this new column should have the reversed string of the column you are trying to search on, and you will reverse the search query and use it to search on the reversed column, here is how the query will look like:
SELECT * FROM table WHERE MATCH(column1) AGAINST ("$key*" IN BOOLEAN MODE) OR MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
the first condition
MATCH(column1) AGAINST ("$key*" IN BOOLEAN MODE)
example:
reversedColumn1==>Jmaes $reveresedkey*==>ames*
will search for words that start with ames ==> no match
the seconds condition
MATCH(reversedColumn1) AGAINST ("$reveresedkey*" IN BOOLEAN MODE)
example:
reversedColumn1==>semaJ $reveresedkey*==>sema*
will search for words that end with ames ==> we have a match
This might not be a bad idea if your text is short:
Can't be done due to limitation of MySQL. Values are indexed left-to-right, not right-to-left. You'll have to stick with LIKE if you want wildcards prepended to search string.

Problems with MySQL MATCH AGAINST

I'm using MATCH AGAINST to search against multiple fields in the database, but am having trouble with the results.
The query is:
MATCH(productname, stockcode, productdescription, additional_1, additional_2, additional_3, additional_4, additional_5, additional_6, additional_7, additional_8) AGAINST ('red* tile*' IN BOOLEAN mode)
From the query, I would like both "red%" or "tile%" results, but this is returning records where the word 'requiRED' is included, which I don't want.
Can this be done?
Most probably, you have ft_min_word_len set to default value of 4.
red does not get indexed or matched in this setup and just ignored in the queries.
Change ft_min_word_len to a lesser value if you want to match red (requires rebuilding the index if you have one).
See this fiddle.
Update:
If you want both words matched, use this:
AGAINST ('+red* +tile*' IN BOOLEAN mode)

Mysql fulltext boolean ignore phrase

I'm trying to come up with a mysql boolean search that matches "monkey" but ignores / doesn't take into account an matches of "monkey business".
Something like the following is the general idea but it negates the value. I need it to not match rows with "monkey business" unless they also have "monkey" without "business" after it elsewhere in the text.
SELECT * FROM table
MATCH (title) against ('+monkey ~"monkey business"' IN BOOLEAN MODE);
Is it possible? Thanks in advance for any help you can give! I realise this could be done with better search engines, at the moment fulltext is all I can use.
I tested this and it works:
SELECT * FROM table
WHERE MATCH (title) against ('+monkey' IN BOOLEAN MODE)
AND NOT MATCH (title) against ('+"monkey business"' IN BOOLEAN MODE);
p.s I also tried the boolean negation operator:
AND MATCH (title) against ('-"monkey business"' IN BOOLEAN MODE)
but it didn't work.