WIP|FEATURE: Allow fields and sorting to be configurable

This commit is contained in:
Daniel Siepmann 2017-10-23 16:35:38 +02:00
parent b1f81c0d3b
commit 07a4fec622
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 39 additions and 31 deletions

View file

@ -92,7 +92,8 @@ class SearchRequest implements SearchRequestInterface
*/ */
public function setFilter(array $filter) public function setFilter(array $filter)
{ {
$this->filter = array_filter($filter); $filter = \TYPO3\CMS\Core\Utility\ArrayUtility::removeArrayEntryByValue($filter, '');
$this->filter = \TYPO3\CMS\Extbase\Utility\ArrayUtility::removeEmptyElementsRecursively($filter);
} }
/** /**

View file

@ -25,7 +25,9 @@ use Codappix\SearchCore\Configuration\InvalidArgumentException;
use Codappix\SearchCore\Connection\ConnectionInterface; use Codappix\SearchCore\Connection\ConnectionInterface;
use Codappix\SearchCore\Connection\Elasticsearch\Query; use Codappix\SearchCore\Connection\Elasticsearch\Query;
use Codappix\SearchCore\Connection\SearchRequestInterface; use Codappix\SearchCore\Connection\SearchRequestInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\ArrayUtility; use TYPO3\CMS\Extbase\Utility\ArrayUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
class QueryFactory class QueryFactory
{ {
@ -152,40 +154,28 @@ class QueryFactory
protected function addFields(SearchRequestInterface $searchRequest, array &$query) protected function addFields(SearchRequestInterface $searchRequest, array &$query)
{ {
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [ try {
'stored_fields' => [ '_source' ], $query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
]); 'stored_fields' => GeneralUtility::trimExplode(',', $this->configuration->get('searching.fields.stored_fields'), true),
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [ ]);
'script_fields' => [ } catch (InvalidArgumentException $e) {
'distance' => [ // Nothing configured
'script' => [ }
'params' => [
'lat' => 51.168098, try {
'lon' => 6.381384, $scriptFields = $this->configuration->get('searching.fields.script_fields');
], $scriptFields = $this->replaceArrayValuesWithRequestContent($searchRequest, $scriptFields);
'lang' => 'painless', $query = ArrayUtility::arrayMergeRecursiveOverrule($query, ['script_fields' => $scriptFields]);
'inline' => 'doc["location"].arcDistance(params.lat,params.lon) * 0.001', } catch (InvalidArgumentException $e) {
], // Nothing configured
], }
],
]);
} }
protected function addSort(SearchRequestInterface $searchRequest, array &$query) protected function addSort(SearchRequestInterface $searchRequest, array &$query)
{ {
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [ $sorting = $this->configuration->getIfExists('searching.sort') ?: [];
'sort' => [ $sorting = $this->replaceArrayValuesWithRequestContent($searchRequest, $sorting);
'_geo_distance' => [ $query = ArrayUtility::arrayMergeRecursiveOverrule($query, ['sort' => $sorting]);
'location' => [
'lat' => 51.168098,
'lon' => 6.381384,
],
'order' => 'asc',
'unit' => 'km',
'distance_type' => 'plane',
],
],
]);
} }
protected function addFilter(SearchRequestInterface $searchRequest, array &$query) protected function addFilter(SearchRequestInterface $searchRequest, array &$query)
@ -247,4 +237,21 @@ class QueryFactory
]); ]);
} }
} }
protected function replaceArrayValuesWithRequestContent(SearchRequestInterface $searchRequest, array $array) : array
{
array_walk_recursive($array, function (&$value, $key, SearchRequestInterface $searchRequest) {
$template = new StandaloneView();
$template->assign('request', $searchRequest);
$template->setTemplateSource($value);
$value = $template->render();
// As elasticsearch does need some doubles to be end as doubles.
if (is_numeric($value)) {
$value = (float) $value;
}
}, $searchRequest);
return $array;
}
} }