Laravel Explorer configuration

43 views Asked by At

I have a hard time configuring Laravel Explorer in a way I want. The main idea, why I use elasticsearch is the productsearch. As every product search on the internet, it should have an autocomplete feature (for example, if I search for "pro," it should find "product"), and weighting feature (for example, prioritizing title over description).

For the autocomplete I find out that may the edge_ngram tokenizer should help, but I can't configure in the indexSettings() function, where I believe it should be configured, and I don't find anything about this in the documentation, except a github discussion, but that either don't help.

Regarding weighting the only thing what I find usefull is the boost, but this don't work properly also, and it's a little confusing for me what is the difference between boost and weight and why I can't define weights for columns, to prioritize them.

Here is how I am attempting to configure my model:

class Product extends Model implements Explored, Aliased, IndexSettings
{
    use HasFactory,Searchable;

    protected $fillable = ['name', 'sku', 'description'];

    public function mappableAs(): array
    {
        return [
            'id' => 'keyword',
            'name'=> [
                'type' => 'text',
                'analyzer' => 'nGram_analyzer',
                'search_analyzer' => 'standard',
            ],
            'sku' => 'text',
            'description' => 'text',
        ];
    }

    public function toSearchableArray()
    {
        return[
            'id' => $this->id,
            'name' => $this->name,
            'description' => strip_tags($this->description),
            'sku' => $this->sku
        ];
    }

    public function indexSettings(): array
    {
        return [
            'settings' => [
                'analysis' => [
                    'tokenizer' => [
                        'nGram_tokenizer' => [
                            'type' => 'nGram',
                        ],
                    ],
                    'analyzer' => [
                        'nGram_analyzer' => [
                            'type' => 'custom',
                            'tokenizer' => 'nGram_tokenizer',
                            'filter' => [
                                'lowercase',
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }
}

I'm strict to Elasticsearch, so if somehow is possible to make this work it would be great. Also I tried out TNTSearch but that also don't works as I expected, after a little workaround.

At this point I'm not event sure that is this a good approach, any idea or experience in writing product search would be usefull, is that real that hard?

1

There are 1 answers

0
Telexx On

I succeeded to configure the edge_ngram analyzer, and with the boost I earn the desired searching effect. The edge_ngram analyzer solve the autocomplete missing problem.

Note: The Scout::flush command don't modify the index, it just removes the records from it, so I have to define a new index in the searchableAs function each time I modify the index settings

My model what works well for me:

class ... Model implements Explored, IndexSettings

public function mappableAs(): array
{
    return [
        'property' => [
            'type' => 'property_type',
            'analyzer' => 'custom_ngram_analyzer'
        ],
    ];
}

public function searchableAs(): string
{
   return 'products_5';
}

public function indexSettings(): array
{
   return [
       "analysis" => [
            "analyzer" => [
                "custom_ngram_analyzer" => [
                    "type" => "custom",
                    "tokenizer" => "edge_ngram_tokenizer"
                ],
            ],
            "tokenizer" => [
                "edge_ngram_tokenizer" => [
                    "token_chars" => [
                        "letter",
                        "digit"
                    ],
                "min_gram" => "2",
                "type" => "edge_ngram",
                "max_gram" => "15"
                ]
            ]
        ],
   ];
}