Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 132 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
Service | |
0.00% |
0 / 132 |
|
0.00% |
0 / 6 |
552 | |
0.00% |
0 / 1 |
fetchId | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
fetchList | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
fetchFromCsv | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
searchAll | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
42 | |||
readSearchResultList | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 | |||
fetchServicesForCompilation | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | /** |
4 | * @package ClientDldb |
5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
6 | **/ |
7 | |
8 | namespace BO\Zmsdldb\Elastic; |
9 | |
10 | use BO\Zmsdldb\Entity\Service as Entity; |
11 | use BO\Zmsdldb\Collection\Services as Collection; |
12 | use BO\Zmsdldb\File\Service as Base; |
13 | |
14 | /** |
15 | * @SuppressWarnings(Coupling) |
16 | */ |
17 | class Service extends Base |
18 | { |
19 | /** |
20 | * |
21 | * @return Entity\Service |
22 | */ |
23 | public function fetchId($service_id) |
24 | { |
25 | if ($service_id) { |
26 | $query = Helper::boolFilteredQuery(); |
27 | $filter = new \Elastica\Filter\Ids(); |
28 | $filter->setIds($this->locale . $service_id); |
29 | $query->getFilter()->addMust($filter); |
30 | $result = $this->access() |
31 | ->getIndex() |
32 | ->getType('service') |
33 | ->search($query); |
34 | if ($result->count() == 1) { |
35 | $locationList = $result->getResults(); |
36 | return new Entity($locationList[0]->getData()); |
37 | } |
38 | } |
39 | return false; |
40 | } |
41 | |
42 | /** |
43 | * |
44 | * @return Collection\Services |
45 | */ |
46 | public function fetchList($location_csv = false) |
47 | { |
48 | $boolquery = Helper::boolFilteredQuery(); |
49 | $boolquery->getFilter()->addMust(Helper::localeFilter($this->locale)); |
50 | $query = \Elastica\Query::create($boolquery); |
51 | if ($location_csv) { |
52 | $filter = new \Elastica\Filter\Terms('locations.location', explode(',', $location_csv)); |
53 | $filter->setExecution('and'); |
54 | $query->setPostFilter($filter); |
55 | } |
56 | $resultList = $this->access() |
57 | ->getIndex() |
58 | ->getType('service') |
59 | ->search($query, 10000); |
60 | $serviceList = new Collection(); |
61 | foreach ($resultList as $result) { |
62 | $service = new Entity($result->getData()); |
63 | $serviceList[$service['id']] = $service; |
64 | } |
65 | return $serviceList; |
66 | } |
67 | |
68 | /** |
69 | * |
70 | * @return Collection\Services |
71 | */ |
72 | public function fetchFromCsv($service_csv) |
73 | { |
74 | $query = Helper::boolFilteredQuery(); |
75 | $filter = new \Elastica\Filter\Ids(); |
76 | $ids = explode(',', $service_csv); |
77 | $ids = array_map(function ($value) { |
78 | return $this->locale . $value; |
79 | }, $ids); |
80 | $filter->setIds($ids); |
81 | $query->getFilter()->addMust($filter); |
82 | $resultList = $this->access() |
83 | ->getIndex() |
84 | ->getType('service') |
85 | ->search($query, 10000); |
86 | $serviceList = new Collection(); |
87 | foreach ($resultList as $result) { |
88 | $service = new Entity($result->getData()); |
89 | $serviceList[$service['id']] = $service; |
90 | } |
91 | return $serviceList; |
92 | } |
93 | |
94 | /** |
95 | * |
96 | * @return Collection\Services |
97 | */ |
98 | public function searchAll($querystring, $service_csv = '', $location_csv = '') |
99 | { |
100 | $query = new \Elastica\Query(); |
101 | $locationsCsvByUser = false; |
102 | if (! $location_csv) { |
103 | $location_csv = $this->fetchLocationCsv($service_csv); |
104 | } else { |
105 | $locationsCsvByUser = true; |
106 | } |
107 | |
108 | $boolquery = new \Elastica\Query\BoolQuery(); |
109 | $searchquery = new \Elastica\Query\QueryString(); |
110 | if ('' === trim($querystring)) { |
111 | $searchquery->setQuery('*'); |
112 | } else { |
113 | $searchquery->setQuery($querystring); |
114 | } |
115 | $searchquery->setFields([ |
116 | 'name^9', |
117 | 'keywords^5' |
118 | ]); |
119 | |
120 | $boolquery->addShould($searchquery); |
121 | $filter = null; |
122 | $filter = new \Elastica\Filter\BoolFilter(); |
123 | $filter->addMust(Helper::localeFilter($this->locale)); |
124 | if ($location_csv) { |
125 | $filter->addMust(new \Elastica\Filter\Terms('locations.location', explode(',', $location_csv))); |
126 | } |
127 | $filteredQuery = new \Elastica\Query\Filtered($boolquery, $filter); |
128 | $query->setQuery($filteredQuery); |
129 | $query->addSort(['sort' => 'asc']); |
130 | $resultList = $this->access() |
131 | ->getIndex() |
132 | ->getType('service') |
133 | ->search($query, 1000); |
134 | $serviceList = new Collection(); |
135 | foreach ($resultList as $result) { |
136 | $service = new Entity($result->getData()); |
137 | $serviceList[$service['id']] = $service; |
138 | } |
139 | if ($locationsCsvByUser) { |
140 | $serviceList = $serviceList->containsLocation($location_csv); |
141 | } |
142 | return $serviceList; |
143 | } |
144 | |
145 | /** |
146 | * this function is similar to self::searchAll() but it might get different boosts in the future |
147 | * additionally, a restriction by locale is missing |
148 | * |
149 | * @return Collection |
150 | */ |
151 | public function readSearchResultList($query, $service_csv = '') |
152 | { |
153 | $boolquery = Helper::boolFilteredQuery(); |
154 | $boolquery->getFilter()->addMust(Helper::localeFilter($this->locale)); |
155 | $searchquery = new \Elastica\Query\QueryString(); |
156 | if ('' === trim($query)) { |
157 | $searchquery->setQuery('*'); |
158 | } else { |
159 | $searchquery->setQuery($query); |
160 | } |
161 | $searchquery->setFields([ |
162 | 'name^9', |
163 | 'keywords^5' |
164 | ]); |
165 | $boolquery->getQuery()->addShould($searchquery); |
166 | $filter = null; |
167 | if ($service_csv) { |
168 | $filter = new \Elastica\Filter\Terms('services.service', explode(',', $service_csv)); |
169 | } |
170 | $query = new \Elastica\Query\Filtered($boolquery, $filter); |
171 | $resultList = $this->access() |
172 | ->getIndex() |
173 | ->getType('service') |
174 | ->search($query, 1000); |
175 | $serviceList = new Collection(); |
176 | foreach ($resultList as $result) { |
177 | $service = new Entity($result->getData()); |
178 | $serviceList[$service['id']] = $service; |
179 | } |
180 | return $serviceList; |
181 | } |
182 | |
183 | public function fetchServicesForCompilation($authoritys = [], $locations = [], $services = []) |
184 | { |
185 | $limit = 1000; |
186 | |
187 | $localeFilter = new \Elastica\Query\Term(array( |
188 | 'meta.locale' => $this->locale |
189 | )); |
190 | |
191 | $boolquery = new \Elastica\Query\BoolQuery(); |
192 | $boolquery->addMust($localeFilter); |
193 | |
194 | if (!empty($authoritys)) { |
195 | $authorityFilter = new \Elastica\Query\Terms('authorities.id', $authoritys); |
196 | $boolquery->addMust($authorityFilter); |
197 | } |
198 | if (!empty($locations)) { |
199 | $locationFilter = new \Elastica\Query\Terms('locations.location', $locations); |
200 | $boolquery->addMust($locationFilter); |
201 | } |
202 | if (!empty($services)) { |
203 | $serviceFilter = new \Elastica\Query\Terms('id', $services); |
204 | $boolquery->addMust($serviceFilter); |
205 | } |
206 | |
207 | $query = \Elastica\Query::create($boolquery); |
208 | $query->addSort(['sort' => 'asc']); |
209 | $resultList = $this |
210 | ->access() |
211 | ->getIndex() |
212 | ->getType('service') |
213 | ->search($query, $limit) |
214 | ; |
215 | $serviceList = new Collection(); |
216 | foreach ($resultList as $result) { |
217 | $service = new Entity($result->getData()); |
218 | $serviceList[$service['id']] = $service; |
219 | } |
220 | return $serviceList; |
221 | } |
222 | } |