Emmanuel-Ab
Emmanuel-Ab

Reputation: 331

Why am I not able to search short words in my Laravel Application?

This is my search function. With it I am able to search whole words and numbers perfectly. However, I am not able to search short words like 'the'.

public function search($search)
 {
        $goodString = $this->makeString($search);
        $search_results = Model::select('*')->selectRaw('MATCH(title) AGAINST(? IN NATURAL LANGUAGE MODE) AS relevance', [$goodString])->whereRaw('MATCH(title) AGAINST(? IN NATURAL LANGUAGE MODE)', [$goodString])->orderBy('relevance', 'desc');

        return $search_results;
 }

I am using:

mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper

Ubuntu 16.04

I made changes to the mysql config /etc/mysql/my.cnf and set the following:

[mysqld]
innodb_ft_min_token_size = 2
ft_min_word_len = 2

After that I restarted the mysql servel and went on to rebuild my Fulltext tables with the following commands in mysql:

set GLOBAL innodb_optimize_fulltext_only=ON;
OPTIMIZE TABLE table_name;

Then I restarted the mysql server again and nothing. No results when I search "the", "of", "at" etc.

So my question is, what should I do in order to be able to search short words and numeric values like '10' or '20' etc?

Thank you in advance!

EDIT:

This is how I created the fulltext search via Laravel:

On migrating I added the following at the end of the Schema::create function

DB::statement('ALTER TABLE news ADD FULLTEXT search(title)');

Also, if there is double number in the title like "13" I can't get a results when I search for 13.

A follow up question:

Is it possible to search for partial match word in FULLTEXT search? At the moment I break the words of the string and make string such as "+search +string +here" is it possible to find a match if a word "searched" exists in the titles?

Upvotes: 1

Views: 322

Answers (2)

Mihai
Mihai

Reputation: 26784

The words you mention are known as stop words (soo frequent that are ignored)

https://dev.mysql.com/doc/refman/8.0/en/fulltext-stopwords.html#fulltext-stopwords-stopwords-for-innodb-search-indexes

You can create a new (shorter) list and use that.Make sure you change the documentation to your Mysql version.

Upvotes: 2

Julius Fasema
Julius Fasema

Reputation: 902

to effect a search of words in your sentences, use this:

preg_match("/{$wordsOrSentenceFromDB}/i", $WordsYouEnteredFromForm);

Upvotes: 0

Related Questions