Skip to content

Commit

Permalink
Harmonize pagination with filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Nov 21, 2024
1 parent 234f0b1 commit f39f60f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
73 changes: 73 additions & 0 deletions Classes/Common/QueryParamsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class QueryParamsBuilder
public static function createElasticParams(array $searchParams): array
{
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_bibliography');
$commonConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_common');
$bibIndex = $extConf['elasticIndexName'];
$aggs = []; // TYPOSCRIPT stuff here

Expand Down Expand Up @@ -128,9 +129,81 @@ public static function createElasticParams(array $searchParams): array
]
];
}
if (isset($searchParams['searchParamsPage']) && $searchParams['searchParamsPage'] !== "") {
$params['from'] = ($searchParams['searchParamsPage'] - 1) * $commonConf['itemsPerPage'];
}

return $params;
}

public static function createCountParams(array $searchParams): array
{

$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('liszt_bibliography');
$bibIndex = $extConf['elasticIndexName'];

$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 $params;
}
}
8 changes: 8 additions & 0 deletions Classes/Services/ElasticSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public function search($searchParams): Collection
return new Collection($response->asArray());
}

public function count($searchParams): int
{
$this->init();
$this->params = QueryParamsBuilder::createCountParams($searchParams);
$response = $this->client->count($this->params);
return $response['count'];
}

}
4 changes: 3 additions & 1 deletion Classes/ViewHelpers/SearchParamsViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public static function renderStatic(
}

// Convert the array to a string formatted as {key: 'value', key2: 'value2'}
/*
$formattedParams = [];
foreach ($searchParamsArray as $paramKey => $paramValue) {
$formattedParams[] = "{$paramKey}: '" . $paramValue . "'";
}
*/

// return '{' . implode(', ', $formattedParams) . '}';
return ['searchParams' => $searchParamsArray];
return ['searchParams' => $searchParamsArray];

}
}
6 changes: 4 additions & 2 deletions Resources/Private/Partials/FilterBlock.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,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: 'searchParamsPage')}</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.{searchParamsFilterKey}} == {filter.key}">
<f:then>
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'remove', searchParamsArray: searchParams, key: searchParamsFilterKey)}" class="filter-item selected">
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'remove', searchParamsArray: searchParamsFromFirstPage, key: searchParamsFilterKey)}" 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: 'add', searchParamsArray: searchParams, key: searchParamsFilterKey, value: filter.key)}" class="filter-item">
<f:link.action action="index" controller="Search" pluginName="SearchListing" arguments="{lc:searchParams(action: 'add', searchParamsArray: searchParamsFromFirstPage, key: searchParamsFilterKey, 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

0 comments on commit f39f60f

Please sign in to comment.