1
<?php
2

3
/*
4
 * This file is part of the Solarium package.
5
 *
6
 * For the full copyright and license information, please view the COPYING
7
 * file that was distributed with this source code.
8
 */
9

10
namespace Solarium\Component;
11

12
use Solarium\Component\RequestBuilder\ComponentRequestBuilderInterface;
13
use Solarium\Component\RequestBuilder\DistributedSearch as RequestBuilder;
14

15
/**
16
 * Distributed Search (sharding) component.
17
 *
18
 * @see https://lucene.apache.org/solr/guide/distributed-search-with-index-sharding.html
19
 * @see https://lucene.apache.org/solr/guide/solrcloud.html
20
 */
21
class DistributedSearch extends AbstractComponent
22
{
23
    /**
24
     * Request to be distributed across all shards in the list.
25
     *
26
     * @var array
27
     */
28
    protected $shards = [];
29

30
    /**
31
     * Requests will be distributed across collections in this list.
32
     *
33
     * @var array
34
     */
35
    protected $collections = [];
36

37
    /**
38
     * Requests will be load balanced across replicas in this list.
39
     *
40
     * @var array
41
     */
42
    protected $replicas = [];
43

44
    /**
45
     * Get component type.
46
     *
47
     * @return string
48
     */
49 12
    public function getType(): string
50
    {
51 12
        return ComponentAwareQueryInterface::COMPONENT_DISTRIBUTEDSEARCH;
52
    }
53

54
    /**
55
     * Get a requestbuilder for this query.
56
     *
57
     * @return RequestBuilder
58
     */
59 12
    public function getRequestBuilder(): ComponentRequestBuilderInterface
60
    {
61 12
        return new RequestBuilder();
62
    }
63

64
    /**
65
     * Add a shard.
66
     *
67
     * @param string $key   unique string
68
     * @param string $shard The syntax is host:port/base_url
69
     *
70
     * @return self Provides fluent interface
71
     *
72
     * @see https://lucene.apache.org/solr/guide/distributed-search-with-index-sharding.html
73
     */
74 12
    public function addShard(string $key, string $shard): self
75
    {
76 12
        $this->shards[$key] = $shard;
77

78 12
        return $this;
79
    }
80

81
    /**
82
     * Add multiple shards.
83
     *
84
     * Example usage:
85
     * <code>
86
     * $client = new Solarium\Client;
87
     * $query = $client->createSelect();
88
     * $distributedSearch = $query->getDistributedSearch();
89
     * $distributedSearch->addShards(array(
90
     *     'core0' => 'localhost:8983/solr/core0',
91
     *     'core1' => 'localhost:8983/solr/core1'
92
     * ));
93
     * $result = $client->select($query);
94
     * </code>
95
     *
96
     * @param array $shards
97
     *
98
     * @return self Provides fluent interface
99
     */
100 12
    public function addShards(array $shards): self
101
    {
102 12
        foreach ($shards as $key => $shard) {
103 12
            $this->addShard($key, $shard);
104
        }
105

106 12
        return $this;
107
    }
108

109
    /**
110
     * Remove a shard.
111
     *
112
     * @param string $key
113
     *
114
     * @return self Provides fluent interface
115
     */
116 12
    public function removeShard(string $key): self
117
    {
118 12
        if (isset($this->shards[$key])) {
119 12
            unset($this->shards[$key]);
120
        }
121

122 12
        return $this;
123
    }
124

125
    /**
126
     * Remove all shards.
127
     *
128
     * @return self Provides fluent interface
129
     */
130 12
    public function clearShards(): self
131
    {
132 12
        $this->shards = [];
133

134 12
        return $this;
135
    }
136

137
    /**
138
     * Set multiple shards.
139
     *
140
     * This overwrites any existing shards
141
     *
142
     * Example usage:
143
     * <code>
144
     * $client = new Solarium\Client;
145
     * $query = $client->createSelect();
146
     * $distributedSearch = $query->getDistributedSearch();
147
     * $distributedSearch->setShards(array(
148
     *     'core0' => 'localhost:8983/solr/core0',
149
     *     'core1' => 'localhost:8983/solr/core1'
150
     * ));
151
     * $result = $client->select($query);
152
     * </code>
153
     *
154
     * @param array $shards Associative array of shards
155
     *
156
     * @return self Provides fluent interface
157
     */
158 12
    public function setShards(array $shards): self
159
    {
160 12
        $this->clearShards();
161 12
        $this->addShards($shards);
162

163 12
        return $this;
164
    }
165

166
    /**
167
     * Get a list of the shards.
168
     *
169
     * @return array
170
     */
171 12
    public function getShards(): array
172
    {
173 12
        return $this->shards;
174
    }
175

176
    /**
177
     *  A sharded request will go to the standard request handler
178
     *  (not necessarily the original); this can be overridden via shards.qt.
179
     *
180
     * @param string $handler
181
     *
182
     * @return self Provides fluent interface
183
     */
184 12
    public function setShardRequestHandler(string $handler): self
185
    {
186 12
        $this->setOption('shardhandler', $handler);
187

188 12
        return $this;
189
    }
190

191
    /**
192
     * Get a shard request handler (shards.qt).
193
     *
194
     * @return ?string
195
     */
196 12
    public function getShardRequestHandler(): ?string
197
    {
198 12
        return $this->getOption('shardhandler');
199
    }
200

201
    /**
202
     * Add a collection.
203
     *
204
     * @param string $key        unique string
205
     * @param string $collection The syntax is host:port/base_url
206
     *
207
     * @return self Provides fluent interface
208
     *
209
     * @see https://lucene.apache.org/solr/guide/solrcloud.html
210
     */
211 12
    public function addCollection(string $key, string $collection): self
212
    {
213 12
        $this->collections[$key] = $collection;
214

215 12
        return $this;
216
    }
217

218
    /**
219
     * Add multiple collections.
220
     *
221
     * @param array $collections
222
     *
223
     * @return self Provides fluent interface
224
     */
225 12
    public function addCollections(array $collections): self
226
    {
227 12
        foreach ($collections as $key => $collection) {
228 12
            $this->addCollection($key, $collection);
229
        }
230

231 12
        return $this;
232
    }
233

234
    /**
235
     * Remove a collection.
236
     *
237
     * @param string $key
238
     *
239
     * @return self Provides fluent interface
240
     */
241 12
    public function removeCollection(string $key): self
242
    {
243 12
        if (isset($this->collections[$key])) {
244 12
            unset($this->collections[$key]);
245
        }
246

247 12
        return $this;
248
    }
249

250
    /**
251
     * Remove all collections.
252
     *
253
     * @return self Provides fluent interface
254
     */
255 12
    public function clearCollections(): self
256
    {
257 12
        $this->collections = [];
258

259 12
        return $this;
260
    }
261

262
    /**
263
     * Set multiple collections.
264
     *
265
     * This overwrites any existing collections
266
     *
267
     * @param array $collections Associative array of collections
268
     *
269
     * @return self Provides fluent interface
270
     */
271 12
    public function setCollections(array $collections): self
272
    {
273 12
        $this->clearCollections();
274 12
        $this->addCollections($collections);
275

276 12
        return $this;
277
    }
278

279
    /**
280
     * Get a list of the collections.
281
     *
282
     * @return array
283
     */
284 12
    public function getCollections(): array
285
    {
286 12
        return $this->collections;
287
    }
288

289
    /**
290
     * Add a replica.
291
     *
292
     * @param string $key     unique string
293
     * @param string $replica The syntax is host:port/base_url
294
     *
295
     * @return self Provides fluent interface
296
     *
297
     * @see https://lucene.apache.org/solr/guide/distributed-requests.html
298
     */
299 12
    public function addReplica(string $key, string $replica): self
300
    {
301 12
        $this->replicas[$key] = $replica;
302

303 12
        return $this;
304
    }
305

306
    /**
307
     * Add multiple replicas.
308
     *
309
     * @param array $replicas
310
     *
311
     * @return self Provides fluent interface
312
     */
313 12
    public function addReplicas(array $replicas): self
314
    {
315 12
        foreach ($replicas as $key => $replica) {
316 12
            $this->addReplica($key, $replica);
317
        }
318

319 12
        return $this;
320
    }
321

322
    /**
323
     * Remove a replica.
324
     *
325
     * @param string $key
326
     *
327
     * @return self Provides fluent interface
328
     */
329 12
    public function removeReplica(string $key): self
330
    {
331 12
        if (isset($this->replicas[$key])) {
332 12
            unset($this->replicas[$key]);
333
        }
334

335 12
        return $this;
336
    }
337

338
    /**
339
     * Remove all replicas.
340
     *
341
     * @return self Provides fluent interface
342
     */
343 12
    public function clearReplicas(): self
344
    {
345 12
        $this->replicas = [];
346

347 12
        return $this;
348
    }
349

350
    /**
351
     * Set multiple replicas.
352
     *
353
     * This overwrites any existing replicas
354
     *
355
     * @param array $replicas Associative array of collections
356
     *
357
     * @return self Provides fluent interface
358
     */
359 12
    public function setReplicas(array $replicas): self
360
    {
361 12
        $this->clearReplicas();
362 12
        $this->addReplicas($replicas);
363

364 12
        return $this;
365
    }
366

367
    /**
368
     * Get a list of the replicas.
369
     *
370
     * @return array
371
     */
372 12
    public function getReplicas(): array
373
    {
374 12
        return $this->replicas;
375
    }
376

377
    /**
378
     * Initialize options.
379
     *
380
     * Several options need some extra checks or setup work, for these options
381
     * the setters are called.
382
     */
383 12
    protected function init()
384
    {
385 12
        foreach ($this->options as $name => $value) {
386
            switch ($name) {
387 12
                case 'shards':
388 12
                    $this->setShards($value);
389 12
                    break;
390 12
                case 'collections':
391 12
                    $this->setCollections($value);
392 12
                    break;
393 12
                case 'replicas':
394 12
                    $this->setReplicas($value);
395 12
                    break;
396
            }
397
        }
398
    }
399
}

Read our documentation on viewing source code .

Loading