MYSQL search for right words | fixing spelling errors - mysql

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

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

How to search for Soundex() substrings in MySQL?

i got a problem with the Joomla! 3 integrated search engine. This engine's indexer creates so called soundex-values when indexing content like, for example
Testobject,
Testobject 1,
Testobject 2239923,
Textobject ....
which all have the same soundex-value of T23123.
Now my problem is, if i do a search for Test, then there won't be any results since the soundex-value for this term is T230.
The query used by the search engine is:
SELECT DISTINCT t.term_id AS id, t.term AS term
FROM tablename AS t
WHERE t.soundex = SOUNDEX('test')
I checked the soundex_match function in this topic, but unfortunately this cannot resolve my problem, because it does not compare soundex values.
I want to avoid hacking the cms core and would like understand if there is some kind of approximation procedure available to compare soundex-values like for regular queries when using the % symbol which i could then try to implement using a plugin or whatever.
The MSSQL DIFFERENCE function mentioned here would be ideal, if it would be available in MySQL and ready to use a soundex value as second parameter.
I am not very well experienced in MySQL and have no idea how to improve the query to also match soundex-substrings.
You're probably looking to calculate the Levenshtein distance; but if you simply want to find those records that start with something that sounds similar to the search term, you can strip any trailing 0 (which is merely used for padding) and then search for soundex strings with the resulting prefix:
WHERE t.soundex LIKE CONCAT(TRIM(TRAILING '0' FROM SOUNDEX('test')), '%')

SQL query for words (not the sentence)

I would like to query a single column (varchar):
sample datarows:
1) The fox jumps like a foo on my bar
2) Jumpers are not cool
3) Apple introduced iJump
When I enter a search criteria like... jump
I expect to get a resultset of: jumps, Jumpers, iJump
(So I dont want the complete row)
Currently I'm using MySQL (I'm open to suggestions as long it's open source)
Since you're using MySQL, I might suggest looking into LIB_MYSQLUDF_PREG.
This open source library will provide you with additional regex functionality, including the PREG_CAPTURE function, which extracts a regex match from a string.
Using this function, you could easily build a regex to return the match you're looking for... Something like:
\b\w*jump\w*\b
Getting any row with your search criteria is easy:
SELECT sentence
FROM sentences
WHERE sentence LIKE '%jump%'
I'd probably do the rest in application logic, since doing it in the database doesn't help you at all.
Also, any method of splitting a string and handling it will probably be database-specific, so you would need to say which one you're using.

Mysql full text search exact phrase problem

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

using LIKE operator in mysql

I want search companies from my company table when i give company name...here am using like operator
eg: saravana stores
it gives the result saravana stores texttiles,saravana stores thanga maligai,etc(which is contained with saravana stroes...coz of using LIKE operator)
Now my problem is when i give lcd projectors in the companyname, also want to fetch the records which are contained with the only projector word...but like operator gave the results with the 'lcd projector'
am making clear?
Try:
WHERE (name LIKE '%saravana%' OR name LIKE '%stores%')
This has two disadvantages:
It can't use an index so it will be slow.
It can give you matches you don't want like 'bestorest' matches '%stores%'.
You might want to use a full text search instead. You could also consider an external engine such as Lucene.
If you want proper fultext search, I highly recommend trying Lucene or Sphinx.
I know it would get a little complicated, but it's worth it for the end result.
Mark Byers is right.
To get more efficiency
After query dividing to words you can modify search input to get word base and unify searching to get smth lika:
WHERE (name LIKE '%sarava%' OR name LIKE '%stor%')