first commit

This commit is contained in:
Sven Wappler
2021-04-17 00:26:33 +02:00
commit 866c63cc63
813 changed files with 100696 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\Options;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\AbstractOptionFacetItem;
/**
* Value object that represent an option of a options facet.
*
* @author Frans Saris <frans@beech.it>
* @author Timo Hund <timo.hund@dkd.de>
*/
class Option extends AbstractOptionFacetItem
{
/**
* @param OptionsFacet $facet
* @param string $label
* @param string $value
* @param int $documentCount
* @param bool $selected
* @param array $metrics
*/
public function __construct(OptionsFacet $facet, $label = '', $value = '', $documentCount = 0, $selected = false, $metrics = [])
{
parent::__construct($facet, $label, $value, $documentCount, $selected, $metrics);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\Options;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\AbstractOptionsFacet;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
/**
* Value object that represent a options facet.
*
* @author Frans Saris <frans@beech.it>
* @author Timo Hund <timo.hund@dkd.de>
*/
class OptionsFacet extends AbstractOptionsFacet
{
const TYPE_OPTIONS = 'options';
/**
* String
* @var string
*/
protected static $type = self::TYPE_OPTIONS;
/**
* OptionsFacet constructor
*
* @param SearchResultSet $resultSet
* @param string $name
* @param string $field
* @param string $label
* @param array $configuration Facet configuration passed from typoscript
*/
public function __construct(SearchResultSet $resultSet, $name, $field, $label = '', array $configuration = [])
{
parent::__construct($resultSet, $name, $field, $label, $configuration);
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\Options;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\AbstractFacetParser;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
use WapplerSystems\Meilisearch\System\Solr\ResponseAdapter;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
/**
* Class OptionsFacetParser
*/
class OptionsFacetParser extends AbstractFacetParser
{
/**
* @var Dispatcher
*/
protected $dispatcher;
/**
* @param Dispatcher $dispatcher
*/
public function injectDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* @param SearchResultSet $resultSet
* @param string $facetName
* @param array $facetConfiguration
* @return OptionsFacet|null
*/
public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration)
{
$response = $resultSet->getResponse();
$fieldName = $facetConfiguration['field'];
$label = $this->getPlainLabelOrApplyCObject($facetConfiguration);
$optionsFromSolrResponse = $this->getOptionsFromSolrResponse($facetName, $response);
$metricsFromSolrResponse = $this->getMetricsFromSolrResponse($facetName, $response);
$optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName);
$hasOptionsInResponse = !empty($optionsFromSolrResponse);
$hasSelectedOptionsInRequest = count($optionsFromRequest) > 0;
$hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest;
$hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName);
if ($hasNoOptionsToShow && $hideEmpty) {
return null;
}
/** @var $facet OptionsFacet */
$facet = $this->objectManager->get(
OptionsFacet::class,
$resultSet,
$facetName,
$fieldName,
$label,
$facetConfiguration
);
$hasActiveOptions = count($optionsFromRequest) > 0;
$facet->setIsUsed($hasActiveOptions);
$facet->setIsAvailable($hasOptionsInResponse);
$optionsToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest);
foreach ($optionsToCreate as $optionsValue => $count) {
if ($this->getIsExcludedFacetValue($optionsValue, $facetConfiguration)) {
continue;
}
$isOptionsActive = in_array($optionsValue, $optionsFromRequest);
$label = $this->getLabelFromRenderingInstructions($optionsValue, $count, $facetName, $facetConfiguration);
$facet->addOption($this->objectManager->get(Option::class, $facet, $label, $optionsValue, $count, $isOptionsActive, $metricsFromSolrResponse[$optionsValue]));
}
// after all options have been created we apply a manualSortOrder if configured
// the sortBy (lex,..) is done by the solr server and triggered by the query, therefore it does not
// need to be handled in the frontend.
$this->applyManualSortOrder($facet, $facetConfiguration);
$this->applyReverseOrder($facet, $facetConfiguration);
if(!is_null($this->dispatcher)) {
$this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
}
return $facet;
}
/**
* @param string $facetName
* @param ResponseAdapter $response
* @return array
*/
protected function getOptionsFromSolrResponse($facetName, ResponseAdapter $response)
{
$optionsFromSolrResponse = [];
if (!isset($response->facets->{$facetName})) {
return $optionsFromSolrResponse;
}
foreach ($response->facets->{$facetName}->buckets as $bucket) {
$optionValue = $bucket->val;
$optionCount = $bucket->count;
$optionsFromSolrResponse[$optionValue] = $optionCount;
}
return $optionsFromSolrResponse;
}
/**
* @param string $facetName
* @param ResponseAdapter $response
* @return array
*/
protected function getMetricsFromSolrResponse($facetName, ResponseAdapter $response)
{
$metricsFromSolrResponse = [];
if (!isset($response->facets->{$facetName}->buckets)) {
return [];
}
foreach ($response->facets->{$facetName}->buckets as $bucket) {
$bucketVariables = get_object_vars($bucket);
foreach ($bucketVariables as $key => $value) {
if (strpos($key, 'metrics_') === 0) {
$metricsKey = str_replace('metrics_', '', $key);
$metricsFromSolrResponse[$bucket->val][$metricsKey] = $value;
}
}
}
return $metricsFromSolrResponse;
}
}

