2

I'm having an issue with my fulltext search query and I've pretty much gone through all the forums and threads I could find but I'm still having this issue.

I am using the MySQL Fulltext Boolean Mode search to return matching rows based on two columns (artist name and track title). The user can enter any phrase they wish into the search bar, and the results are supposed to be only rows that contain ALL parts of the search query in EITHER of the columns.

This is the query I have so far, and it works with most queries but for some it return results too loosely and I'm not sure why.

SELECT * FROM tracks WHERE MATCH(artist, title) AGAINST('+paul +van +dyk ' IN BOOLEAN MODE)

This query however, returns rows containing Paul, without the 'Van' or 'Dyk'. Again, I want the query to return only rows that contain ALL of the keywords in EITHER the Artist or Track Name column.

Thanks in advance

2 Answers 2

5

To enhance sorting of the results in boolean mode you can use the following:

SELECT column_names, MATCH (text) AGAINST ('word1 word2 word3')
AS col1 FROM table1
WHERE MATCH (text) AGAINST ('+word1 +word2 +word3' in boolean mode) 
order by col1 desc;

Using the first MATCH() we get the score in non-boolean search mode (more distinctive). The second MATCH() ensures we really get back only the results we want (with all 3 words).

So your query will become:

SELECT *, MATCH (artist, title) AGAINST ('paul van dyk')
    AS score FROM tracks
    WHERE MATCH (artist, title) 
    AGAINST ('+paul +van +dyk' in boolean mode) 
    order by score desc;

Hopefully; you will get better results now.

If it works or do not work; please let me know.

2
  • So you're saying the keywords should be left as-is and be put in double quotes to be treated as one? What would happen then if the order of the keywords doesn't match, so the user enters 'Van dyk paul' instead? Ideally that should also return results with 'Paul van dyk'. I'm not sure if your method would take care of that. Commented Mar 11, 2015 at 2:04
  • 1
    Yes. Any order of keywords shall give you same results. Please check my edited answer and let me know if it gives you better results.
    – Tushar
    Commented Mar 11, 2015 at 11:07
0

Piggybacking off of @Avidan's answer. These are the operations that can be performed on full-text searches to help create the query you desire.

The following examples demonstrate some search strings that use boolean full-text operators:

'apple banana'

Find rows that contain at least one of the two words.

'+apple +juice'

Find rows that contain both words.

'+apple macintosh'

Find rows that contain the word “apple”, but rank rows higher if they also contain “macintosh”.

'+apple -macintosh'

Find rows that contain the word “apple” but not “macintosh”.

'+apple ~macintosh'

Find rows that contain the word “apple”, but if the row also contains the word “macintosh”, rate it lower than if row does not. This is “softer” than a search for '+apple -macintosh', for which the presence of “macintosh” causes the row not to be returned at all.

'+apple +(>turnover

Find rows that contain the words “apple” and “turnover”, or “apple” and “strudel” (in any order), but rank “apple turnover” higher than “apple strudel”.

'apple*'

Find rows that contain words such as “apple”, “apples”, “applesauce”, or “applet”.

'"some words"'

Docs: https://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.