|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the ContentSearchHitAdapter class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\Pagination\Pagerfanta; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
|
12
|
|
|
use eZ\Publish\API\Repository\SearchService; |
|
13
|
|
|
use Pagerfanta\Adapter\AdapterInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Pagerfanta adapter for eZ Publish content search. |
|
17
|
|
|
* Will return results as SearchHit objects. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ContentSearchHitAdapter implements AdapterInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Query */ |
|
22
|
|
|
private $query; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \eZ\Publish\API\Repository\SearchService */ |
|
25
|
|
|
private $searchService; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
private $languageFilter; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string|null */ |
|
31
|
|
|
private $displayLanguage; |
|
32
|
|
|
|
|
33
|
|
|
/** @var int */ |
|
34
|
|
|
private $nbResults; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
Query $query, |
|
38
|
|
|
SearchService $searchService, |
|
39
|
|
|
array $languageFilter = [], |
|
40
|
|
|
?string $displayLanguage = null |
|
41
|
|
|
) { |
|
42
|
|
|
$this->query = $query; |
|
43
|
|
|
$this->searchService = $searchService; |
|
44
|
|
|
$this->languageFilter = $languageFilter; |
|
45
|
|
|
$this->displayLanguage = $displayLanguage; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getDisplayLanguage(): ?string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->displayLanguage; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the number of results. |
|
55
|
|
|
* |
|
56
|
|
|
* @return int The number of results. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getNbResults() |
|
59
|
|
|
{ |
|
60
|
|
|
if (isset($this->nbResults)) { |
|
61
|
|
|
return $this->nbResults; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$countQuery = clone $this->query; |
|
65
|
|
|
$countQuery->limit = 0; |
|
66
|
|
|
|
|
67
|
|
|
$searchResult = $this->searchService->findContent( |
|
68
|
|
|
$countQuery, |
|
69
|
|
|
$this->languageFilter |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return $this->nbResults = $searchResult->totalCount; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns a slice of the results, as SearchHit objects. |
|
77
|
|
|
* |
|
78
|
|
|
* @param int $offset The offset. |
|
79
|
|
|
* @param int $length The length. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getSlice($offset, $length) |
|
84
|
|
|
{ |
|
85
|
|
|
$query = clone $this->query; |
|
86
|
|
|
$query->offset = $offset; |
|
87
|
|
|
$query->limit = $length; |
|
88
|
|
|
$query->performCount = false; |
|
89
|
|
|
|
|
90
|
|
|
$searchResult = $this->searchService->findContent($query, $this->languageFilter); |
|
91
|
|
|
|
|
92
|
|
|
// Set count for further use if returned by search engine despite !performCount (Solr, ES) |
|
93
|
|
|
if (!isset($this->nbResults) && isset($searchResult->totalCount)) { |
|
94
|
|
|
$this->nbResults = $searchResult->totalCount; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $searchResult->searchHits; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|