-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathElasticSearchQueryTest.php
More file actions
321 lines (280 loc) · 10.8 KB
/
ElasticSearchQueryTest.php
File metadata and controls
321 lines (280 loc) · 10.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
declare(strict_types=1);
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Eel;
/*
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* 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\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryResult;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\BaseElasticsearchContentRepositoryAdapterTest;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\ContentRepositoryNodeCreationTrait;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Traits\ContentRepositorySetupTrait;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Persistence\QueryResultInterface;
class ElasticSearchQueryTest extends BaseElasticsearchContentRepositoryAdapterTest
{
use ContentRepositorySetupTrait, ContentRepositoryNodeCreationTrait;
public function setUp(): void
{
parent::setUp();
$this->setupContentRepository();
$this->createNodesForNodeSearchTest();
sleep(1);
$this->indexNodes();
}
/**
* @test
*/
public function elasticSearchQueryBuilderStartsClean(): void
{
/** @var ElasticSearchQueryBuilder $query */
$query = $this->objectManager->get(ElasticSearchQueryBuilder::class);
$cleanRequestArray = $query->getRequest()->toArray();
$query->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document');
$query2 = $this->objectManager->get(ElasticSearchQueryBuilder::class);
static::assertNotSame($query->getRequest(), $query2->getRequest());
static::assertEquals($cleanRequestArray, $query2->getRequest()->toArray());
}
/**
* @test
*/
public function fullTextSearchReturnsTheDocumentNode(): void
{
/** @var ElasticSearchQueryResult $result */
$result = $this->getQueryBuilder()
->fulltext('circum*')
->log($this->getLogMessagePrefix(__METHOD__))
->execute();
static::assertEquals(1, $result->count());
/** @var NodeInterface $node */
$node = $result->current();
static::assertEquals('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document', $node->getNodeType()->getName());
static::assertEquals('test-node-1', $node->getName());
}
/**
* @test
*/
public function fullTextHighlighting(): void
{
/** @var ElasticSearchQueryBuilder $queryBuilder */
$queryBuilder = $this->getQueryBuilder();
/** @var NodeInterface $resultNode */
$resultNode = $queryBuilder
->fulltext('whistles')
->log($this->getLogMessagePrefix(__METHOD__))
->execute()
->current();
$searchHitForNode = $queryBuilder->getFullElasticSearchHitForNode($resultNode);
$highlightedText = current($searchHitForNode['highlight']['neos_fulltext.text']);
$expected = 'A Scout smiles and <em>whistles</em> under all circumstances.';
static::assertEquals($expected, $highlightedText);
}
/**
* @test
*/
public function filterByNodeType(): void
{
$resultCount = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document')
->count();
static::assertEquals(6, $resultCount);
}
/**
* @test
*/
public function filterByNodeTypes(): void
{
$resultCount = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeTypeFilter(['Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document'], ['Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document2'])
->count();
static::assertEquals(4, $resultCount);
}
/**
* @test
*/
public function filterNodeByProperty(): void
{
$resultCount = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->exactMatch('title', 'egg')
->count();
static::assertEquals(1, $resultCount);
}
/**
* @test
*
* @throws QueryBuildingException
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
*/
public function prefixFilter(): void
{
$resultCount = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->prefix('title', 'chi')
->count();
static::assertEquals(2, $resultCount);
}
/**
* @test
*/
public function limitDoesNotImpactCount(): void
{
$query = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document')
->limit(1);
$resultCount = $query->count();
static::assertEquals(6, $resultCount, 'Asserting the count query returns the total count.');
}
/**
* @test
*/
public function limitImpactGetAccessibleCount(): void
{
$query = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->limit(1);
$result = $query->execute();
static::assertEquals(1, $result->getAccessibleCount(), 'Asserting that getAccessibleCount returns the correct number');
static::assertCount(1, $result->toArray(), 'Asserting the executed query returns a valid number of items.');
}
/**
* @test
* @throws QueryBuildingException
*/
public function fieldBasedAggregations(): void
{
$aggregationTitle = 'titleagg';
$result = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->fieldBasedAggregation($aggregationTitle, 'title')
->execute()
->getAggregations();
static::assertArrayHasKey($aggregationTitle, $result);
static::assertCount(5, $result[$aggregationTitle]['buckets']);
$expectedChickenBucket = [
'key' => 'chicken',
'doc_count' => 2
];
$this->assertEquals($expectedChickenBucket, $result[$aggregationTitle]['buckets'][0]);
}
/**
* @return array
*/
public function termSuggestionDataProvider(): array
{
return [
'singleWord' => [
'term' => 'chickn',
'expectedBestSuggestions' => [
'chicken'
]
],
'multiWord' => [
'term' => 'chickn eggs',
'expectedBestSuggestions' => [
'chicken',
'egg'
]
],
];
}
/**
* @dataProvider termSuggestionDataProvider
* @test
*
* @param string $term
* @param array $expectedBestSuggestions
*/
public function termSuggestion(string $term, array $expectedBestSuggestions): void
{
$result = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->termSuggestions($term, 'title_analyzed')
->execute()
->getSuggestions();
static::assertArrayHasKey('suggestions', $result, 'The result should contain a key suggestions but looks like this ' . print_r($result, true));
static::assertIsArray($result['suggestions']);
static::assertCount(count($expectedBestSuggestions), $result['suggestions'], sprintf('Expected %s suggestions "[%s]" but got %s suggestions', count($expectedBestSuggestions), implode(',', $expectedBestSuggestions), print_r($result['suggestions'], true)));
foreach ($expectedBestSuggestions as $key => $expectedBestSuggestion) {
$suggestion = $result['suggestions'][$key];
static::assertArrayHasKey('options', $suggestion);
static::assertCount(1, $suggestion['options']);
static::assertEquals($expectedBestSuggestion, $suggestion['options'][0]['text']);
}
}
/**
* @test
*/
public function nodesWillBeSortedDesc(): void
{
$result = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document')
->sortDesc('title')
->execute();
/** @var QueryResultInterface $result $node */
static::assertInstanceOf(QueryResultInterface::class, $result);
static::assertCount(6, $result, 'The result should have 6 items');
static::assertEquals(6, $result->count(), 'Count should be 6');
$node = $result->getFirst();
static::assertInstanceOf(NodeInterface::class, $node);
static::assertEquals('welcome', $node->getProperty('title'), 'Asserting a desc sort order by property title');
}
/**
* @test
*/
public function nodesWillBeSortedAsc(): void
{
$result = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document')
->sortAsc('title')
->execute();
/** @var ElasticSearchQueryResult $result */
$node = $result->getFirst();
static::assertInstanceOf(NodeInterface::class, $node);
static::assertEquals('chicken', $node->getProperty('title'), 'Asserting a asc sort order by property title');
}
/**
* @test
*/
public function sortValuesAreReturned(): void
{
$result = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Document')
->sortAsc('title')
->execute();
foreach ($result as $node) {
static::assertEquals([$node->getProperty('title')], $result->getSortValuesForNode($node));
}
}
/**
* @test
* @throws QueryBuildingException
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
*/
public function cacheLifetimeIsCalculatedCorrectly(): void
{
$cacheLifetime = $this->getQueryBuilder()
->log($this->getLogMessagePrefix(__METHOD__))
->nodeType('Flowpack.ElasticSearch.ContentRepositoryAdaptor:Content')
->sortAsc('title')
->cacheLifetime();
static::assertEquals(600, $cacheLifetime);
}
}