MariaDB FULLTEXT search with apostrophe, and mandatory words

All we need is an easy explanation of the problem, so here it is.

An apostrophe / single quote (‘) should be part of a word in a FULLTEXT index.

Admittedly I can’t find this on the MariaDB website, but I’m assuming it should still behave like MySQL (version 5.5).

But when using “IN BOOLEAN MODE”, and prefixing the word with a “+” (so it’s mandatory in all rows returned), the record is not returned.

For example:

CREATE TABLE customer (
    name TINYTEXT NOT NULL,
    FULLTEXT (name)
) ENGINE = InnoDB;

INSERT INTO customer VALUES ('O''Brien');
INSERT INTO customer VALUES ('O Brien');
INSERT INTO customer VALUES ('X''Brien');
INSERT INTO customer VALUES ('Extra Amy');
INSERT INTO customer VALUES ('Extra Brian');
INSERT INTO customer VALUES ('Extra Cat');
INSERT INTO customer VALUES ('Extra Debbie');

I get these results:

SELECT * FROM customer WHERE MATCH (name) AGAINST ("O'Brien" IN BOOLEAN MODE);
  "O'Brien"
  "O Brien"
  "X'Brien"
3 rows in set (0.000 sec)

SELECT * FROM customer WHERE MATCH (name) AGAINST ("+O'Brien" IN BOOLEAN MODE);
Empty set (0.000 sec)

SELECT * FROM customer WHERE MATCH (name) AGAINST ("+Brien" IN BOOLEAN MODE);
  "O'Brien"
  "O Brien"
  "X'Brien"
3 rows in set (0.000 sec)

SELECT * FROM customer WHERE MATCH (name) AGAINST (+"O'Brien" IN BOOLEAN MODE);
  "O'Brien"
  "O Brien"
  "X'Brien"
3 rows in set (0.000 sec)

Am I missing something, like needing to quote the apostrophe?

Or is this an intentional difference between InnoDB and MyISAM?


Thanks to @rick-james, I’ve updated this question to include 3 “extra” records to avoid the 50% threshold limit, and I’ve included an example where the + is before the quoted word.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

If we create the table with the Aria or MyISAM storage engine, then the query succeeds:

CREATE TABLE customer2 (
    name TINYTEXT NOT NULL,
    FULLTEXT (name)
) ENGINE = Aria;

INSERT INTO customer2 VALUES ('O''Brien');

SELECT * FROM customer2 WHERE MATCH (name) AGAINST ("+O'Brien" IN BOOLEAN MODE);
1 row in set (0.001 sec)

CREATE TABLE customer3 (
    name TINYTEXT NOT NULL,
    FULLTEXT (name)
) ENGINE = MyISAM;

INSERT INTO customer3 VALUES ('O''Brien');

SELECT * FROM customer3 WHERE MATCH (name) AGAINST ("+O'Brien" IN BOOLEAN MODE);
1 row in set (0.001 sec)

However, InnoDB doesn’t seem to work for me either (MariaDB 10.4.8.)

So that leads me to think this is a bug. I reported the issue here:
https://jira.mariadb.org/browse/MDEV-20797

Method 2

Works for me in both MyISAM and InnoDB.

But note: out of 185,082 rows, 17 have “O’Brien”. That’s less than 50%, so the search term is not ignored. I verified the count with LIKE "%O'Brien%".

But, when adding the +, it needs to be outside the quotes for InnoDB. (Yet another difference between the implementations.)

(I have updated my blog to add this diff.)

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply