不过即使是 6.4.0,['hits']['total'] 也应该是一个数字而不是数组,可以看 Laravel 5.8 课程里的截图(5.8 课程用的 ES 版本是 6.x)
@leo homestead 上运行命令后报错了,没有更新成功
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
现在就是两个问题
homestead 上运行 php artisan es:migrate 报错,更新 ES 也失败
阿里云服务器上 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:
推荐文章: