The ideal in this case is to use the FULLTEXT search method instead of LIKE.
Vala point out that this will not fetch the broken words, IE, he continues to search for words but what will define whether it will be displayed or not the result is the set, the sentence in the case.
In the example you used, as the two records contain the word 'chocolate' they will be displayed, regardless of biscuit being in the singular or plural.
To use this feature you need to make changes to the database structure and add the fulltext indexes in the columns you want to perform the search, nothing too complicated. I’ll give you an example.
I will create a table just to test the result:
Note that I have already created fulltext indexes in the columns where I want to perform searches
CREATE TABLE receitas
(
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
titulo VARCHAR(200),
corpo TEXT,
FULLTEXT (titulo, corpo)
);
Now I’ll add some similar singular and plural fields:
INSERT INTO receitas (`titulo`, `corpo`) VALUES ('biscoito de chocolate', 'descricao da receita');
INSERT INTO receitas (`titulo`, `corpo`) VALUES ('biscoitos de chocolate', 'descricao da receita');
INSERT INTO receitas (`titulo`, `corpo`) VALUES ('cookies de chocolate', 'descricao da receita');
INSERT INTO receitas (`titulo`, `corpo`) VALUES ('cookie de banana', 'descricao da receita');
Query search. I put a variable to simulate your:
set @pesquisa := 'biscoito de chocolate';
SELECT titulo, corpo FROM receitas
WHERE MATCH(titulo) AGAINST (@pesquisa in natural language mode);
This search will return the two cookies, both singular and plural, but will also return the 'chocolate cookies'.
MATCH returns a relevant calculation of your search, which is very cool for you to sort the results for example. I’ll give you an illustrative example just so you understand how it works:
SELECT titulo, corpo, MATCH(titulo, corpo) AGAINST (@pesquisa in natural language mode) AS score
FROM receitas
WHERE MATCH(titulo) AGAINST (@pesquisa in natural language mode)
ORDER BY score DESC;
But even more interesting is when creating tables of this type already create a column with keywords, which the user fills when entering the record in the database, so it would put the words in the singular in the plural and add any keyword that is relevant to the search. Then just direct the search to that column of Keywords. This would create a much more powerful search system for your system.
Divides the term by word (e.g., delimiter " ") and removes the last two characters of each word from the term, because the plurar is not always a s.
– Claudio Lopes
then, but then in the case of chocolate cookies will not help, because what is plural is the cookie
– Leandro Marzullo
I’ll edit the answer above to make it simpler
– Claudio Lopes