7.1. 开始搜索
开始搜索
上一章我们学习了 Elasticsearch 的一些基础的查询语句,接下来我们将开始改造商品搜索页,改为使用 Elasticsearch 查询。
1. 分页与排序
首先我们先实现分页与排序功能,先不管搜索以及类目功能:
app/Http/Controllers/ProductsController.php
use Illuminate\Pagination\LengthAwarePaginator;
.
.
.
public function index(Request $request)
{
$page = $request->input('page', 1);
$perPage = 16;
// 构建查询
$params = [
'index' => 'products',
'type' => '_doc',
'body' => [
'from' => ($page - 1) * $perPage, // 通过当前页数与每页数量计算偏移值
'size' => $perPage,
'query' => [
'bool' => [
'filter' => [
['term' => ['on_sale' => true]],
],
],...