-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathAbstractType.php
More file actions
162 lines (143 loc) · 4.06 KB
/
AbstractType.php
File metadata and controls
162 lines (143 loc) · 4.06 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Flowpack\ElasticSearch\Domain\Model;
/*
* This file is part of the Flowpack.ElasticSearch package.
*
* (c) Contributors of the Flowpack Team - flowpack.org
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Flowpack\ElasticSearch\Domain\Factory\DocumentFactory;
use Flowpack\ElasticSearch\Transfer\Response;
use Neos\Flow\Annotations as Flow;
/**
* An abstract document type. Implement your own or use the GenericType provided with this package.
*/
abstract class AbstractType
{
/**
* @Flow\Inject
* @var DocumentFactory
*/
protected $documentFactory;
/**
* @var Index
*/
protected $index;
/**
* @var string
*/
protected $name;
/**
* @param Index $index
* @param string $name
*/
public function __construct(Index $index, $name = null)
{
$this->index = $index;
if ($name === null) {
$this->name = str_replace('\\', '_', get_class($this));
} else {
$this->name = $name;
}
}
/**
* Gets this type's name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return Index
*/
public function getIndex()
{
return $this->index;
}
/**
* Returns a document
*
* @param string $id
* @return Document
*/
public function findDocumentById($id)
{
$response = $this->request('GET', '/' . $id);
if ($response->getStatusCode() !== 200) {
return null;
}
return $this->documentFactory->createFromResponse($this, $id, $response);
}
/**
* @param string $method
* @param string $path
* @param array $arguments
* @param string $content
* @return Response
*/
public function request($method, $path = null, array $arguments = [], $content = null, $header = null)
{
$path = '/' . $this->name . ($path ?: '');
return $this->index->request($method, $path, $arguments, $content);
}
/**
* @param string $id
* @return boolean ...whether the deletion is considered successful
*/
public function deleteDocumentById($id)
{
$response = $this->request('DELETE', '/' . $id);
$treatedContent = $response->getTreatedContent();
return $response->getStatusCode() === 200 && $treatedContent['result'] === 'deleted';
}
/**
* @return int
*/
public function count()
{
$response = $this->request('GET', '/_count');
if ($response->getStatusCode() !== 200) {
return null;
}
$treatedContent = $response->getTreatedContent();
return (integer)$treatedContent['count'];
}
/**
* @param array $searchQuery The search query TODO: make it an object
* @return Response
*/
public function search(array $searchQuery)
{
return $this->request('GET', '/_search', [], json_encode($searchQuery));
}
/**
* A bulk request always needs the following strukture:
* action_and_meta_data
* optional_source
* action_and_meta_data
* optional_source
* As the index and type are already in the url the meta_data part could be empty
*
* @param array $arguments
* @param string|array $content
* @return Response
*/
public function bulkRequest(array $arguments = [], $content = null)
{
$path = '/' . $this->name . '/_bulk';
if (is_array($content)) {
$ndjsonContent = '';
foreach ($content as $contentItem) {
// JSON_FORCE_OBJECT is important here as a empty meta_data needs to be a empty json object
$ndjsonContent = $ndjsonContent . json_encode($contentItem, JSON_FORCE_OBJECT) . "\n";
}
$content = $ndjsonContent;
}
return $this->index->request('POST', $path, $arguments, $content, true, 'application/x-ndjson');
}
}