1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* class ElasticIndex|Firesphere\ElasticSearch\Indexes\ElasticIndex is the base for indexing items |
4
|
|
|
* |
5
|
|
|
* @package Firesphere\Elastic\Search |
6
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
7
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Firesphere\ElasticSearch\Indexes; |
11
|
|
|
|
12
|
|
|
use Elastic\Elasticsearch\Exception\ClientResponseException; |
13
|
|
|
use Elastic\Elasticsearch\Exception\MissingParameterException; |
14
|
|
|
use Elastic\Elasticsearch\Exception\ServerResponseException; |
15
|
|
|
use Firesphere\ElasticSearch\Queries\Builders\QueryBuilder; |
16
|
|
|
use Firesphere\ElasticSearch\Queries\ElasticQuery; |
17
|
|
|
use Firesphere\ElasticSearch\Results\SearchResult; |
18
|
|
|
use Firesphere\ElasticSearch\Services\ElasticCoreService; |
19
|
|
|
use Firesphere\ElasticSearch\Traits\IndexTraits\BaseIndexTrait; |
20
|
|
|
use Firesphere\SearchBackend\Indexes\CoreIndex; |
21
|
|
|
use Firesphere\SearchBackend\Traits\LoggerTrait; |
22
|
|
|
use Firesphere\SearchBackend\Traits\QueryTraits\QueryFilterTrait; |
23
|
|
|
use LogicException; |
24
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
25
|
|
|
use SilverStripe\Control\HTTPRequest; |
26
|
|
|
use SilverStripe\Core\Config\Configurable; |
27
|
|
|
use SilverStripe\Core\Extensible; |
28
|
|
|
use SilverStripe\Core\Injector\Injectable; |
29
|
|
|
use SilverStripe\Core\Injector\Injector; |
30
|
|
|
use SilverStripe\Dev\Deprecation; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Base for managing a Elastic core. |
34
|
|
|
* |
35
|
|
|
* Base index settings and methods. Should be extended with at least a name for the index. |
36
|
|
|
* This is an abstract class that can not be instantiated on it's own |
37
|
|
|
* |
38
|
|
|
* @package Firesphere\Elastic\Search |
39
|
|
|
*/ |
40
|
|
|
abstract class ElasticIndex extends CoreIndex |
41
|
|
|
{ |
42
|
|
|
use Extensible; |
43
|
|
|
use Configurable; |
44
|
|
|
use Injectable; |
45
|
|
|
use QueryFilterTrait; |
46
|
|
|
use BaseIndexTrait; |
47
|
|
|
use LoggerTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $clientQuery; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array Classes to index |
56
|
|
|
*/ |
57
|
|
|
protected $class = []; |
58
|
|
|
|
59
|
|
|
public function __construct() |
60
|
|
|
{ |
61
|
|
|
$this->client = Injector::inst()->get(ElasticCoreService::class)->getClient(); |
62
|
|
|
|
63
|
|
|
$this->extend('onBeforeInit'); |
64
|
|
|
$this->init(); |
65
|
|
|
$this->extend('onAfterInit'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param HTTPRequest|null $request |
70
|
|
|
* @return bool |
71
|
|
|
* @throws ClientResponseException |
72
|
|
|
* @throws MissingParameterException |
73
|
|
|
* @throws NotFoundExceptionInterface |
74
|
|
|
* @throws ServerResponseException |
75
|
|
|
*/ |
76
|
|
|
public function deleteIndex(HTTPRequest $request): bool |
77
|
|
|
{ |
78
|
|
|
$deleteResult = false; |
79
|
|
|
if ($this->shouldClear($request) && $this->indexExists()) { |
80
|
|
|
$this->getLogger()->info(sprintf('Clearing index %s', $this->getIndexName())); |
81
|
|
|
$deleteResult = $this->client |
82
|
|
|
->indices() |
83
|
|
|
->delete(['index' => $this->getIndexName()]) |
84
|
|
|
->asBool(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $deleteResult; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param HTTPRequest $request |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
private function shouldClear(HTTPRequest $request): bool |
95
|
|
|
{ |
96
|
|
|
$var = $request->getVar('clear'); |
97
|
|
|
|
98
|
|
|
return !empty($var); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
* @throws ClientResponseException |
105
|
|
|
* @throws MissingParameterException |
106
|
|
|
* @throws ServerResponseException |
107
|
|
|
*/ |
108
|
|
|
public function indexExists(): bool |
109
|
|
|
{ |
110
|
|
|
return $this->getClient() |
111
|
|
|
->indices() |
112
|
|
|
->exists(['index' => $this->getIndexName()]) |
113
|
|
|
->asBool(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
abstract public function getIndexName(): string; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get classes |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getClasses(): array |
124
|
|
|
{ |
125
|
|
|
return $this->class; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the classes |
130
|
|
|
* |
131
|
|
|
* @param array $class |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setClasses($class): self |
135
|
|
|
{ |
136
|
|
|
$this->class = $class; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param ElasticQuery $query |
143
|
|
|
* @return SearchResult |
144
|
|
|
* @throws ClientResponseException |
145
|
|
|
* @throws ServerResponseException |
146
|
|
|
*/ |
147
|
|
|
public function doSearch(ElasticQuery $query) |
148
|
|
|
{ |
149
|
|
|
$this->clientQuery = QueryBuilder::buildQuery($query, $this); |
150
|
|
|
|
151
|
|
|
$result = $this->client->search($this->clientQuery); |
152
|
|
|
|
153
|
|
|
$result = new SearchResult($result, $query, $this); |
|
|
|
|
154
|
|
|
|
155
|
|
|
return $result; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Add a class to index or query |
160
|
|
|
* $options is not used anymore, added for backward compatibility |
161
|
|
|
* |
162
|
|
|
* @param $class |
163
|
|
|
* @param array $options unused |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function addClass($class, $options = []): self |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->class[] = $class; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getClientQuery(): array |
174
|
|
|
{ |
175
|
|
|
return $this->clientQuery; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function setClientQuery(array $clientQuery): void |
179
|
|
|
{ |
180
|
|
|
$this->clientQuery = $clientQuery; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.