Laravel Explorer 配置

问题描述 投票:0回答:1

我很难以我想要的方式配置 Laravel Explorer。我使用elasticsearch的主要想法是产品搜索。正如互联网上的每个产品搜索一样,它应该具有自动完成功能(例如,如果我搜索“pro”,它应该找到“产品”)和加权功能(例如,优先考虑标题而不是描述)。

对于自动完成,我发现

edge_ngram
标记生成器应该有所帮助,但我无法在
indexSettings()
函数中进行配置,我认为应该配置它,并且我在文档中没有找到任何有关此内容的信息,除了 github 讨论,但这也没有帮助。

关于加权,我发现唯一有用的是

boost
,但这也不能正常工作,对我来说有点困惑
boost
weight
之间的区别是什么以及为什么我不能定义列的权重,以优先考虑它们。

这是我尝试配置模型的方法:

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',
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }
}

我对 Elasticsearch 很严格,所以如果能以某种方式实现这项工作那就太好了。我还尝试了 TNTSearch,但经过一些解决方法后,它也没有像我预期的那样工作。

目前我不确定这是一个好方法,任何编写产品搜索的想法或经验都会有用,真的那么难吗?

php laravel elasticsearch e-commerce laravel-scout
1个回答
0
投票

我成功配置了edge_ngram分析器,并且通过提升我获得了期望的搜索效果。 Edge_ngram 分析器解决了自动完成缺失问题。

注意:Scout::flush 命令不会修改索引,它只是从中删除记录,因此每次修改索引设置时,我都必须在

searchableAs
函数中定义一个新索引

我的模型最适合我:

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"
                ]
            ]
        ],
   ];
}
© www.soinside.com 2019 - 2024. All rights reserved.