Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace IQDEV\ElasticSearch;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use IQDEV\ElasticSearch\Converter\CriteriaToEsRequest;
use IQDEV\ElasticSearch\Converter\EsResponseToResult;
use IQDEV\Search\Criteria;
use IQDEV\Search\Result;
class SearchService implements \IQDEV\Search\SearchService
{
private Client $esClient;
private Configuration $configuration;
private CriteriaToEsRequest $criteriaToEsRequest;
private EsResponseToResult $esResponseToResult;
public function __construct(Client $esClient, Configuration $configuration)
{
$this->esClient = $esClient;
$this->configuration = $configuration;
$this->criteriaToEsRequest = new CriteriaToEsRequest();
$this->esResponseToResult = new EsResponseToResult();
}
/**
* @throws ServerResponseException
* @throws ClientResponseException
*/
public function search(Criteria $criteria): Result
{
$request = $this->criteriaToEsRequest->fromCriteria($criteria);
$response = $this->esClient->search([
'index' => $this->configuration->getIndexName(),
'body' => $request->es(),
]);
return $this->esResponseToResult->fromResponse($response);
}
}