访问首页出错?

访问后台没有问题,访问首页出现了以下问题

访问首页出错?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
leo
最佳答案

第 50 行: file

$total 是 Laravel 计算出来的不可能错,你自己检查一下 perPage 是不是传错了

3年前 评论
讨论数量: 9
leo

第 50 行: file

$total 是 Laravel 计算出来的不可能错,你自己检查一下 perPage 是不是传错了

3年前 评论

@leo 不是 perPage 的问题,应该是这个 es 哪里出了问题吧,$result['hits']['total'] 是个数组。 file 我猜想因为在 7.7. Elasticsearch 索引结构迁移 这节我没有测试,可能是这节代码出了问题,所以今天我在本地测试了一下,出现了报错。我已经提交了另一个 问题。 比较奇怪的是我服务器上运行这命令的时候没有报错。

3年前 评论
leo

结构不一样说明 es 的版本和课程版本不一致,先看下你的 es 版本是多少

$ curl http://127.0.0.1:9200/
3年前 评论

@leo

file 老师,我 homestead 按照教程一步步下来都没问题,然后到 课程7.7节 7.7. Elasticsearch 索引结构迁移 删除索引,运行 php artisan es:migrate 后出错了也是因为版本问题嘛? 这是 错误链接 您也帮忙看下

3年前 评论
leo

是的,Laravel 6.x 课程代码用的是 ES 7.x 的版本

3年前 评论
leo

不过即使是 6.4.0,['hits']['total'] 也应该是一个数字而不是数组,可以看 Laravel 5.8 课程里的截图(5.8 课程用的 ES 版本是 6.x)

6.9. Elasticsearch 查询入门

3年前 评论

@leo 老师教一下如何升级到 ES 7.x 版本 ,是不是要更新整个 homestead ?

3年前 评论
leo

@断桥红颜V sudo apt update && sudo apt install -y elasticsearch=7.10

3年前 评论

@leo homestead 上运行命令后报错了,没有更新成功

file

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: cli-assets.heroku.com/branches/sta... ./ InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 5DC22404A6F9F1CA

老师,我是在 阿里云服务器 上打印出来是数组,阿里云上的 ES 版本是 7.10.1

file

现在就是两个问题

  1. homestead 上运行 php artisan es:migrate 报错,更新 ES 也失败

  2. 阿里云服务器上 ES 是 7.10.1 版本,但是获取到的 $result['hits']['total'] 是个数组,下面是代码

    public function index(Request $request)
     {
         $page    = $request->input('page', 1);
         $perPage = 16;
         // 新建查询构造器对象,设置只搜索上架商品,设置分页
         $builder = (new ProductSearchBuilder())->onSale()->paginate($perPage, $page);
    
         if ($request->input('category_id') && $category = Category::find($request->input('category_id'))) {
             // 调用查询构造器的类目筛选
             $builder->category($category);
         }
    
         if ($search = $request->input('search', '')) {
             $keywords = array_filter(explode(' ', $search));
             // 调用查询构造器的关键词筛选
             $builder->keywords($keywords);
         }
    
         if ($search || isset($category)) {
             // 调用查询构造器的分面搜索
             $builder->aggregateProperties();
         }
    
         $propertyFilters = [];
         if ($filterString = $request->input('filters')) {
             $filterArray = explode('|', $filterString);
             foreach ($filterArray as $filter) {
                 list($name, $value) = explode(':', $filter);
                 $propertyFilters[$name] = $value;
                 // 调用查询构造器的属性筛选
                 $builder->propertyFilter($name, $value);
             }
         }
    
         if ($order = $request->input('order', '')) {
             if (preg_match('/^(.+)_(asc|desc)$/', $order, $m)) {
                 if (in_array($m[1], ['price', 'sold_count', 'rating'])) {
                     // 调用查询构造器的排序
                     $builder->orderBy($m[1], $m[2]);
                 }
             }
         }
    
         // 最后通过 getParams() 方法取回构造好的查询参数
         $result = app('es')->search($builder->getParams());
         // 通过 collect 函数将返回结果转为集合,并通过集合的 pluck 方法取到返回的商品 ID 数组
         $productIds = collect($result['hits']['hits'])->pluck('_id')->all();
         // 通过 whereIn 方法从数据库中读取商品数据
         $products = Product::query()
             ->byIds($productIds)
             ->get();
         // 返回一个 LengthAwarePaginator 对象
         $pager = new LengthAwarePaginator($products, $result['hits']['total'], $perPage,
             $page, [
                 'path' => route('products.index') // 手动构建分页的 url
             ]);
    
         $properties = [];
         // 如果返回结果里有 aggregations 字段,说明做了分面搜索
         if (isset($result['aggregations'])) {
             // 使用 collect 函数将返回值转为集合
             $properties = collect($result['aggregations']['properties']['properties']['buckets'])
                 ->map(function ($bucket) {
                     // 通过 map 方法取出我们需要的字段
                     return [
                         'key'    => $bucket['key'],
                         'values' => collect($bucket['value']['buckets'])->pluck('key')->all(),
                     ];
                 })->filter(function ($property) use ($propertyFilters) {
                     // 过滤掉只剩下一个值 或者 已经在筛选条件里的属性
                     return count($property['values']) > 1 && !isset($propertyFilters[$property['key']]) ;
                 });
         }
    
         return view('products.index', [
             'products' => $pager,
             'filters'  => [
                 'search' => '',
                 'order'  => $order,
             ],
             'category' => $category ?? null,
             'properties' => $properties,
             'propertyFilters' => $propertyFilters,
         ]);
     }

    有点不知道如何解决,老师再帮忙看看,谢谢 :joy:

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!