nicolaslopezj / searchable

A php trait to search laravel models
MIT License
2.01k stars 291 forks source link

Search Request Having Space Not Displaying The Expected Result #219

Closed lizeshakya closed 4 years ago

lizeshakya commented 4 years ago

I implemented SearchableTrait to Product Model.

My request search value is : "Nach g"

Expected Result:

Output Result:

The search seemed to split one word into two words having " " (space)

SQL used from the searchable

select count(*) as aggregate from (select `products`.*, max((case when LOWER(`products`.`name`) LIKE 'nach' then 150 else 0 end) + (case when LOWER(`products`.`name`) LIKE 'g' then 150 else 0 end) + (case when LOWER(`products`.`name`) LIKE 'nach%' then 50 else 0 end) + (case when LOWER(`products`.`name`) LIKE 'g%' then 50 else 0 end) + (case when LOWER(`products`.`name`) LIKE '%nach%' then 10 else 0 end) + (case when LOWER(`products`.`name`) LIKE '%g%' then 10 else 0 end) + (case when LOWER(`products`.`address`) LIKE 'nach' then 90 else 0 end) + (case when LOWER(`products`.`address`) LIKE 'g' then 90 else 0 end) + (case when LOWER(`products`.`address`) LIKE 'nach%' then 30 else 0 end) + (case when LOWER(`products`.`address`) LIKE 'g%' then 30 else 0 end) + (case when LOWER(`products`.`address`) LIKE '%nach%' then 6 else 0 end) + (case when LOWER(`products`.`address`) LIKE '%g%' then 6 else 0 end) + (case when LOWER(`products`.`description`) LIKE 'nach' then 75 else 0 end) + (case when LOWER(`products`.`description`) LIKE 'g' then 75 else 0 end) + (case when LOWER(`products`.`description`) LIKE 'nach%' then 25 else 0 end) + (case when LOWER(`products`.`description`) LIKE 'g%' then 25 else 0 end) + (case when LOWER(`products`.`description`) LIKE '%nach%' then 5 else 0 end) + (case when LOWER(`products`.`description`) LIKE '%g%' then 5 else 0 end)) as relevance from `products` where `products`.`deleted_at` is null group by `products`.`id` having relevance >= 7.00 order by `relevance` desc) as `products`

Model

Product.php

use Searchable;

protected $searchable = [
        /**
         * Columns and their priority in search results.
         * Columns with higher values are more important.
         * Columns with equal values have equal importance.
         *
         * @var array
         */
        'columns' => [
            'products.name' => 10,
            'products.address' => 6,
            'products.description' => 5,
        ],
    ];
lizeshakya commented 4 years ago

It was added to the documentation itself.

Entire Text Search