How would the best way to approach this problem be?
Search for user by first name, where the parameter is a string. The result should display a list
of the 10 users whose first name is closest to the searched string.
Clearly there are different ways to approach this one. Is there any mysql functionality that does this already?
Should I just query all my rows and run some kind of search function?
Have a look at MySQL Full-Text search functions which will automatically return results based on relevancy, though it will not work with InnoDB.
Related
Am trying to search my sphinx index instance but can't seem to get it working with my current logic.
Basically, am using Query Builder for SphinxQL instead of the offical API but works pretty well until I tried comparing columns against each without any success.
Example 1: In MySQL
SELECT ID, NAME, NICKNAME WHERE NAME=NICKNAME;
Example 2: Query Builder for SphinxQL
$instance->select(...)->from('theIndex')->where('NAME', '=', 'NICKNAME');
The first example works well in mysql but the second one is using PHP to access SphinxQL and thus doesn't care that the value entered in the value field is actually a column. It never matches at all.
Is what am trying to achieve here even possible at all in sphinx?
I have spent way too much too time searching but can't find a viable solution.
Any help will be greatly appreciated.
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
I want to make a search facility in my website.I'm using php..
What criteria should be taken for searching.
For ex: if someone searches
How to make soap
I can use many approaches for the search like finding database entries having exactly the same search string
or finding the database entries in the order of search keywords(ie . entry with search string "How" +"Soap" will have less preference than entry having search string "how soap make")...
So what is the algorithm generally used for searching.?
Also what is meant by full text search?
This is kind of a big subject for a simple answer, but I think what you mean is how to run complex fulltext searches on MySQL. In other words, this is really a MySQL question, not a PHP one.
Basically, you need to:
1. Create a fulltext index on a text field in your database.
2. Run queries on that database field using MySQL's fulltext syntax.
The basic syntax for querying a fulltext indexed table in MySQL is:
SELECT * FROM table
WHERE MATCH (fulltextfield)
AGAINST ('my search phrase');
There's a lot more to it than that, but the MySQL documentation is the place to go: http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
If you want to do really advanced fulltext searches, a good recommendation is Sphinx, but that's probably way more advanced than you need.
I have a MySQL Database table with peoples names with thousands of rows.
I'm coding a search script for this table to display the most similiar names stored in the table.
So I thought of fetching ALL the rows of the table, then using a FOREACH loop that will call similar_text() (a function that returns a percentage number) and then display on the table only the names that matches 60% of similarity.
Will my website performance slow too much if do this (fetching all rows)?
Will my server bandwidth suffer because of that?
ps: 'SOUNDS LIKE' MySQL command doesn't help much on this case
Let the database do the searching.
See this question, looks like what you need: How to find similar results and sort by similarity?
Yes this will most likely slow down your site, especially as your site grows and you have many users searching simultaneously.
If possible use a stored procedure or user defined function inside the database to do the searching. Also even if you don't know the exact spelling of the entry you are looking for, if you know the first letter you can speed up the search. You can use something like WHERE name LIKE 'F%' AND similar_text(name, 'FOOBAR') > 0.6 because then an index can be used to find only those rows that start with F.
Is there a PHP or MySQL function which will check how relevant a matching field is? Could it review the string and match against a percentage of characters?
For example I am doing a basic search script pulling back results but how can I make the more relevant results appear at the top?
A lot depends on your data and the type of searches that you are expecting. But basically, you could be looking for a fuzzy search. Soundex and Levenshtein distance are two of the many functions that you can use for string matches
http://php.net/manual/en/function.levenshtein.php
Well, you are asking a few complicated questions here. Mostly, I think you are looking for information retrieval techniques. Some answers are all over Stack OVerflow.
What tried and true algorithms for suggesting related articles are out there? is great I think
You might want to use the levenshtein distance if you are just looking for how closely a keyword matches an existing keyword.
I tried :P
Mysql has a function MATCH
You can youse it like
SELECT * FROM `table` WHERE MATCH(content) AGAINST('search text')
So it will look within content how relevancy it is.
But you need to index field content to FULLTEXT which requires an table type "MYISAM".
The output will automaticly sorted ascending.
hope this helps