Skip to content

Commit

Permalink
Filter Facettes with should aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-sc committed Dec 11, 2024
1 parent eadb2e8 commit 58d55f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 83 deletions.
99 changes: 24 additions & 75 deletions Classes/Common/QueryParamsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,70 +101,6 @@ public function getCountQueryParams(): array

$this->setCommonParams();

/*
$params = [
//'index' => $bibIndex,
'body' => [ ]
];
if (!isset($searchParams['searchText']) || $searchParams['searchText'] == '') {
$params['body']['query'] = [
'bool' => [
'must' => [[
'match_all' => new \stdClass()
]]
]
];
} else {
// search in field "fulltext" exakt phrase match boost over all words must contain
$params['body']['query'] = [
'bool' => [
'should' => [
[
'match_phrase' => [
'tx_lisztcommon_searchable' => [
'query' => $searchParams['searchText'],
'boost' => 2.0 // boosting for exakt phrases
]
]
],
[
'query_string' => [
'query' => $searchParams['searchText'],
'fields' => ['fulltext'],
'default_operator' => 'AND'
]
]
]
]
];
}
// Todo: automate the creation of parameters
if (isset($searchParams['f_itemType']) && $searchParams['f_itemType'] !== "") {
$params['body']['query']['bool']['filter'][] = ['term' => ['itemType.keyword' => $searchParams['f_itemType']]];
}
if (isset($searchParams['f_place']) && $searchParams['f_place'] !== "") {
$params['body']['query']['bool']['filter'][] = ['term' => ['place.keyword' => $searchParams['f_place']]];
}
if (isset($searchParams['f_date']) && $searchParams['f_date'] !== "") {
$params['body']['query']['bool']['filter'][] = ['term' => ['date.keyword' => $searchParams['f_date']]];
}
// filter creators name, Todo: 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?
if (isset($searchParams['f_creators_name']) && $searchParams['f_creators_name'] !== "") {
$params['body']['query']['bool']['must'][] = [
'nested' => [
'path' => 'creators',
'query' => [
'match' => [
'creators.fullName' => $searchParams['f_creators_name']
]
]
]
];
}
*/
return $this->query;
}
private function getIndexName(): string
Expand Down Expand Up @@ -233,13 +169,13 @@ private static function getFilter(array $field): array
) {
*/
return [
'term' => [
'terms' => [
$field['name']. '.keyword' => $field['value']
]
];
//}

return [
/* return [
$field['name'] => [
'nested' => [
'path' => $field['name']
Expand All @@ -256,7 +192,7 @@ private static function getFilter(array $field): array
]
]
]
];
];*/
}

/**
Expand Down Expand Up @@ -303,17 +239,30 @@ 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,
Collection::wrap($this->params['filter'] ?? [])
->each(function($value, $key) use (&$query) {
// get array keys from $value as new array for multiple facettes
$value = array_keys($value);
// $value = array('Rochester','Bonn');

if ($key !== 'creators') {
/* $query['body']['query']['bool']['filter'][] = self::getFilter([
'name' => $key,
//'type' => $field['type'],
'type' => 'terms',
'value' => $value
]);
]);*/

// post_filter for multiple selection facettes and OR function to combine results from multiple facettes
$query['body']['post_filter']['bool']['should'][] = self::getFilter([
'name' => $key,
//'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?
Expand Down
7 changes: 4 additions & 3 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ public function indexAction(array $searchParams = []): ResponseInterface
$currentPage = 1;
}

$totalItems = $this->elasticSearchService->count($searchParams, $this->settings);
$pagination = Paginator::createPagination($currentPage, $totalItems, $this->extConf);
// $totalItems = $this->elasticSearchService->count($searchParams, $this->settings);
//$totalItems = 100;

$elasticResponse = $this->elasticSearchService->search($searchParams, $this->settings);
$pagination = Paginator::createPagination($currentPage, $elasticResponse['hits']['total']['value'], $this->extConf);

$this->view->assign('locale', $locale);
$this->view->assign('totalItems', $elasticResponse['hits']['total']['value']);
$this->view->assign('searchParams', $searchParams);
$this->view->assign('searchResults', $elasticResponse);
$this->view->assign('pagination', $pagination);
$this->view->assign('totalItems', $totalItems);
// $this->view->assign('totalItems', $totalItems);
$this->view->assign('currentString', Paginator::CURRENT_PAGE);
$this->view->assign('dots', Paginator::DOTS);

Expand Down
7 changes: 3 additions & 4 deletions Resources/Private/Partials/FilterBlock.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

<div class="filter-block frame">
<h4 class="">{key}</h4>
<f:variable name="searchParamsFilterKey">f_{key}</f:variable>
<f:variable name="paramsRemovePage">{lc:searchParams(action: 'remove', searchParamsArray: searchParams, key: 'page')}</f:variable>
<f:variable name="searchParamsFromFirstPage">{paramsRemovePage.searchParams}</f:variable>
<ul>
<f:for each="{filterGroup.buckets}" as="filter">
<li>
<f:if condition="{filter.key} && {searchParams.filter.{searchParamsFilterKey}.{filter.key}} == 1">
<f:if condition="{filter.key} && {searchParams.filter.{key}.{filter.key}} == 1">
<f:then>
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'removeFilter', searchParamsArray: searchParamsFromFirstPage, key: searchParamsFilterKey, value: filter.key)}" class="filter-item selected">
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'removeFilter', searchParamsArray: searchParamsFromFirstPage, key: key, value: filter.key)}" class="filter-item selected">
<span class="filter-item-label">{filter.key}</span>
<span class="filter-item-count">{filter.doc_count}</span>
</f:link.action>
</f:then>
<f:else if="{filter.key}">
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'addFilter', searchParamsArray: searchParamsFromFirstPage, key: searchParamsFilterKey, value: filter.key)}" class="filter-item">
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'addFilter', searchParamsArray: searchParamsFromFirstPage, key: key, value: filter.key)}" class="filter-item">
<span class="filter-item-label">{filter.key}</span>
<span class="filter-item-count">{filter.doc_count}</span>
</f:link.action>
Expand Down
1 change: 0 additions & 1 deletion Resources/Private/Templates/Search/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xmlns:i="http://typo3.org/ns/Quellenform/Iconpack/ViewHelpers"
data-namespace-typo3-fluid="true">

<f:debug>{_all}</f:debug>
<div class="searchresults-info frame">
<div class="searchresults-info-total">
<output class="searchresults-total-items">{totalItems} {f:translate(key: 'searchResults_hits_label', extensionName: 'liszt_common')}</output>
Expand Down

0 comments on commit 58d55f7

Please sign in to comment.