I would like to tweak the results returned by this Fulltext-query:
$STH = $DBH->prepare('SELECT *,
MATCH (title,title_under,subject) AGAINST (:query) AS score
FROM articles
WHERE MATCH(title,title_under,subject) AGAINST(:query IN BOOLEAN MODE)
order by score desc');
Is there a way to return the score calculated by mysql so that I can run my own conditions for adding/subtracting points before parsing the results?
Yes, the "MATCH() AGAINST() AS score" in your SELECT-statement will already do just that, it'll return the score calculated by MySQL.
I notice you executing the FT search IN BOOLEAN MODE in the WHERE-clause, but not in the SELECT however.
Related
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
I would like to ask if anybody can help me with this query
SELECT count(MATCH(product_text) AGAINST('lorem*' in boolean mode)) AS score FROM table_products WHERE MATCH (text) AGAINST ('lorem*' in boolean mode) limit 0,50000
There is fulltext index on column text. A.m. query return sum of rows as score.
What I wont is to count fulltext search results. If the number of results(rows)
is higher than 50000, than count-sum 50000 will be returned, otherwise the exact count of
of results is returned.
Problem is that it is not fast on table width 1,5 million rows if user try to find for example word "lorem" and this word appears in table f.e. more than 500 000 x.
I tried also
SELECT id,name,product_text,price, MATCH(product_text) AGAINST('lorem*' in boolean mode) AS score FROM table_products WHERE MATCH(product_text) AGAINST('lorem*' in boolean mode) and show_product='1' limit 0,50000
... width php mysql_num_rows
Another problem is that in following query mysql use fulltext index only
and sort by another column or by score is than slow
SELECT id,name,product_text,price, MATCH(product_text) AGAINST('lorem*' in boolean mode) AS score FROM table_products WHERE MATCH(product_text) AGAINST('lorem*' in boolean mode) and show_product='1' order by score desc limit 0,50000
resp. (..order by name, order by price)
Is there another better and faster way?
Many thanks for any help.
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.
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)
I'm trying to get fulltext searches to be sorted by relevance in a
Doctrine_RawSql query.
This code will perform the search:
$q = new Doctrine_RawSql();
$q->select('{p.*}')
->from('cms_page p')
->where('match(p.content) against (?)', $user_query)
->addComponent('p', 'CmsPage p');
This will execute. I would like the results to be sorted by relevance
The real sql would have to look something like:
select
p.id,
match(p.content) against (?) as score
from
cms_page as p
order by
score desc;
So I need to get that match ... against clause in the select... I think.
My crapshoot guess at accomplishing this was:
$q->select("{p.id}, match({p.content}) against ('$escaped_user_query') as score")
->from('cms_page p')
->orderBy('score DESC')
->addComponent('p', 'CmsPage p');
That doesn't work. Any pointers?
Thanks in advance!
According to the MySQL fulltext natural language search docs:
When MATCH() is used in a WHERE clause, as in the example shown earlier, the rows returned are automatically sorted with the highest relevance first. Relevance values are nonnegative floating-point numbers. Zero relevance means no similarity. Relevance is computed based on the number of words in the row, the number of unique words in that row, the total number of words in the collection, and the number of documents (rows) that contain a particular word.
Does this not work for you?
You can read more about natural fulltext search in MySQL here.