View File

@@ -0,0 +1,147 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\Options;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\DefaultFacetQueryBuilder;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\FacetQueryBuilderInterface;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\SortingExpression;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
/**
* Class OptionsFacetQueryBuilder
*
* The Options facet query builder builds the facets as json structure
*
* @Todo: When we use json faceting for other facets some logic of this class can be moved to the base class.
*/
class OptionsFacetQueryBuilder extends DefaultFacetQueryBuilder implements FacetQueryBuilderInterface {
/**
* @param string $facetName
* @param TypoScriptConfiguration $configuration
* @return array
*/
public function build($facetName, TypoScriptConfiguration $configuration)
{
$facetParameters = [];
$facetConfiguration = $configuration->getSearchFacetingFacetByName($facetName);
$jsonFacetOptions = [
'type' => 'terms',
'field' => $facetConfiguration['field'],
];
$jsonFacetOptions['limit'] = $this->buildLimitForJson($facetConfiguration, $configuration);
$jsonFacetOptions['mincount'] = $this->buildMincountForJson($facetConfiguration, $configuration);
$sorting = $this->buildSortingForJson($facetConfiguration);
if (!empty($sorting)) {
$jsonFacetOptions['sort'] = $sorting;
}
if (is_array($facetConfiguration['metrics.'])) {
foreach ($facetConfiguration['metrics.'] as $key => $value) {
$jsonFacetOptions['facet']['metrics_' . $key] = $value;
}
}
$excludeTags = $this->buildExcludeTagsForJson($facetConfiguration, $configuration);
if (!empty($excludeTags)) {
$jsonFacetOptions['domain']['excludeTags'] = $excludeTags;
}
$facetParameters['json.facet'][$facetName] = $jsonFacetOptions;
return $facetParameters;
}
/**
* @param array $facetConfiguration
* @param TypoScriptConfiguration $configuration
* @return string
*/
protected function buildExcludeTagsForJson(array $facetConfiguration, TypoScriptConfiguration $configuration)
{
$excludeFields = [];
if ($configuration->getSearchFacetingKeepAllFacetsOnSelection()) {
if (!$configuration->getSearchFacetingCountAllFacetsForSelection()) {
// keepAllOptionsOnSelection globally active
foreach ($configuration->getSearchFacetingFacets() as $facet) {
$excludeFields[] = $facet['field'];
}
} else {
$excludeFields[] = $facetConfiguration['field'];
}
}
$isKeepAllOptionsActiveForSingleFacet = $facetConfiguration['keepAllOptionsOnSelection'] == 1;
if ($isKeepAllOptionsActiveForSingleFacet) {
$excludeFields[] = $facetConfiguration['field'];
}
if (!empty($facetConfiguration['additionalExcludeTags'])) {
$excludeFields[] = $facetConfiguration['additionalExcludeTags'];
}
return implode(',', array_unique($excludeFields));
}
/**
* @param array $facetConfiguration
* @param TypoScriptConfiguration $configuration
* @return int
*/
protected function buildLimitForJson(array $facetConfiguration, TypoScriptConfiguration $configuration)
{
if (isset($facetConfiguration['facetLimit'])) {
return (int)$facetConfiguration['facetLimit'];
} elseif (!is_null($configuration->getSearchFacetingFacetLimit()) && $configuration->getSearchFacetingFacetLimit() >= 0) {
return $configuration->getSearchFacetingFacetLimit();
} else {
return -1;
}
}
/**
* @param array $facetConfiguration
* @param TypoScriptConfiguration $configuration
* @return int
*/
protected function buildMincountForJson(array $facetConfiguration, TypoScriptConfiguration $configuration)
{
if (isset($facetConfiguration['minimumCount'])) {
return (int)$facetConfiguration['minimumCount'];
} elseif (!is_null($configuration->getSearchFacetingMinimumCount()) && (int)$configuration->getSearchFacetingMinimumCount() >= 0) {
return $configuration->getSearchFacetingMinimumCount();
} else {
return 1;
}
}
/**
* @param array $facetConfiguration
* @return string
*/
protected function buildSortingForJson(array $facetConfiguration) {
if (isset($facetConfiguration['sortBy'])) {
$sortingExpression = new SortingExpression();
$sorting = $facetConfiguration['sortBy'];
$direction = $facetConfiguration['sortDirection'];
return $sortingExpression->getForJsonFacet($sorting, $direction);
}
return '';
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\OptionBased\Options;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Facets\AbstractFacetPackage;
/**
* Class OptionsPackage
*/
class OptionsPackage extends AbstractFacetPackage {
/**
* @return string
*/
public function getParserClassName() {
return (string)OptionsFacetParser::class;
}
/**
* @return string
*/
public function getQueryBuilderClassName() {
return (string)OptionsFacetQueryBuilder::class;
}
}