【问题标题】:Search engine query impoving Laravel 5.2搜索引擎查询改进 Laravel 5.2
【发布时间】:2017-05-15 19:37:32
【问题描述】:

大家好,新年快乐! 所以我为我的应用程序实现了一个简单的搜索引擎,这样用户就可以搜索产品并购买他们喜欢的东西。到目前为止,查询只关注 2 列;标题和描述。

这是我的查询:

Product::where('title', 'LIKE', "%{$query}%")->orWhere('description', 'LIKE', "%{$query}%")->get();

这是正在查询的产品表:

Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('imagePath');
        $table->string('title');
        $table->text('description');
        $table->string('category',30);
        $table->integer('price');
        $table->integer('quantity');
        $table->integer('XS');
        $table->integer('S');
        $table->integer('M');
        $table->integer('L');
        $table->integer('XL');
        $table->integer('XXL');
    });

我可以这样修改我的查询:

Product::where('title', 'LIKE', "%{$query}%")->
orWhere('description', 'LIKE', "%{$query}%")->
orWhere('category', 'LIKE', "%{$query}%")->
orWhere('price', 'LIKE', "%{$query}%")->
orWhere('quantity', 'LIKE', "%{$query}%")->
orWhere('XS', 'LIKE', "%{$query}%")->
orWhere('S', 'LIKE', "%{$query}%")->
orWhere('M', 'LIKE', "%{$query}%")->
orWhere('L', 'LIKE', "%{$query}%")->
orWhere('XL', 'LIKE', "%{$query}%")->
orWhere('XXL', 'LIKE', "%{$query}%")->get();

现在我关心的是两件事; 如何使查询区分用户是在搜索名称中包含 xl 的产品,还是搜索 xl 尺寸或价格而不是数量的产品(例如 T 恤),因为它们都是整数?

此外,如果用户在搜索字段 (_) 中点击空格键,则会返回数据库中的所有产品。如果用户按两次空格键 (__),它会返回一条消息“没有此类产品等”,就像它应该的那样。有什么办法可以防止这种情况发生吗?

任何帮助将不胜感激!

【问题讨论】:

    标签: php mysql laravel search laravel-5


    【解决方案1】:

    您可以在页面中添加一些标签匹配输入或选择,这些标签如尺寸/价格。当您提交搜索表单时,表单数据如下:

    {
        title:'t-shirt',
        category:'clothes',
        size:'xl',
        min-price:100,
        max-price:200
    }
    

    那么你的查询看起来像:

    $query = Product::where('title', 'LIKE', $title})->
    orWhere('description', 'LIKE', $titlel)->
    orWhere('category', 'LIKE', $category)->
    orWhere('price', '>', $min_price)->
    orWhere('price', '<', $mac_price)->
    orWhere('quantity', 'LIKE', $quantity)->
    
    //judge the params have size
    if(params_size_haved){
        $query->orWhereNotNull($size)    
    }
    
    $query->get();
    

    代码只是一个简单的demo,可以根据具体使用情况修改。

    关于空间:

    您可以使用trim($search_str)str_replce() 删除搜索字段中的空格,这应该在查询执行之前。

    更多建议:

    桌子不太好。也许你可以谷歌sku table design以获取更多信息

    【讨论】:

    • 感谢您的回答。我去看看
    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 2012-01-29
    • 2018-05-16
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多