Optimizing auto-complete FULLTEXT SQL query - php

I have the following query which is used in order to do an auto-complete of a search box:
SELECT *, MATCH (screen_name, name) AGAINST ('+query*' IN BOOLEAN MODE) AS SCORE
FROM users
WHERE MATCH (screen_name, name) AGAINST ('+query*' IN BOOLEAN MODE)
ORDER BY SCORE DESC LIMIT 3
I also have a FULL TEXT index on screen_name & name (together). When this table was relatively small (50k) this worked great. Now the table is ~200k and it takes seconds(!) to complete each query. I'm using MySql MyISAM. Is this reasonable? What directions might I check in order to improve this as surely it doesn't satisfy the needs of an auto-complete query.

MYSQL Match against is really slow, you should look into alternatives like Sphinx Search Server.

Related

Full text search query generating error, why?

Why do I get a MySQL error from this query?
SELECT *, MATCH($SW) AGAINST(reb, keb, gloss) as Score
FROM dictionary
WHERE MATCH($SW) AGAINST(reb, keb, gloss)
ORDER BY Score DESC;
I'm trying to test Full Text search but I can't create a working query it seems like.
It would be nice if you gave the error message, but I think I can guess the fix. You forgot to quote the strings:
SELECT *, MATCH('$SW') AGAINST(reb, keb, gloss) as Score
FROM dictionary
WHERE MATCH('$SW') AGAINST(reb, keb, gloss)
ORDER BY Score DESC;

Sorting mysql results on the basis of relevance to query

I'm searching mysql database using user's query with php via html form.
SELECT * FROM Collection WHERE `Name` LIKE '%$query%';
I want to sort the results with the row having highest matching score on the top in decreasing order with matching score.
How to do this?
You must take a look at FULLTEXT search in Boolean mode.
Boolean Full-Text Searches
SELECT * FROM Collection WHERE `Name` LIKE '%$query%' ORDER By name ASC;
use the above query for sorting

PHP search on keywords

I have a table in a database with records containing keywords as well as other data. What would be a logical way to create a search function that would allow people to search based on keywords, and order results based on the number of matched keywords?
Mysql provide FULLTEXT search options. Check this link mysql full text search. These search results will be sorted according to best match. it also has support for boolean mode and NATURAL LANGUAGE MODE(default). You need to add FULLTEXT index on search column.
Here is the query that will work for you.
SELECT *, MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE) AS relevancy
FROM table_name
WHERE MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE)
ORDER BY relevancy DESC;

MySQL match against - IN BOOLEAN MODE?

I'm using PDO to execute a MATCH AGAINST query.
The following returns nothing:
SELECT title, author, isbn, MATCH(title, isbn) AGAINST (:term) AS score
FROM books
WHERE MATCH(title, isbn) AGAINST (:term)
ORDER BY score DESC LIMIT 0,10
Where as this returns perfectly:
SELECT title, author, isbn, MATCH(title, isbn) AGAINST (:term) AS score
FROM books
WHERE MATCH(title, isbn) AGAINST (:term IN BOOLEAN MODE)
ORDER BY score DESC LIMIT 0,10
Could anyone tell me why IN BOOLEAN MODE is making such a difference, and whether or not I should be using it in my query?
The second query is running as a "natural language search" as that is the default when no natural language search type is specified. This type of search filters additionally filters out words that are present in 50% or more of the rows automatically.
"IN BOOLEAN MODE" does do this additional filtering, and thus, may return matches if you are searching on a common term.
Whether or not you should be using a boolean search depends on what the specifics of your situation and cannot be determined without more information. However, some considerations may include, size of the input data set vs how large of a matching dataset you want returned and whether you want to return results for words that occur frequently.
(Ref: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html)

PHP MySQL Search And Order By Relevancy

I know how to do a regular php mysql search and display the results. However, because of the nature of what I'm trying to accomplish I need to be able to sort by relevancy. Let me explain this better:
Normal Query "apple iphone applications" will search the database using %apple iphone application%, but if there aren't records which display that phrase in that exact order the search will produce nothing.
What I basically need to do is search for 'apple', 'iphone' and 'applications' all separately and then merge the results into one, and then I need to grade the relevancy by how many instances of the word are found in the records. For example if I did what I wanted to do and it returned them following:
Iphone Applications From Apple
Apple Make The Best Apple Iphone Applications
Iphone Applications
They would rank as follows:
Apple Make The Best Apple Iphone Applications
Iphone Applications From Apple
Iphone Applications
Because of how many instances of the search terms are found. See highlighted:
[Apple] Make The Best [Apple] [Iphone] [Applications]
[Iphone] [Applications] From [Apple]
[Iphone] [Applications]
I hope I have explained this well enough and I would be extremely grateful if anyone could give me any pointers.
take a look at the MySQL FULLTEXT search functions,
These should automatically return results by relevancy, and give you much more control over your searches
The only potential issue with using fulltext indexes is that they aren't supported by InnoDB tables.
Maybe this might help someone (order by relevance and keep word count):
None full-text:
SELECT *, ( (value_column LIKE '%rusten%') + (value_column LIKE '%dagen%') + (value_column LIKE '%bezoek%') + (value_column LIKE '%moeten%') ) as count_words
FROM data_table
WHERE (value_column LIKE '%dagen%' OR value_column LIKE '%rusten%' OR value_column LIKE '%bezoek%' OR value_column LIKE '%moeten%')
ORDER BY count_words DESC
Full-text:
SELECT * FROM data_table
WHERE MATCH(value_column) AGAINST('+dagen +rusten +bezoek +moeten' IN BOOLEAN MODE)
ORDER BY MATCH(value_column) AGAINST('+dagen +rusten +bezoek +moeten' IN BOOLEAN MODE) DESC;
A quick google gave me this link.
Example:
select title, match (title,content) against (”internet”) as score
from cont
where match (title,content) against (”internet”) limit 10;
SELECT field2, field3, ..., MATCH(field1, field2) AGAINST ("search string") AS relevance WHERE MATCH(field1, field2) AGAINST "search string" ORDER BY relevance DESC LIMIT 0,10
In the result set, there will be a field "relevance", which is used here to sort the results.
I Don't What exactly you want but the following code definitely work for you.
SELECT ("some text here" or `column_name`) RLIKE "Apple|Iphone|Application" AS Result ORDER BY Result DESC;
Separate all words with Bar(|) but results will be 1 or 0 founded or not resp.
If you want to get founded rows see below.
SELECT * FROM "table_name" WHERE `column_name` RLIKE "Apple|Iphone|Application";

Categories