I've used full text search functionality, however it's not working up to the expectation.
I've following code in search.php file:
$kws = $_POST['kw'];
$kws = mysql_real_escape_string($kws);
$query = "SELECT * FROM products WHERE MATCH (product_name,brand,short_desc)
AGAINST ('*".$kws."*' IN BOOLEAN MODE) limit 14" ;
$res_old = mysql_query($query);
'kw' is something what I type in search box. Now for an example, if I search for 'Dove Intense', it places Dove Antihairfall on top because that's on top in database.
I understand I'm searching the full text functionality over two separate columns i.e. brand & product_name, this situation can occur. However is there anyway I can have it the other way round so that it actually ranks the search higher if it matches against both the columns. Basically what user types in, I need that thing ranks higher in search result.
Anyone can give some idea how to achieve that?
You (or others) may still be interested in the answer:
From Mysql documentation for match against using boolean mode :
"They do not automatically sort rows in order of decreasing relevance. You can see this from the preceding query result:..." CheckThis
You should use boolean operations to achieve what you expect, so a search for 'Dove Intense' will return rows that contain at least one of the two words (ie. rows with Dove only + rows with Intense only + rows with both dove, intense (in any order) ), simply because having no operation between the two words indicates an OR operation !
This may not be the result you expect, since you may be interested to make an "and" operation between the two words, but what about the order?
If you don't mind the order (ie. any row containing both words will be included in the results ex. 'Intense whatever whatever dove ...') this means that you should match against:
'+Dove +Intense'
in this search, rows containing only one of the two worlds will not be included in result.
if you are trying to implement a strict search ie. only rows containing this phrase "Dove Intense" you should match against '"Dove Intense"'
Now about ranking:
If you want to obtain all results having at least "Dove" but rank rows higher if they also contain "Intense", then you should match against '+Dove Intense'
hope this was useful for what you are trying to implement.
Related
I would like to search for an email address in the content field.
SELECT *
FROM message m
WHERE MATCH (m.content) AGAINST (
'robert.oppenheimer#gmail.com' IN BOOLEAN MODE
)
Well it finds all the messages where a 'robert' or an 'oppenheimer' is. But i would like it to behave like
SELECT *
FROM message m
WHERE m.content LIKE '%robert.oppenheimer#gmail.com%'
Any ideas?
This will give you exactly what you want:
SELECT *
FROM message m
WHERE MATCH (m.content) AGAINST (
'+"robert.oppenheimer#gmail.com"' IN BOOLEAN MODE
)
Combine them.
SELECT *
FROM message m
WHERE MATCH (m.content) AGAINST ('annie.testman#example.com')
AND m.content LIKE '%annie.testman#example.com%';
Many people realize that LIKE '%thing%' is almost always a bad construct, an antipattern even, since every row of the table will be scanned for matches and the query will perform badly (the trailing % prevents the use of an index for lookup)... but what many may not realize is that the MySQL optimizer, when faced with multiple AND conditions, all of which must be true, tries to decide which condition will involve the least amount of work.
Once that's been decided (the "query plan"), the server (ideally, at the storage engine layer) will evaluate that condition first -- no matter which order the conditions appear in the WHERE clause -- and will then filter the resulting rows by any remaining criteria.
Fulltext indexes are considered by the optimizer to be one of the best possible choices, so it's going to find the fulltext matches, first, and then remove anything that doesn't also match the LIKE, before returning results to you.
The only conditions that immediately come to mind that will typically trump a fulltext search, when it comes to which query plan the optimizer will choose, to process first, would be an indexed column with an equality comparison and no matching value, or an IMPOSSIBLE WHERE, such as WHERE 2 < 1, both of which of course would always return 0 rows.
I want to query a table as follows:
I have a field called "category" and my input match contains N separate words. I want the query to match all rows that contain all N words, but in any order.
For example if the field category contains "hello good morning world", my input query can contain "hello morning" or "good" or "world hello" and all are matches to the query.
How do I formulate such an SQL expression?
Also it would be good if the query can be made case insensitive.
If you are using MySQL you can use the boolean fulltext search feature to achieve this. You can put a + in front of each term and then only results with all the terms, in any order, will be returned. You will need to make sure the column containing the category field has a fulltext index specified on it for this to work. Other database engines probably have similar features. So for example you might do something like the following assuming there were a fulltext index over the category column...
SELECT * FROM myTable WHERE MATCH (category) AGAINST ('+term1 +term2 +term3' IN BOOLEAN MODE);
I would avoid using the "LIKE" operator as others have suggested you would have to worry about the headache of mixed upper/lower case and if you have a large database using a % in the front of a LIKE search term is going to cause a full table scan instead of using an index which is horrible for performance.
I'm not writing the loop that will build this query for you. This will get the job done, but it will be pretty inefficient.
SELECT * FROM table
WHERE (
TOUPPER(category) LIKE '*HELLO*' AND
TOUPPER(category) LIKE '*GOOD*' AND
TOUPPER(category) LIKE '*MORNING*' AND
TOUPPER(category) LIKE '*WORLD*'
);
You could also research using REGEXes with SQL.
I want to create an autosuggest for a fulltext search with AJAX, PHP & MySQL.
I am looking for the right way to implement the backend. While the user is typing, the input field should give him suggests. Suggests should be generated from text entrys in a table.
Some information for this entrys: They are stored in fulltext, generated from PDF with 3-4 pages each. There not more than 100 entrys for now and will reach a maximum of 2000 in the next few years.
If the user starts to type, the word he is typing should be completed with a word which is stored in the DB, sorted by occurrences descending. Next step is to suggest combinations with other words, witch have a high occurrence in the entrys matching the first word. Surely you can compare it to Google autosuggest.
I am thinking about 3 different ways to implement this:
Generate an index via cronjob, witch counts occurrences of words and combinations over night. The user searches on this index.
I do a live search within the entrys with an 'LIKE "%search%"' function. Then I look for the word after the this and GROUP them by occurrence.
I create a logfile for all user searches, and look for good combinations like in 1), so the search gets more intelligent with each search action.
What is the best way to start with this? The search should be fast and performant.
Is there a better possibility I did not think about?
I'd use mysql's MATCH() AGAINST() (http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html), eg:
SELECT *
FROM table
WHERE MATCH(column) AGAINST('search')
ORDER BY MATCH(column) AGAINST('search')
Another advantage is that you could further tweak the importance of words being searched for (if neccessary), like:
MATCH(column) AGAINST('>important <lessimportant') IN BOOLEAN MODE
Or say that certain words of the search term are to be required, whilst others may not be present in the result, eg:
MATCH(column) AGAINST('+required -prohibited') IN BOOLEAN MODE
I think, the idea no 1 is the best. By the way, dont't forget to eliminate stopwords from autosuggest (an, the, by, ...).
I have a table that lists people and all their contact info. I want for users to be able to perform an intelligent search on the table by simply typing in some stuff and getting back results where each term they entered matches at least one of the columns in the table. To start I have made a query like
SELECT * FROM contacts WHERE
firstname LIKE '%Bob%'
OR lastname LIKE '%Bob%'
OR phone LIKE '%Bob%' OR
...
But now I realize that that will completely fail on something as simple as 'Bob Jenkins' because it is not smart enough to search for the first an last name separately. What I need to do is split up the the search terms and search for them individually and then intersect the results from each term somehow. At least that seems like the solution to me. But what is the best way to go about it?
I have heard about fulltext and MATCH()...AGAINST() but that sounds like a rather fuzzy search and I don't know how much work it is to set up. I would like precise yes or no results with reasonable performance. The search needs to be done on about 20 columns by 120,000 rows. Hopefully users wouldn't type in more than two or three terms.
Oh sorry, I forgot to mention I am using MySQL (and PHP).
I just figured out fulltext search and it is a cool option to consider (is there a way to adjust how strict it is? LIMIT would just chop of the results regardless of how well it matched). But this requires a fulltext index and my website is using a view and you can't index a view right? So...
I would suggest using MATCH / AGAINST. Full-text searches are more advanced searches, more like Google's, less elementary.
It can match across multiple tables and rank them to how many matches they have.
Otherwise, if the word is there at all, esp. across multiple tables, you have no ranking. You can do ranking server-side, but that is going to take more programming/time.
Depending on what database you're using, the ability to do cross columns can become more or less difficult. You probably don't want to do 20 JOINs as that will be a very slow query.
There are also engines such as Sphinx and Lucene dedicated to do these types of searches.
BOOLEAN MODE
SELECT * FROM contacts WHERE
MATCH(firstname,lastname,email,webpage,country,city,street...)
AGAINST('+bob +jenkins' IN BOOLEAN MODE)
Boolean mode is very powerful. It might even fulfil all my needs. I will have to do some testing. By placing + in front of the search terms those terms become required. (The row must match 'bob' AND 'jenkins' instead of 'bob' OR 'jenkins'). This mode even works on non-indexed columns, and thus I can use it on a view although it will be slower (that is what I need to test). One final problem I had was that it wasn't matching partial search terms, so 'bob' wouldn't find 'bobby' for example. The usual % wildcard doesn't work, instead you use an asterisk *.
This is kind of a weird question so my title is just as weird.
This is a voting app so I have a table called ballots that has a two important fields: username and ballot. The field ballot is a VARCHAR but it basically stores a list of 25 ids (numbers from 1-200) as CSVs. For example it might be:
22,12,1,3,4,5,6,7,...
And another one might have
3,4,12,1,4,5,...
And so on.
So given an id (let's say 12) I want to find which row (or username) has that id in the leading spot. So in our example above it would be the first user because he has 12 in the second position whereas the second user has it in the third position. It's possible that multiple people may have 12 in the leading position (say if user #3 and #4 have it in spot #1) and it's possible that no one may have ranked 12.
I also need to do the reverse (who has it in the worst spot) but I figure if I can figure out one problem the rest is easy.
I would like to do this using a minimal number of queries and statements but for the life of me I cannot see how.
The simplest solution I thought of is to traverse all of the users in the table and keep track of who has an id in the leading spot. This will work fine for me now but the number of users can potentially increase exponentially.
The other idea I had was to do a query like this:
select `username` from `ballots` where `ballot` like '12,%'
and if that returns results I'm done because position 1 is the leading spot. But if that returned 0 results I'd do:
select `username` from `ballots` where `ballot` like '*,12,%'
where * is a wildcard character that will match one number and one number only (unlike the %). But I don't know if this can actually be done.
Anyway does anyone have any suggestions on how best to do this?
Thanks
I'm not sure I understood correctly what you want to do - to get a list of users who have a given number in the 'ballot' field ordered by its position in that field?
If so, you should be able to use MySQL FIND_IN_SET() function:
SELECT username, FIND_IN_SET(12, ballot) as position
FROM ballots
WHERE FIND_IN_SET(12, ballot) > 0
ORDER BY position
This will return all rows that have your number (e.g. 12) somewhere in ballot sorted by position you can apply LIMIT to reduce the number of rows returned.