mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-22 17:36:11 +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)
|
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\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)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
$query = ArrayUtility::arrayMergeRecursiveOverrule($query, [
|
||||||
'stored_fields' => [ '_source' ],
|
'stored_fields' => GeneralUtility::trimExplode(',', $this->configuration->get('searching.fields.stored_fields'), true),
|
||||||
]);
|
|
||||||
$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',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
]);
|
]);
|
||||||
|
} 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)
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue