Mysql full text search exact phrase problem - mysql

Using full text search in mysql I'd like to have exact phrase:
"romantic dinner" to be found.
But I also would like each of the words could have synonyms like:
"romantic dinners" to be found for example (our language has great problem where every word has 8 endings like)...
I tried:
+"romantic (dinner dinners)" and
"romantic +(dinner dinners)"
but nothing seems to get results... Is it possible to make some logical OR inside exact search?
UPDATE: TO make it one sentance question: Is there a way to put some logical operators in exact match ("") in full text search?

It will only partially your problem, but there is a soundex() function in mysql which transform the given string to a soundex representation. Similars string should have the same soundex representation so it's maybe a start.
Hope this helps.

If '"roman* dinn*" doesn't work for you, it might be time to look into something like Solr: http://lucene.apache.org/solr/ which will allow for more sophisticated searches, and might already have a stemmer for your language.

SELECT field_name FROM table_name WHERE MATCH(field_name) AGAINST('romantic* dinner*' IN BOOLEAN MODE)...
definitely you can use + and - to further refining your results
reference: http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html

Related

How can I find a list of rows which contain a string similar to "Butterflies" in MySQL? [duplicate]

I have a table dictionary which contains a list of words Like:
ID|word
---------
1|hello
2|google
3|similar
...
so i want if somebody writes a text like
"helo iam looking for simlar engines for gogle".
Now I want to check every word if it exists in the database, if not it should
get me the similar word for the word. For example: helo = hello, simlar = similar, gogle = google.
Well, i want to fix the spelling errors. In my database i have a full dictionary of all english words. I coudn't find any mysql function which helps me. LIKE isn't helpfull in my situation.
you can use soundex() function for comparing phonetically
your query should be something like:
select * from table where soundex(word) like soundex('helo');
and this will return you the hello row
There is a function that does roughly want you want, but it's intensive and will slow queries down. You might be able to use in your circumstances, I have used it before. It's called Levenshtein. You can get it here How to add levenshtein function in mysql?
What you want to do is called a fuzzy search. You could use the SOUNDEX function in MySQL, documented here:
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex
You query would look like:
SELECT * FROM dictionary where SOUNDEX(word) = SOUNDEX(:yourSearchTerm)
... where your search term is bound to the :yourSearchTerm parameter value.
A next step would be to try implementing and making use of a Levenshtein function in MySQL. One is described here:
http://www.artfulsoftware.com/infotree/qrytip.php?id=552
The Levenshtein distance between two strings is the minimum number of
operations needed to transform one string into the other, where an
operation may be insertion, deletion or substitution of one character.
You might also consider looking into databases that are aimed at full text searching, such as Elastic Search, which provides this natively:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

MYSQL search for right words | fixing spelling errors

I have a table dictionary which contains a list of words Like:
ID|word
---------
1|hello
2|google
3|similar
...
so i want if somebody writes a text like
"helo iam looking for simlar engines for gogle".
Now I want to check every word if it exists in the database, if not it should
get me the similar word for the word. For example: helo = hello, simlar = similar, gogle = google.
Well, i want to fix the spelling errors. In my database i have a full dictionary of all english words. I coudn't find any mysql function which helps me. LIKE isn't helpfull in my situation.
you can use soundex() function for comparing phonetically
your query should be something like:
select * from table where soundex(word) like soundex('helo');
and this will return you the hello row
There is a function that does roughly want you want, but it's intensive and will slow queries down. You might be able to use in your circumstances, I have used it before. It's called Levenshtein. You can get it here How to add levenshtein function in mysql?
What you want to do is called a fuzzy search. You could use the SOUNDEX function in MySQL, documented here:
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex
You query would look like:
SELECT * FROM dictionary where SOUNDEX(word) = SOUNDEX(:yourSearchTerm)
... where your search term is bound to the :yourSearchTerm parameter value.
A next step would be to try implementing and making use of a Levenshtein function in MySQL. One is described here:
http://www.artfulsoftware.com/infotree/qrytip.php?id=552
The Levenshtein distance between two strings is the minimum number of
operations needed to transform one string into the other, where an
operation may be insertion, deletion or substitution of one character.
You might also consider looking into databases that are aimed at full text searching, such as Elastic Search, which provides this natively:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html

Mysql Full-Text Search - Searching for part of a keyword

I've read about full-text search functions in mysql. But in these methods you have to search for exactly right spelled complete words.
For example if your text contains 'Bitdefender 2009' and you search for Bit, you get nothing
SELECT * FROM logs WHERE MATCH (log) AGAINST ('Bit 09' IN BOOLEAN MODE);
So are there any solution for this?
(Is there a technique which would let you search for misspelled keywords as well? for example you search for Bitdefedner)
You could also turn to Lucene or other specialized search engine mentioned in https://stackoverflow.com/questions/553055/best-full-text-search-for-mysql
You probably need the help of LIKE, REGEXP, AGAINST or SOUNDEX functions.
Have a look at the following:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex
You could use the * wildcard AGAINST ('Bit* 09' IN BOOLEAN MODE)
For misspelt keywords you need a separate spellcheck phase.

Query MySQL with unicode char code

I have been having trouble searching through a MySQL table, trying to find entries with the character (UTF-16 code 200E) in a particular column.
This particular code doesn't have a glyph, so it doesn't seem to work when I try to paste it into my search term. Is there a way to specify characters as their respective code point instead for a query?
Thanks,
-Ben
Not tested, but CHAR() could work for this:
CHAR(0x200E);
I can't set up a full test case right now, let us know whether it worked.

Punctuation insensitive search in mySQL

I have a database of phrases that users will search for from their own input. I want them to find the phrase regardless of what punctuation they use. For example if the phrase, "Hey, how are you?" is in the row, I want all of the following searches to return it:
"Hey! How are you?!"
"Hey how are you?"
"Hey :) How are you?"
Right now, I have the columns 'phrase' and 'phrase_search'. Phrase search is a stripped down version of phrase so our example would be 'hey-how-are-you'.
Is there anyway to achieve this without storing the phrase twice?
Thank you!
-Nicky
What you've done is probably the most time-efficient way of doing it. Yes, it requires double the space, but is that an issue?
If it is an issue, a possible solution would be to convert your search string to use wildcards (eg. %Hey%how%are%you%) and then filter the SQL results in your code by applying the same stripping function to the database input and the search string and comparing them. The rationale behind this is that there should be relatively few matches with non-punctuation characters in-between the words, so you're still getting MySQL to do the "heavy lifting" while your PHP/Perl/Python/whatever code can do a more fine-grained check on a relatively small number of rows.
(This assumes that you have some code calling this, rather than a user typing the SQL query from the command line, of course.)