mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-22 11:56:12 +01:00
WIP|FEATURE: Allow fields and sorting to be configurable
This commit is contained in:
parent
b1f81c0d3b
commit
07a4fec622
2 changed files with 39 additions and 31 deletions
|
@ -92,7 +92,8 @@ class SearchRequest implements SearchRequestInterface
|
|||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,7 +25,9 @@ use Codappix\SearchCore\Configuration\InvalidArgumentException;
|
|||
use Codappix\SearchCore\Connection\ConnectionInterface;
|
||||
use Codappix\SearchCore\Connection\Elasticsearch\Query;
|
||||
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Fluid\View\StandaloneView;
|
||||
|
||||
class QueryFactory
|
||||
{
|
||||
|
@ -152,40 +154,28 @@ class QueryFactory
|
|||
|
||||
protected function addFields(SearchRequestInterface $searchRequest, array &$query)
|
||||
{
|
||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||
'stored_fields' => [ '_source' ],
|
||||
]);
|
||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||
'script_fields' => [
|
||||
'distance' => [
|
||||
'script' => [
|
||||
'params' => [
|
||||
'lat' => 51.168098,
|
||||
'lon' => 6.381384,
|
||||
],
|
||||
'lang' => 'painless',
|
||||
'inline' => 'doc["location"].arcDistance(params.lat,params.lon) * 0.001',
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
try {
|
||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||
'stored_fields' => GeneralUtility::trimExplode(',', $this->configuration->get('searching.fields.stored_fields'), true),
|
||||
]);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// Nothing configured
|
||||
}
|
||||
|
||||
try {
|
||||
$scriptFields = $this->configuration->get('searching.fields.script_fields');
|
||||
$scriptFields = $this->replaceArrayValuesWithRequestContent($searchRequest, $scriptFields);
|
||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, ['script_fields' => $scriptFields]);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// Nothing configured
|
||||
}
|
||||
}
|
||||
|
||||
protected function addSort(SearchRequestInterface $searchRequest, array &$query)
|
||||
{
|
||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||
'sort' => [
|
||||
'_geo_distance' => [
|
||||
'location' => [
|
||||
'lat' => 51.168098,
|
||||
'lon' => 6.381384,
|
||||
],
|
||||
'order' => 'asc',
|
||||
'unit' => 'km',
|
||||
'distance_type' => 'plane',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$sorting = $this->configuration->getIfExists('searching.sort') ?: [];
|
||||
$sorting = $this->replaceArrayValuesWithRequestContent($searchRequest, $sorting);
|
||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, ['sort' => $sorting]);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue