How to make a fulltext search

524 views Asked by At

I want to make a fulltext search with metaphone. Everythings works fine. I have 4 fields ie.

ID |Category     | Type          |Title                        |Meta
1  |Vehicle      |4 Wheelers     |Farrari Car for Sale         |FHKL WLRS FRR KR FR SL
2  |Real Estate  |Residential Apt|3BHK for sale                |RL ESTT RSTN APT BK FR SL
3  |Music        |Instruments    |Piano for sale               |MSK INST PN FR SL
4  |Stationary   |College        |Bag for $50                  |STXN KLJ BK FR
5  |Services     |Job            |Vacancy for Jr.Web Developer |SRFS JB FKNS FR JRWB TFLP

The above is the sample data. Here I want to use metaphone and fulltext search using match()against(). Everything works fine. However Some words like Bag, Job and Car are ignored as the default minimum character 4. The issue is now that I use shared hosting and the hosting provider has told me that he cannot provide me a mysql config file nor can they change this so doing this in config file ft_min_word_len = 2 is not an option.

//Code for generating metaphone

<?php 
    $string = "Vacancy for Jr.Web Developer";
    $a = explode(" ", $string);
    foreach ($a as $value) {
        echo metaphone($value,4)."<br>";
    }
?>

I am using normal

SELECT * FROM tbl_sc WHERE MATCH(META) AGAINST('$USER_SEARCH');

All the information in the database are user generated so I cannot supervise. Since I use mysql, PHP and on a shared hosting. I cannot use any elastic search library or solr like things. I have searched google and stack overflow however I am not able to get anything

One options is using LIKE operator but I want to use MATCH() AGAINST() if possible.

Kindly help me out with some work around or alternate route.

1

There are 1 answers

2
demon Time On

first there are three types of fulltext searches

Natural language full text search

Boolean fulltext searches

Query expansion searches

what suits your question here is the natural language full-text search, since your queries are mostly in free language and uses no special characters.the syntax goes like this

SELECT * FROM table_name WHERE MATCH(col1, col2)
AGAINST('search terms' IN NATURAL LANGUAGE MODE)

in your case first, add the fulltext functionality to your table

$stmt_txt_search = $conn->prepare("ALTER TABLE tbl_sc ADD FULLTEXT (Category, Type, Title, Meta)");
$stmt_txt_search->execute();

your query should be something like this

$stmt_match = $conn->prepare("SELECT * FROM tbl_sc WHERE MATCH (Meta) AGAINST(? IN NATURAL LANGUAGE MODE)");
$stmt_match->bind_param("s",$USER_SEARCH);
$stmt_match->execute();

to alter the ft_min_word_len you have to go the the my.cnf file, change it to the desired value, restart the server and rebuild your indexes like so

[mysqld]
set-variable = ft_min_word_len=3

then

mysql> ALTER TABLE tbl_sc DROP INDEX Title, Category...;
mysql> ALTER TABLE tbl_sc ADD FULLTEXT Title, Category...;

but since you are in shared hosting account, you cannot access the my.cnf file. However, you using SHOW VARIABLES and INFORMATION SCHEMA you can see all set variables and even change them using SET in your session such that all db connections will be based on the newly set values

for instance to SHOW VARIABLES in sql you can use

SELECT * FROM information_schema.global_variables; this shows all existing variables in your current session, for a variable like flush time it can be set to 1 using SET flush_time = 1; so now the database will have a flushtime of 1 onwards, in your case i suppose the variables ft_max_word_len and ft_min_word_len are dynamically changeable and i would therefore suggest trying

SET ft_min_word_len = 2; within your current session, for more information see server system variables