I want to find all rows that match a full-text search for one pair of columns but also do not match the same text in another column.
Both of these seem to work
SELECT * FROM docs WHERE MATCH(title, descript) AGAINST ('energy' IN BOOLEAN MODE) AND NOT MATCH(categories) AGAINST ('energy' IN BOOLEAN MODE);
Or using a subquery:
SELECT * FROM docs WHERE MATCH(title, descript) AGAINST ('energy' IN BOOLEAN MODE) AND id NOT IN (SELECT id FROM docs where MATCH(categories) AGAINST ('energy' IN BOOLEAN MODE));
The docs field has the relevant full-text indexes set up.
Any reason to prefer one over the other?
On the (small) database I'm using they are both very fast, too fast to measure reliably.
Thanks for any suggestions.
Related
I have table with quite many rows (~2M).
When i search it like
SELECT * FROM product WHERE
MATCH(name) AGAINST('+some' '+word' IN BOOLEAN MODE)
it works like charm and finds what i need in less than 0.5s.
But when i search for 2 sets of words like this
SELECT * FROM product WHERE
MATCH(name) AGAINST('+some' '-word' IN BOOLEAN MODE)
OR
MATCH(name) AGAINST('+something' '-other' IN BOOLEAN MODE)
search takes sometimes over minute.
I would expect it to work 2 times slower (it's 2 searches), maybe a bit more (you still have to compare results and remove duplicates, but if there are only few results it should not take long), but not so much longer. After adding OR it works slower, than LIKE "%...%" OR LIKE "%...%"
Anyone can tell me what am i doing wrong?
Unfortunately for you, fulltext indexes have some limitations, and not being able to properly merge the results of two independent fulltext searches is one them:
The Index Merge optimization algorithm has the following known limitations:
[...]
Index Merge is not applicable to full-text indexes.
Fortunately for you, fulltext searches can be more complex, so you can merge your searches yourself. Your second query can be written as a single search using:
SELECT * FROM product WHERE
MATCH(name) AGAINST('(+something -other) (+some -word)' IN BOOLEAN MODE)
This defines two search sets and is ok if either of the two (...) matches - which is an or.
Alternatively, you can use a union instead of an or, which allows MySQL to actually run two independent fulltext searches and then combine the two results, which is basically what you are thinking of:
SELECT * FROM product WHERE
MATCH(name) AGAINST('+some -word' IN BOOLEAN MODE)
UNION
SELECT * FROM product WHERE
MATCH(name) AGAINST('+something -other' IN BOOLEAN MODE)
This also works for more complicated situations, e.g. merging searches on different columns, but will not work that easy if you want to do something else than or.
I want to search in a MySQL table in two columns for one or more keywords.
I tried this query:
select *
from table
where match(col1, col2) against ('keyword1 keyword 2' with query expansion)
But this query does not always gives the right results. Any suggestions?
May be this helps:
select *, match(col1, col2)
against ('keyword1 keyword2' in boolean mode) as relevance
from content
where match(col1, col2)
against ('keyword1 keyword2' in boolean mode)
order by relevance;
Compare results of this query and the one you wrote above.
Note that using WITH QUERY EXPANSION results in a more fuzzy search. From the documentation:
"It works by performing the search twice, where the search phrase for the second search is the original search phrase concatenated with the few most highly relevant documents from the first search."
How can I do a MySQL search which will match partial words but also provide accurate relevancy sorting?
SELECT name, MATCH(name) AGAINST ('math*' IN BOOLEAN MODE) AS relevance
FROM subjects
WHERE MATCH(name) AGAINST ('math*' IN BOOLEAN MODE)
The problem with boolean mode is the relevancy always returns 1, so the sorting of results isn't very good. For example, if I put a limit of 5 on the search results the ones returned don't seem to be the most relevant sometimes.
If I search in natural language mode, my understanding is that the relevancy score is useful but I can't match partial words.
Is there a way to perform a query which fulfils all of these criteria:
Can match partial words
Results are returned with accurate relevancy
Is efficient
The best I've got so far is:
SELECT name
FROM subjects
WHERE name LIKE 'mat%'
UNION ALL
SELECT name
FROM subjects
WHERE name LIKE '%mat%' AND name NOT LIKE 'mat%'
But I would prefer not to be using LIKE.
The new InnoDB full-text search feature in MySQL 5.6 helps in this case.
I use the following query:
SELECT MATCH(column) AGAINST('(word1* word2*) ("word1 word1")' IN BOOLEAN MODE) score, id, column
FROM table
having score>0
ORDER BY score
DESC limit 10;
where ( ) groups words into a subexpression. The first group has like word% meaning; the second looks for exact phrase. The score is returned as float.
I obtained a good solution in this (somewhat) duplicate question a year later:
MySQL - How to get search results with accurate relevance
I am using the latest version of MySQL 5.5.
I have a fulltext index spanning multiple columns in a table generated specifically for fulltext search (other tables in the database uses innodb):
somedata_search
========
id
name
about
note
dislike
I have a fulltext index on all the columns except for ID. I am able to run fulltext searches using:
SELECT * FROM account_search WHERE MATCH(name, about, note, dislike) AGAINST('mykeyword*' IN BOOLEAN MODE);
This all works fine, but is there a way to deteremine which column the match originates from for each row? If there are matches across columns in a row, I am happy to have just the first column.
I don't think there is any "native" way of getting it but it is possible to do it anyway.
I'm not sure this is fast but it returns the correct data
select text_test.*,
match(name) against ('dude' in boolean mode) as name_match,
match(info) against ('dude' in boolean mode) as info_match
from text_test
where match(name, info) against ('dude' in boolean mode);
http://sqlfiddle.com/#!2/5159c/1
I have a table with names of movies, and I want to be able to search for a movie in that table. But I want to be able to search for part of the title, and still return a result. For example, if there is a record with the name "The quantum of solace", then I want to be able to do a search for "quantum solace", or even "007: quantum solace" and I want to find that record. Is there a way to do this?
EDIT
And how do I sort according to the matches? That is, the row that matches the most, should be returned first.
Use a MySQL Full Text Search in boolean mode.
If you do this when you search for '007: quantum solace' as it contains at least one matching result in the column it will be displayed, you can then order by relevancy.
SELECT *, MATCH(title) AGAINST ('quantum solace' IN BOOLEAN MODE) AS rank
FROM films
WHERE MATCH(title) AGAINST ('quantum solace' IN BOOLEAN MODE) ORDER BY rank DESC
Have a look at the full text search capabilities of MySQL. Once you set up a full text index, you can do queries like this one:
SELECT *
FROM movies
WHERE MATCH(title) AGAINST ('quantum solace' IN BOOLEAN MODE)