Skip to content

Commit

Permalink
Adjust filter calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Dec 11, 2024
1 parent 9189c9e commit 7b5101a
Showing 1 changed file with 50 additions and 46 deletions.
96 changes: 50 additions & 46 deletions Classes/Common/QueryParamsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,33 +226,34 @@ private static function getAggs(array $settings, string $index): array

private static function getFilter(array $field): array
{
/*
if (
isset($field['type']) &&
$field['type'] == 'terms'
) {
*/
return [
'term' => [
$field['name']. '.keyword' => $field['value']
$field['name'] . '.keyword' => $field['value']
]
];
//}
}

if (
isset($field['type']) &&
$field['type'] == 'keyword'
) {
return [
'term' => [
$field['name'] => $field['value']
]
];
}

return [
$field['name'] => [
'nested' => [
'path' => $field['name']
],
'aggs' => [
'names' => [
'terms' => [
'script' => [
'source' => $field['script'],
'lang' => 'painless'
],
'size' => 15,
]
'nested' => [
'path' => $field['name'],
'query' => [
'match' => [
$field['name'] . '.' . $field['path'] => $field['value']
]
]
]
Expand All @@ -265,7 +266,8 @@ private static function getFilter(array $field): array
private function setCommonParams(): void
{
// set index name
$this->query['index'] = $this->indexName;
$index = $this->indexName;
$this->query['index'] = $index;

// set body
if (!isset($this->params['searchText']) || $this->params['searchText'] == '') {
Expand Down Expand Up @@ -302,34 +304,36 @@ private function setCommonParams(): void
}

// set filters
$query = $this->query;
Collection::wrap($this->params)->
filter(function($_, $key) { return Str::of($key)->startsWith('f_'); })->
each(function($value, $key) use (&$query) {
$field = Str::of($key)->replace('f_', '')->__toString();
if ($field !== 'creators') {
$query['body']['query']['bool']['filter'][] = self::getFilter([
'name' => $field,
//'type' => $field['type'],
'type' => 'terms',
'value' => $value
]);
} else {
// its not a filter query because they need 100% match (with spaces from f_creators_name)
// better would be to build the field 'fullName' at build time with PHP?
$query['body']['query']['bool']['must'][] = [
'nested' => [
'path' => 'creators',
'query' => [
'match' => [
'creators.fullName' => $value
]
]
]
];
}
});
$this->query = $query;
if ($this->searchAll == false) {
$filterTypes = Collection::wrap($this->settings)->
recursive()->
get('entityTypes')->
filter(function($entityType) use ($index) {return $entityType->get('indexName') === $index;})->
values()->
get(0)->
get('filters')->
mapWithKeys(function($filter) { return [
$filter['field'] => [
'type' => $filter['type'],
'path' => isset($filter['path']) ? $filter['path'] : ''
]];
})->
all();

$query = $this->query;
Collection::wrap($this->params)->
filter(function($_, $key) { return Str::of($key)->startsWith('f_'); })->
each(function($value, $key) use (&$query, $filterTypes) {
$field = Str::of($key)->replace('f_', '')->__toString();
$query['body']['query']['bool']['filter'][] = self::getFilter([
'name' => $field,
'type' => $filterTypes[$field]['type'],
'value' => $value,
'path' => $filterTypes[$field]['path']
]);
});
$this->query = $query;
}

}

Expand Down

0 comments on commit 7b5101a

Please sign in to comment.