123456789101112131415161718192021222324252627282930313233343536 |
- <?php
- namespace App\Service;
- use Hyperf\Elasticsearch\ClientBuilderFactory;
- use Elasticsearch\ClientBuilder;
- class ArticleSearchService
- {
- protected $client;
- public function __construct(ClientBuilderFactory $clientBuilderFactory)
- {
- $this->client = $clientBuilderFactory->create();
- }
- public function searchArticles($query, $page = 1, $pageSize = 10)
- {
- $from = ($page - 1) * $pageSize;
- $params = [
- 'index' => 'articles',
- 'body' => [
- 'from' => $from,
- 'size' => $pageSize,
- 'query' => [
- 'match' => [
- 'title' => $query,
- ],
- ],
- ],
- ];
- $response = $this->client->search($params);
- return $response['hits']['hits'];
- }
- }
|