A
A
alabs2020-02-12 18:46:56
PHP
alabs, 2020-02-12 18:46:56

How to set PHP + MySQL search relevancy?

Good afternoon.
Search example - https://4ip.info/
Enter "iphone 6", returns everything containing iphone 6 or iphone 6s , but does not return, for example, from the string, iPhone 7 1960 mAh, orig.
The same goes for "6 iphone" .

Searching for structures like '%iphone%' and like '6' will find a bunch of extra stuff. Full-text search generally scores on the number 6. Sphinx will also chew the figure, most likely.

There is an idea with a dictionary, but I hope that it is possible to organize it somehow easier.

I would appreciate suggestions, thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alabs, 2020-02-15
@alabs

I solved the problem in the framework of php + mysql. Search is not loaded on the site. Here is the solution:
Started a dictionary of models.

<?
  $slovar = array(
    'iphone 6' => 'iphone 6',
    '6 iphone' => 'iphone 6',
    'iphone 6s' => 'iphone 6s',
    '6s iphone' => 'iphone 6s',
    'iphone 7' => 'iphone 7',
    '7 iphone' => 'iphone 7',
    'iphone 7s' => 'iphone 7s',
    '7s iphone' => 'iphone 7s',
  );

Further, in the search line, I replace all phrases with phrases where instead of a space, an underscore. After breaking the search query into words, I return spaces to their place.
$tmp = substr( $tmp, 0, 40 ); //обрезаем строку
//делаем массив с подчеркиваниями
    foreach($slovar as $k=>$v){
      $arr1[] = $k;
      $arr2[] = str_replace(" ", "_", $v);
    } 
//заменяем в поисковом запросе словосочетания
    $tmp =  str_replace($arr1, $arr2, $tmp);
//бьем поисковую строку  на пробелы
    $tmp = explode( ' ', $tmp );
//пробелы заменяем на конструкцию SQL
    $where = "`title` LIKE '%" . implode ( "%' AND `title` LIKE '%", $tmp ) . "%'";
//возвращаем в запрос обратно пробелы
    $where =  str_replace("_", " ", $where);

I
Ivan Shumov, 2020-02-12
@inoise

Sphinx will also chew the figure, most likely.

If you don't try, you won't know.
In general, Elasticsearch is used for such tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question