teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

tntsearch relation model with column name as 'name' showing only one result #231

Closed Penjor closed 3 months ago

Penjor commented 5 years ago

I don't know whether its a laravel scout problem or tntsearch problem. The issue is that I have references to several other relational models in the toSearchableArray function in the Post Model but the related models with the field name 'name' are not being indexed entirely (only first record is getting indexed). I have tried other columns and they are getting indexed. Then I figured it was the column with name as 'name' had problems. So I changed the column name of one of the related models to something else and it got indexed and appears in the search results as expected. However, doing that requires changing all model's name column and all references to the name column.

Here is my scout settings:
'tntsearch' => [ 'storage' => storage_path(), //place where the index files will be stored 'fuzziness' => env('TNTSEARCH_FUZZINESS', false), 'fuzzy' => [ 'prefix_length' => 2, 'max_expansions' => 50, 'distance' => 2 ], 'asYouType' => false, 'searchBoolean' => env('TNTSEARCH_BOOLEAN', false), ],

And below are the lines I added in Post Model: use Searchable;//for laravel scout

public $asYouType = true;

/**
 * Get the indexable data array for the model.
 *
 * @return array
 */
public function toSearchableArray()
{
    //$array = $this->toArray(); //this line would index every column as searchable

    $array = $this->only('id', 'title', 'content');

    $related1=[];
    if(!is_null($this->companies)){
        $related1 = $this->companies->only('acronym','name');
    }
    $related2=[];
    if(!is_null($this->qualification)){
        $related2 = $this->qualification->only('name');
    }
    $related3=[];
    if(!is_null($this->manufacturer)){
        $related3 = $this->manufacturer->only('name');
    }
    $related4=[];
    if(!is_null($this->model)){
        $related4 = $this->model->only('name');
    }
    $related5=[];
    if(!is_null($this->propertyType)){
        $related5 = $this->propertyType->only('name');
    }

    // Customize array...
    //return $array;
    return array_merge($array, $related1, $related2, $related3, $related4, $related5);
}

public function shouldBeSearchable()
{
    $isPublished = true;
    if($this->status != 1) {
        $isPublished =false;
    }

    return $isPublished;
}

Any help will be appreciated.

Penjor commented 5 years ago

I forgot to mention it was working the first time when I indexed. All posts with the company name was displayed in the result. Then I had to reindex the Post model through php artisan and the problems occurred. The only change I made was change the asYouType to true from false in the scout.php file. However, reverting the change and reindexing doesn't work now. And then I discovered the column name problem as stated above.

akizor commented 4 years ago

Hey, i've came across the same issue. However, it's something strange. If i save the model, gets indexed properly including relations. If i use the CLI command, it doesn't.