Skip to content

Commit 801f19a

Browse files
committed
FEATURE: allow modifying the Elasticsearch request in arbitrary ways by using request()
1 parent 4abb406 commit 801f19a

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
1515
use TYPO3\Eel\ProtectedContextAwareInterface;
1616
use TYPO3\Flow\Annotations as Flow;
17+
use TYPO3\Flow\Utility\Arrays;
1718
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
1819
use TYPO3\TYPO3CR\Search\Search\QueryBuilderInterface;
1920

@@ -792,6 +793,28 @@ public function query(NodeInterface $contextNode)
792793
return $this;
793794
}
794795

796+
/**
797+
* Modify a part of the Elasticsearch Request denoted by $path, merging together
798+
* the existing values and the passed-in values.
799+
*
800+
* @param string $path
801+
* @param mixed $requestPart
802+
* @return $this
803+
*/
804+
public function request($path, $requestPart)
805+
{
806+
$valueAtPath = Arrays::getValueByPath($this->request, $path);
807+
if (is_array($valueAtPath)) {
808+
$result = Arrays::arrayMergeRecursiveOverrule($valueAtPath, $requestPart);
809+
} else {
810+
$result = $requestPart;
811+
}
812+
813+
$this->request = Arrays::setValueByPath($this->request, $path, $result);
814+
815+
return $this;
816+
}
817+
795818
/**
796819
* All methods are considered safe
797820
*

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ Furthermore, there is a more low-level operator which can be used to add arbitra
224224

225225
* `queryFilter("filterType", {option1: "value1"})`
226226

227+
At lowest level, there is the `request` operator which allows to modify the request in arbitrary manner. Note that the existing request is merged with the passed-in type in case it is an array:
228+
229+
* `request('query.filtered.query.bool.minimum_should_match', 1)`
230+
* `request('query.filtered.query.bool', {"minimum_should_match": 1})`
231+
227232
In order to debug the query more easily, the following operation is helpful:
228233

229234
* `log()` log the full query on execution into the Elasticsearch log (i.e. in `Data/Logs/ElasticSearch.log`)

Tests/Unit/Eel/ElasticSearchQueryBuilderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,32 @@ public function aCustomAggregationDefinitionCanBeApplied()
328328
$this->assertInArray($expected, $actual);
329329
}
330330

331+
/**
332+
* @test
333+
*/
334+
public function requestCanBeExtendedByArbitraryProperties()
335+
{
336+
$this->queryBuilder->request('foo.bar', ['field' => 'x']);
337+
$expected = [
338+
'bar' => ['field' => 'x']
339+
];
340+
$actual = $this->queryBuilder->getRequest();
341+
$this->assertEquals($expected, $actual['foo']);
342+
}
343+
344+
/**
345+
* @test
346+
*/
347+
public function existingRequestPropertiesCanBeOverridden()
348+
{
349+
$this->queryBuilder->limit(2);
350+
$this->queryBuilder->request('limit', 10);
351+
$expected = 10;
352+
$actual = $this->queryBuilder->getRequest();
353+
$this->assertEquals($expected, $actual['limit']);
354+
}
355+
356+
331357
/**
332358
* Test helper
333359
*

0 commit comments

Comments
 (0)