-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathElasticSearchQueryBuilder.php
More file actions
1078 lines (944 loc) · 35.8 KB
/
ElasticSearchQueryBuilder.php
File metadata and controls
1078 lines (944 loc) · 35.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\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\Driver\QueryInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Dto\SearchResult;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
use Neos\Flow\Persistence\QueryResultInterface;
use Psr\Log\LoggerInterface;
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Search\Search\QueryBuilderInterface;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Utility\Now;
use Neos\Utility\Arrays;
/**
* Query Builder for ElasticSearch Queries
*/
class ElasticSearchQueryBuilder implements QueryBuilderInterface, ProtectedContextAwareInterface
{
/**
* @Flow\Inject
* @var ElasticSearchClient
*/
protected $elasticSearchClient;
/**
* @Flow\Inject
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var ThrowableStorageInterface
*/
protected $throwableStorage;
/**
* @var boolean
*/
protected $logThisQuery = false;
/**
* @var string
*/
protected $logMessage;
/**
* @var integer
*/
protected $limit;
/**
* @var integer
*/
protected $from;
/**
* @Flow\Inject(lazy=false)
* @var Now
*/
protected $now;
/**
* This (internal) array stores, for the last search request, a mapping from Node Identifiers
* to the full Elasticsearch Hit which was returned.
*
* This is needed to e.g. use result highlighting.
*
* @var array
*/
protected $elasticSearchHitsIndexedByNodeFromLastRequest;
/**
* The Elasticsearch request, as it is being built up.
*
* @var QueryInterface
* @Flow\Inject
*/
protected $request;
/**
* @var array
*/
protected $result = [];
/**
* HIGH-LEVEL API
*/
/**
* Filter by node type, taking inheritance into account.
*
* @param string $nodeType the node type to filter for
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function nodeType(string $nodeType): QueryBuilderInterface
{
// on indexing, neos_type_and_supertypes contains the typename itself and all supertypes, so that's why we can
// use a simple term filter here.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html
return $this->queryFilter('term', ['neos_type_and_supertypes' => $nodeType]);
}
/**
* Sort descending by $propertyName
*
* @param string $propertyName the property name to sort by
* @return ElasticSearchQueryBuilder
* @api
*/
public function sortDesc(string $propertyName): QueryBuilderInterface
{
$configuration = [
$propertyName => ['order' => 'desc']
];
$this->sort($configuration);
return $this;
}
/**
* Sort ascending by $propertyName
*
* @param string $propertyName the property name to sort by
* @return ElasticSearchQueryBuilder
* @api
*/
public function sortAsc(string $propertyName): QueryBuilderInterface
{
$configuration = [
$propertyName => ['order' => 'asc']
];
$this->sort($configuration);
return $this;
}
/**
* Add a $configuration sort filter to the request
*
* @param array $configuration
* @return ElasticSearchQueryBuilder
* @api
*/
public function sort($configuration): ElasticSearchQueryBuilder
{
$this->request->addSortFilter($configuration);
return $this;
}
/**
* output only $limit records
*
* Internally, we fetch $limit*$workspaceNestingLevel records, because we fetch the *conjunction* of all workspaces;
* and then we filter after execution when we have found the right number of results.
*
* This algorithm can be re-checked when https://github.com/elasticsearch/elasticsearch/issues/3300 is merged.
*
* @param integer $limit
* @return ElasticSearchQueryBuilder
* @throws IllegalObjectTypeException
* @api
*/
public function limit($limit): QueryBuilderInterface
{
if ($limit === null) {
return $this;
}
$currentWorkspaceNestingLevel = 1;
$workspace = $this->elasticSearchClient->getContextNode()->getContext()->getWorkspace();
while ($workspace->getBaseWorkspace() !== null) {
$currentWorkspaceNestingLevel++;
$workspace = $workspace->getBaseWorkspace();
}
$this->limit = $limit;
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html
$this->request->size($limit * $currentWorkspaceNestingLevel);
return $this;
}
/**
* output records starting at $from
*
* @param integer $from
* @return ElasticSearchQueryBuilder
* @api
*/
public function from($from): QueryBuilderInterface
{
if (!$from) {
return $this;
}
$this->from = $from;
$this->request->from($from);
return $this;
}
/**
* add an exact-match query for a given property
*
* @param string $propertyName Name of the property
* @param mixed $value Value for comparison
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function exactMatch(string $propertyName, $value): QueryBuilderInterface
{
return $this->queryFilter('term', [$propertyName => $this->convertValue($value)]);
}
/**
* @param string $propertyName
* @param mixed $value
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
*/
public function exclude(string $propertyName, $value): ElasticSearchQueryBuilder
{
return $this->queryFilter('term', [$propertyName => $this->convertValue($value)], 'must_not');
}
/**
* add a range filter (gt) for the given property
*
* @param string $propertyName Name of the property
* @param mixed $value Value for comparison
* @param string $clauseType one of must, should, must_not
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function greaterThan(string $propertyName, $value, string $clauseType = 'must'): ElasticSearchQueryBuilder
{
return $this->queryFilter('range', [$propertyName => ['gt' => $this->convertValue($value)]], $clauseType);
}
/**
* add a range filter (gte) for the given property
*
* @param string $propertyName Name of the property
* @param mixed $value Value for comparison
* @param string $clauseType one of must, should, must_not
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function greaterThanOrEqual(string $propertyName, $value, string $clauseType = 'must'): ElasticSearchQueryBuilder
{
return $this->queryFilter('range', [$propertyName => ['gte' => $this->convertValue($value)]], $clauseType);
}
/**
* add a range filter (lt) for the given property
*
* @param string $propertyName Name of the property
* @param mixed $value Value for comparison
* @param string $clauseType one of must, should, must_not
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function lessThan(string $propertyName, $value, string $clauseType = 'must'): ElasticSearchQueryBuilder
{
return $this->queryFilter('range', [$propertyName => ['lt' => $this->convertValue($value)]], $clauseType);
}
/**
* add a range filter (lte) for the given property
*
* @param string $propertyName Name of the property
* @param mixed $value Value for comparison
* @param string $clauseType one of must, should, must_not
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function lessThanOrEqual(string $propertyName, $value, string $clauseType = 'must'): ElasticSearchQueryBuilder
{
return $this->queryFilter('range', [$propertyName => ['lte' => $this->convertValue($value)]], $clauseType);
}
/**
* LOW-LEVEL API
*/
/**
* Add a filter to query.filtered.filter
*
* @param string $filterType
* @param mixed $filterOptions
* @param string $clauseType one of must, should, must_not
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function queryFilter(string $filterType, $filterOptions, string $clauseType = 'must'): ElasticSearchQueryBuilder
{
$this->request->queryFilter($filterType, $filterOptions, $clauseType);
return $this;
}
/**
* Append $data to the given array at $path inside $this->request.
*
* Low-level method to manipulate the Elasticsearch Query
*
* @param string $path
* @param array $data
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
*/
public function appendAtPath(string $path, array $data): ElasticSearchQueryBuilder
{
$this->request->appendAtPath($path, $data);
return $this;
}
/**
* Add multiple filters to query.filtered.filter
*
* Example Usage:
*
* searchFilter = Neos.Fusion:RawArray {
* author = 'Max'
* tags = Neos.Fusion:RawArray {
* 0 = 'a'
* 1 = 'b'
* }
* }
*
* searchQuery = ${Search.queryFilterMultiple(this.searchFilter)}
*
* @param array $data An associative array of keys as variable names and values as variable values
* @param string $clauseType one of must, should, must_not
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @api
*/
public function queryFilterMultiple(array $data, $clauseType = 'must'): ElasticSearchQueryBuilder
{
foreach ($data as $key => $value) {
if ($value !== null) {
if (is_array($value)) {
$this->queryFilter('terms', [$key => array_values($value)], $clauseType);
} else {
$this->queryFilter('term', [$key => $value], $clauseType);
}
}
}
return $this;
}
/**
* This method adds a field based aggregation configuration. This can be used for simple
* aggregations like terms
*
* Example Usage to create a terms aggregation for a property color:
* nodes = ${Search....fieldBasedAggregation("colors", "color").execute()}
*
* Access all aggregation data with {nodes.aggregations} in your fluid template
*
* @param string $name The name to identify the resulting aggregation
* @param string $field The field to aggregate by
* @param string $type Aggregation type
* @param string $parentPath
* @param int|null $size The amount of buckets to return or null if not applicable to the aggregation
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
*/
public function fieldBasedAggregation(string $name, string $field, string $type = 'terms', string $parentPath = '', ?int $size = null): ElasticSearchQueryBuilder
{
$aggregationDefinition = [
$type => [
'field' => $field
]
];
if ($size !== null) {
$aggregationDefinition[$type]['size'] = $size;
}
$this->aggregation($name, $aggregationDefinition, $parentPath);
return $this;
}
/**
* This method is used to create any kind of aggregation.
*
* Example Usage to create a terms aggregation for a property color:
*
* aggregationDefinition = Neos.Fusion:DataStructure {
* terms {
* field = "color"
* }
* }
*
* nodes = ${Search....aggregation("color", this.aggregationDefinition).execute()}
*
* Access all aggregation data with {nodes.aggregations} in your fluid template
*
* @param string $name
* @param array $aggregationDefinition
* @param string $parentPath
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
*/
public function aggregation(string $name, array $aggregationDefinition, string $parentPath = ''): ElasticSearchQueryBuilder
{
$this->request->aggregation($name, $aggregationDefinition, $parentPath);
return $this;
}
/**
* This method is used to create a simple term suggestion.
*
* Example Usage of a term suggestion
*
* nodes = ${Search....termSuggestions("aTerm")}
*
* Access all suggestions data with ${Search....getSuggestions()}
*
* @param string $text
* @param string $field
* @param string $name
* @return ElasticSearchQueryBuilder
*/
public function termSuggestions(string $text, string $field = 'neos_fulltext.text', string $name = 'suggestions'): ElasticSearchQueryBuilder
{
$suggestionDefinition = [
'text' => $text,
'term' => [
'field' => $field
]
];
$this->suggestions($name, $suggestionDefinition);
return $this;
}
/**
* This method is used to create any kind of suggestion.
*
* Example Usage of a term suggestion for the fulltext search
*
* suggestionDefinition = Neos.Fusion:RawArray {
* text = "some text"
* terms = Neos.Fusion:RawArray {
* field = "body"
* }
* }
*
* nodes = ${Search....suggestion("my-suggestions", this.suggestionDefinition).execute()}
*
* Access all suggestions data with {nodes.suggestions} in your fluid template
*
* @param string $name
* @param array $suggestionDefinition
* @return ElasticSearchQueryBuilder
*/
public function suggestions(string $name, array $suggestionDefinition): ElasticSearchQueryBuilder
{
$this->request->suggestions($name, $suggestionDefinition);
return $this;
}
/**
* Get the Elasticsearch request as we need it
*
* @return QueryInterface
*/
public function getRequest(): QueryInterface
{
return $this->request;
}
/**
* Log the current request to the Elasticsearch log for debugging after it has been executed.
*
* @param string $message an optional message to identify the log entry
* @return ElasticSearchQueryBuilder
* @api
*/
public function log($message = null): ElasticSearchQueryBuilder
{
$this->logThisQuery = true;
$this->logMessage = $message;
return $this;
}
/**
* @return int
*/
public function getTotalItems(): int
{
return $this->evaluateResult($this->result)->getTotal();
}
/**
* @return int
*/
public function getLimit(): int
{
return $this->limit;
}
/**
* @return int
*/
public function getFrom(): int
{
return $this->from;
}
/**
* This low-level method can be used to look up the full Elasticsearch hit given a certain node.
*
* @param NodeInterface $node
* @return array the Elasticsearch hit for the node as array, or NULL if it does not exist.
*/
public function getFullElasticSearchHitForNode(NodeInterface $node): ?array
{
return $this->elasticSearchHitsIndexedByNodeFromLastRequest[$node->getIdentifier()] ?? null;
}
/**
* Execute the query and return the list of nodes as result.
*
* This method is rather internal; just to be called from the ElasticSearchQueryResult. For the public API, please use execute()
*
* @return array<\Neos\ContentRepository\Domain\Model\NodeInterface>
* @throws Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
*/
public function fetch(): array
{
try {
$timeBefore = microtime(true);
$request = $this->request->getRequestAsJson();
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_search', [], $request);
$timeAfterwards = microtime(true);
$this->result = $response->getTreatedContent();
$searchResult = $this->evaluateResult($this->result);
$this->result['nodes'] = [];
$this->logThisQuery && $this->logger->debug(sprintf('Query Log (%s): Indexname: %s %s -- execution time: %s ms -- Limit: %s -- Number of results returned: %s -- Total Results: %s', $this->logMessage, $this->getIndexName(), $request, (($timeAfterwards - $timeBefore) * 1000), $this->limit, count($searchResult->getHits()), $searchResult->getTotal()), LogEnvironment::fromMethodName(__METHOD__));
if (count($searchResult->getHits()) > 0) {
$this->result['nodes'] = $this->convertHitsToNodes($searchResult->getHits());
}
} catch (ApiException $exception) {
$message = $this->throwableStorage->logThrowable($exception);
$this->logger->error(sprintf('Request failed with %s', $message), LogEnvironment::fromMethodName(__METHOD__));
$this->result['nodes'] = [];
}
return $this->result;
}
/**
* @param array $result
* @return SearchResult
*/
protected function evaluateResult(array $result): SearchResult
{
return new SearchResult(
$hits = $result['hits']['hits'] ?? [],
$total = $result['hits']['total']['value'] ?? 0
);
}
/**
* Get a query result object for lazy execution of the query
*
* @param bool $cacheResult
* @return ElasticSearchQueryResult
* @throws \JsonException
* @api
*/
public function execute(bool $cacheResult = true): \Traversable
{
$elasticSearchQuery = new ElasticSearchQuery($this);
return $elasticSearchQuery->execute($cacheResult);
}
/**
* Get a uncached query result object for lazy execution of the query
*
* @return ElasticSearchQueryResult
* @throws \JsonException
* @api
*/
public function executeUncached(): ElasticSearchQueryResult
{
$elasticSearchQuery = new ElasticSearchQuery($this);
return $elasticSearchQuery->execute();
}
/**
* Return the total number of hits for the query.
*
* @return integer
* @throws Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
* @api
*/
public function count(): int
{
$timeBefore = microtime(true);
$request = $this->getRequest()->getCountRequestAsJson();
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_count', [], $request);
$timeAfterwards = microtime(true);
$treatedContent = $response->getTreatedContent();
$count = (int)$treatedContent['count'];
$this->logThisQuery && $this->logger->debug('Count Query Log (' . $this->logMessage . '): Indexname: ' . $this->getIndexName() . ' ' . $request . ' -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . $count, LogEnvironment::fromMethodName(__METHOD__));
return $count;
}
/**
* Match the searchword against the fulltext index.
*
* NOTE: Please use {@see simpleQueryStringFulltext} instead, as it is more robust.
*
* @param string $searchWord
* @param array $options Options to configure the query_string, see https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html
* @return QueryBuilderInterface
* @throws \JsonException
* @api
*/
public function fulltext(string $searchWord, array $options = []): QueryBuilderInterface
{
$encodedSearchWord = json_encode($searchWord, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
// remove quotes added by json_encode
$encodedSearchWord = \substr($encodedSearchWord, 1, \strlen($encodedSearchWord)-2);
// We automatically enable result highlighting when doing fulltext searches. It is up to the user to use this information or not use it.
$this->request->fulltext($encodedSearchWord, $options);
$this->request->highlight(150, 2);
return $this;
}
/**
* Match the searchword against the fulltext index using the elasticsearch
* [simple query string query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html).
*
* This method has two main benefits over {@see fulltext()}:
* - Supports phrase searches like `"Neos and Flow"`, where
* quotes mean "I want this exact phrase" (similar to many search engines).
* - do not crash if the user does not enter a fully syntactically valid query
* (invalid query parts are ignored).
*
* This is exactly what we want for a good search field behavior.
*
* @param string $searchWord
* @param array $options Options to configure the query_string, see https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html
* @return QueryBuilderInterface
* @api
*/
public function simpleQueryStringFulltext(string $searchWord, array $options = []): QueryBuilderInterface
{
// We automatically enable result highlighting when doing fulltext searches. It is up to the user to use this information or not use it.
$this->request->simpleQueryStringFulltext($searchWord, $options);
$this->request->highlight(150, 2);
return $this;
}
/**
* Adds a prefix filter to the query
* See: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-prefix-query.html
*
* @param string $propertyName
* @param string $prefix
* @param string $clauseType one of must, should, must_not
* @return $this|QueryBuilderInterface
* @throws QueryBuildingException
*/
public function prefix(string $propertyName, string $prefix, string $clauseType = 'must'): QueryBuilderInterface
{
$this->request->queryFilter('prefix', [$propertyName => $prefix], $clauseType);
return $this;
}
/**
* Filters documents that include only hits that exists within a specific distance from a geo point.
*
* @param string $propertyName
* @param string|array $geoPoint Either ['lon' => x.x, 'lat' => y.y], [lon, lat], 'lat,lon', or GeoHash
* @param string $distance Distance with unit. See: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/common-options.html#distance-units
* @param string $clauseType one of must, should, must_not
* @return QueryBuilderInterface
* @throws QueryBuildingException
*/
public function geoDistance(string $propertyName, $geoPoint, string $distance, string $clauseType = 'must'): QueryBuilderInterface
{
$this->queryFilter('geo_distance', [
'distance' => $distance,
$propertyName => $geoPoint,
], $clauseType);
return $this;
}
/**
* Configure Result Highlighting. Only makes sense in combination with fulltext(). By default, highlighting is enabled.
* It can be disabled by calling "highlight(FALSE)".
*
* @param int|bool $fragmentSize The result fragment size for highlight snippets. If this parameter is FALSE, highlighting will be disabled.
* @param int|null $fragmentCount The number of highlight fragments to show.
* @param int $noMatchSize
* @param string $field
* @return ElasticSearchQueryBuilder
* @api
*/
public function highlight($fragmentSize, int $fragmentCount = null, int $noMatchSize = 150, string $field = 'neos_fulltext.*'): ElasticSearchQueryBuilder
{
$this->request->highlight($fragmentSize, $fragmentCount, $noMatchSize, $field);
return $this;
}
/**
* This method is used to define a more like this query.
* The More Like This Query (MLT Query) finds documents that are "like" a given set of documents.
* See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-mlt-query.html
*
* @param array $like An array of strings or documents
* @param array $fields Fields to compare other docs with
* @param array $options Additional options for the more_like_this quey
* @return ElasticSearchQueryBuilder
*/
public function moreLikeThis(array $like, array $fields = [], array $options = []): ElasticSearchQueryBuilder
{
$like = is_array($like) ? $like : [$like];
$getDocumentDefinitionByNode = function (QueryInterface $request, NodeInterface $node): array {
$request->queryFilter('term', ['neos_node_identifier' => $node->getIdentifier()]);
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_search', [], $request->toArray())->getTreatedContent();
$respondedDocuments = Arrays::getValueByPath($response, 'hits.hits');
if (count($respondedDocuments) === 0) {
$this->logger->info(sprintf('The node with identifier %s was not found in the elasticsearch index.', $node->getIdentifier()), LogEnvironment::fromMethodName(__METHOD__));
return [];
}
$respondedDocument = current($respondedDocuments);
return [
'_id' => $respondedDocument['_id'],
'_index' => $respondedDocument['_index'],
];
};
$processedLike = [];
foreach ($like as $key => $likeElement) {
if ($likeElement instanceof NodeInterface) {
$documentDefinition = $getDocumentDefinitionByNode(clone $this->request, $likeElement);
if (!empty($documentDefinition)) {
$processedLike[] = $documentDefinition;
}
} else {
$processedLike[] = $likeElement;
}
}
$processedLike = array_filter($processedLike);
if (!empty($processedLike)) {
$this->request->moreLikeThis($processedLike, $fields, $options);
}
return $this;
}
/**
* Sets the starting point for this query. Search result should only contain nodes that
* match the context of the given node and have it as parent node in their rootline.
*
* @param NodeInterface $contextNode
* @return ElasticSearchQueryBuilder
* @throws QueryBuildingException
* @throws IllegalObjectTypeException
* @api
*/
public function query(NodeInterface $contextNode): QueryBuilderInterface
{
$this->elasticSearchClient->setContextNode($contextNode);
// on indexing, the neos_parent_path is tokenized to contain ALL parent path parts,
// e.g. /foo, /foo/bar/, /foo/bar/baz; to speed up matching.. That's why we use a simple "term" filter here.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html
// another term filter against the path allows the context node itself to be found
$this->queryFilter('bool', [
'should' => [
[
'term' => ['neos_parent_path' => $contextNode->getPath()]
],
[
'term' => ['neos_path' => $contextNode->getPath()]
]
]
]);
$workspaces = array_merge(
[$contextNode->getContext()->getWorkspace()->getName()],
array_keys($contextNode->getContext()->getWorkspace()->getBaseWorkspaces())
);
//
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html
$this->queryFilter('terms', ['neos_workspace' => $workspaces]);
return $this;
}
/**
* Modify a part of the Elasticsearch Request denoted by $path, merging together
* the existing values and the passed-in values.
*
* @param string $path
* @param mixed $requestPart
* @return ElasticSearchQueryBuilder
*/
public function request(string $path, $requestPart): ElasticSearchQueryBuilder
{
$this->request->setByPath($path, $requestPart);
return $this;
}
/**
* All methods are considered safe
*
* @param string $methodName
* @return bool
*/
public function allowsCallOfMethod($methodName)
{
return true;
}
/**
* @param array $hits
* @return array Array of Node objects
*/
protected function convertHitsToNodes(array $hits): array
{
$nodes = [];
$elasticSearchHitPerNode = [];
$notConvertedNodePaths = [];
/**
* TODO: This code below is not fully correct yet:
*
* We always fetch $limit * (numberOfWorkspaces) records; so that we find a node:
* - *once* if it is only in live workspace and matches the query
* - *once* if it is only in user workspace and matches the query
* - *twice* if it is in both workspaces and matches the query *both times*. In this case we filter the duplicate record.
* - *once* if it is in the live workspace and has been DELETED in the user workspace (STILL WRONG)
* - *once* if it is in the live workspace and has been MODIFIED to NOT MATCH THE QUERY ANYMORE in user workspace (STILL WRONG)
*
* If we want to fix this cleanly, we'd need to do an *additional query* in order to filter all nodes from a non-user workspace
* which *do exist in the user workspace but do NOT match the current query*. This has to be done somehow "recursively"; and later
* we might be able to use https://github.com/elasticsearch/elasticsearch/issues/3300 as soon as it is merged.
*/
foreach ($hits as $hit) {
$nodePath = $hit[isset($hit['fields']['neos_path']) ? 'fields' : '_source']['neos_path'];
if (is_array($nodePath)) {
$nodePath = current($nodePath);
}
$node = $this->elasticSearchClient->getContextNode()->getNode($nodePath);
if (!$node instanceof NodeInterface) {
$notConvertedNodePaths[] = $nodePath;
continue;
}
if (isset($nodes[$node->getIdentifier()])) {
continue;
}
$nodes[$node->getIdentifier()] = $node;
$elasticSearchHitPerNode[$node->getIdentifier()] = $hit;
if ($this->limit > 0 && count($nodes) >= $this->limit) {
break;
}
}
$this->logThisQuery && $this->logger->debug(sprintf('[%s] Returned %s nodes.', $this->logMessage, count($nodes)), LogEnvironment::fromMethodName(__METHOD__));
if (!empty($notConvertedNodePaths)) {
$this->logger->warning(sprintf('[%s] Search resulted in %s hits but only %s hits could be converted to nodes. Nodes with paths "%s" could not have been converted.', $this->logMessage, count($hits), count($nodes), implode(', ', $notConvertedNodePaths)), LogEnvironment::fromMethodName(__METHOD__));
}
$this->elasticSearchHitsIndexedByNodeFromLastRequest = $elasticSearchHitPerNode;
return array_values($nodes);
}
/**
* This method will get the minimum of all allowed cache lifetimes for the
* nodes that would result from the current defined query. This means it will evaluate to the nearest future value of the
* hiddenBeforeDateTime or hiddenAfterDateTime properties of all nodes in the result.
*
* @return int
* @throws Exception
* @throws QueryBuildingException
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
*/
public function cacheLifetime(): int
{
$minTimestamps = array_filter([
$this->getNearestFutureDate('neos_hidden_before_datetime'),
$this->getNearestFutureDate('neos_hidden_after_datetime')
], static function ($value) {
return $value !== 0;
});
if (empty($minTimestamps)) {
return 0;
}
$minTimestamp = min($minTimestamps);
return $minTimestamp - $this->now->getTimestamp();
}
/**
* @param string $dateField
* @return int
* @throws Exception
* @throws QueryBuildingException
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
*/
protected function getNearestFutureDate(string $dateField): int
{
$request = clone $this->request;
$convertDateResultToTimestamp = static function (array $dateResult): int {
if (!isset($dateResult['value_as_string'])) {
return 0;
}
return (new \DateTime($dateResult['value_as_string']))->getTimestamp();