forked from elasticquent/Elasticquent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElasticquentCollectionTrait.php
108 lines (90 loc) · 3.06 KB
/
ElasticquentCollectionTrait.php
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php namespace Elasticquent;
/**
* Elasticquent Collection Trait
*
* Elasticsearch functions that you
* can run on collections of documents.
*/
trait ElasticquentCollectionTrait
{
use ElasticquentClientTrait;
/**
* @var int The number of records (ie. models) to send to Elasticsearch in one go
* Also, the number of models to get from the database at a time using Eloquent's chunk()
*/
static public $entriesToSendToElasticSearchInOneGo = 500;
/**
* Add To Index
*
* Add all documents in this collection to to the Elasticsearch document index.
*
* @return null|array
*/
public function addToIndex()
{
if ($this->isEmpty()) {
return null;
}
// Use an stdClass to store result of elasticsearch operation
$result = new \stdClass;
// Iterate according to the amount configured, and put that iteration's worth of records into elastic search
// This is done so that we do not exceed the maximum request size
$all = $this->all();
$iteration = 0;
do {
$chunk = array_slice($all, (0 + ($iteration * static::$entriesToSendToElasticSearchInOneGo)), static::$entriesToSendToElasticSearchInOneGo);
$params = array();
foreach ($chunk as $item) {
$params['body'][] = array(
'index' => array(
'_id' => $item->getKey(),
'_type' => $item->getTypeName(),
'_index' => $item->getIndexName(),
),
);
$params['body'][] = $item->getIndexDocumentData();
}
$result = $this->getElasticSearchClient()->bulk($params);
// Check for errors
if ( (array_key_exists('errors', $result) && $result['errors'] != false ) || (array_key_exists('Message', $result) && stristr('Request size exceeded', $result['Message']) !== false)) {
break;
}
// Remove vars immediately to prevent them hanging around in memory, in case we have a large number of iterations
unset($chunk, $params);
++$iteration;
} while (count($all) > ($iteration * static::$entriesToSendToElasticSearchInOneGo) );
return $result;
}
/**
* Delete From Index
*
* @return array
*/
public function deleteFromIndex()
{
$all = $this->all();
$params = array();
foreach ($all as $item) {
$params['body'][] = array(
'delete' => array(
'_id' => $item->getKey(),
'_type' => $item->getTypeName(),
'_index' => $item->getIndexName(),
),
);
}
return $this->getElasticSearchClient()->bulk($params);
}
/**
* Reindex
*
* Delete the items and then re-index them.
*
* @return array
*/
public function reindex()
{
$this->deleteFromIndex();
return $this->addToIndex();
}
}