Skip to content

Commit

Permalink
[Task]: Fixing tests and remove Elasticsearch 7 support (pimcore#13797)
Browse files Browse the repository at this point in the history
* fixed pimcore#13411 - [Task]: Remove Elasticsearch 7 support

* further cleanup

* further cleanup

* some type fixes

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* Update ElasticSearch.php

* fix uses

* type fixes
  • Loading branch information
fashxp authored Dec 14, 2022
1 parent 1894105 commit 90e2d59
Show file tree
Hide file tree
Showing 24 changed files with 81 additions and 755 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ services:
Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\OptimizedMysql:
parent: Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\AbstractWorker

Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\DefaultElasticSearch7:
parent: Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\AbstractWorker

Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\DefaultElasticSearch8:
parent: Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\AbstractWorker

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\OptimizedMysql as OptimizedMysqlConfig;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\DefaultFindologic;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\DefaultMysql;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\DefaultElasticSearch7;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\DefaultElasticSearch8;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\OptimizedMysql;

/**
Expand All @@ -38,7 +38,7 @@ class DefaultWorkerConfigMapper
OptimizedMysqlConfig::class => OptimizedMysql::class,
DefaultMysqlConfig::class => DefaultMysql::class,
DefaultMysqlSubTenantConfig::class => DefaultMysql::class,
ElasticSearch::class => DefaultElasticSearch7::class,
ElasticSearch::class => DefaultElasticSearch8::class,
DefaultFindologicConfig::class => DefaultFindologic::class,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function addCondition(AbstractFilterDefinitionType $filterDefinition, Pro
$value = $preSelect;
}

$value = trim($value);
$value = trim((string)$value);
$currentFilter[$field] = $value;

if (!empty($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
/**
* Default configuration for elastic search as product index implementation.
*
* @method DefaultElasticSearchWorker getTenantWorker()
*/
class ElasticSearch extends AbstractConfig implements MockupConfigInterface, ElasticSearchConfigInterface
{
Expand Down Expand Up @@ -127,20 +126,13 @@ protected function processOptions(array $options)
// TODO validate client config and other settings/params?
$this->clientConfig = $options['client_config'];
$this->indexSettings = $options['index_settings'];
$this->elasticSearchClientParams = $options['es_client_params'];

//add default type for elasticsearch
if (empty($this->elasticSearchClientParams['indexType'])) {
$this->elasticSearchClientParams['indexType'] = '_doc';
}
}

protected function configureOptionsResolver(string $resolverName, OptionsResolver $resolver)
{
$arrayFields = [
'client_config',
'index_settings',
'es_client_params',
'mapping',
];

Expand All @@ -161,8 +153,6 @@ protected function configureOptionsResolver(string $resolverName, OptionsResolve
$resolver->setDefined('es_client_name');
$resolver->setAllowedTypes('es_client_name', 'string');

//set options to deprecated
$resolver->setDeprecated('es_client_params');
}

protected function extractPossibleFirstSubFieldnameParts(string $fieldName): array
Expand Down Expand Up @@ -248,11 +238,6 @@ public function getIndexSettings(): array
return $this->indexSettings;
}

public function getElasticSearchClientParams(): array
{
return $this->elasticSearchClientParams;
}

/**
* checks, if product should be in index for current tenant
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
*/
interface ElasticSearchConfigInterface extends ConfigInterface
{
/**
* returns elastic search client parameters defined in the tenant config
*
* @return array
*/
public function getElasticSearchClientParams(): array;

/**
* returns condition for current subtenant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ public function resetCondition(string $fieldname): void
* Fieldname is optional but highly recommended - needed for resetting condition based on fieldname
* and exclude functionality in group by results
*
* @param string $condition
* @param string|array $condition
* @param string $fieldname
*/
public function addQueryCondition(string $condition, string $fieldname = '')
public function addQueryCondition(string|array $condition, string $fieldname = '')
{
$this->products = null;
$this->queryConditions[$fieldname][] = $condition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public function resetConditions()
* Fieldname is optional but highly recommended - needed for resetting condition based on fieldname
* and exclude functionality in group by results
*
* @param string $condition
* @param string|array $condition
* @param string $fieldname
*/
public function addQueryCondition(string $condition, string $fieldname = '')
public function addQueryCondition(string|array $condition, string $fieldname = '')
{
$this->products = null;
$this->queryConditions[$fieldname][] = $condition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ElasticSearch;

use Elastic\Elasticsearch\Client;
use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\InvalidConfigException;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\ElasticSearch;
Expand Down Expand Up @@ -63,7 +64,7 @@ abstract class AbstractElasticSearch implements ProductListInterface

protected ?string $order = null;

protected string|array $orderKey;
protected string|array $orderKey = '';

protected bool $orderByPrice = false;

Expand Down Expand Up @@ -219,10 +220,10 @@ public function resetConditions()
* Fieldname is optional but highly recommended - needed for resetting condition based on fieldname
* and exclude functionality in group by results
*
* @param string $condition
* @param string|array $condition
* @param string $fieldname - must be set for elastic search
*/
public function addQueryCondition(string $condition, string $fieldname = '')
public function addQueryCondition(string|array $condition, string $fieldname = '')
{
$this->queryConditions[$fieldname][] = $condition;
$this->preparedGroupByValuesLoaded = false;
Expand Down Expand Up @@ -417,7 +418,7 @@ public function load(): array
$this->products = $this->productPositionMap = [];
$i = 0;
foreach ($objectRaws as $raw) {
$product = $this->loadElementById($raw);
$product = $this->loadElementById((int) $raw);
if ($product) {
$this->products[] = $product;
$this->productPositionMap[$product->getId()] = $i;
Expand Down Expand Up @@ -452,7 +453,6 @@ public function getQuery(): array

$params = [];
$params['index'] = $this->getIndexName();
$params['type'] = $this->getTenantConfig()->getElasticSearchClientParams()['indexType'];
$params['track_total_hits'] = true;
$params['rest_total_hits_as_int'] = true;

Expand Down Expand Up @@ -1161,39 +1161,36 @@ public function getTenantConfig(): ElasticSearchConfigInterface

/**
* send a request to elasticsearch
*
* @param array $params
*
* @return array
*/
protected function sendRequest(array $params): array
{
$tenantWorker = $this->tenantConfig->getTenantWorker();
if (!($tenantWorker instanceof \Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\AbstractElasticSearch)) {
throw new InvalidConfigException('Invalid tenant worker configured. Should be instance of AbstractElasticSearch.');

$worker = $this->tenantConfig->getTenantWorker();
if(!$worker instanceof \Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\AbstractElasticSearch) {
throw new InvalidConfigException('Invalid worker configured, AbstractElasticSearch compatible worker expected.');
}

/**
* @var \Elasticsearch\Client $esClient
* @var Client $esClient
*/
$esClient = $tenantWorker->getElasticSearchClient();
$esClient = $worker->getElasticSearchClient();
$result = [];

if ($esClient instanceof \Elasticsearch\Client) {
if ($esClient instanceof Client) {
if ($this->doScrollRequest) {
$params = array_merge(['scroll' => $this->scrollRequestKeepAlive], $params);
//kind of dirty hack :/
$params['body']['size'] = $this->getLimit();
}

$result = $esClient->search($params);
$result = $esClient->search($params)->asArray();

if ($this->doScrollRequest) {
$additionalHits = [];
$scrollId = $result['_scroll_id'];

while (true) {
$additionalResult = $esClient->scroll(['scroll_id' => $scrollId, 'scroll' => $this->scrollRequestKeepAlive]);
$additionalResult = $esClient->scroll(['scroll_id' => $scrollId, 'scroll' => $this->scrollRequestKeepAlive])->asArray();

if (count($additionalResult['hits']['hits'])) {
$additionalHits = array_merge($additionalHits, $additionalResult['hits']['hits']);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,5 @@

class DefaultElasticSearch8 extends AbstractElasticSearch
{
/**
* send a request to elasticsearch
*/
protected function sendRequest(array $params): array
{
/**
* @var Client $esClient
*/
$esClient = $this->tenantConfig->getTenantWorker()->getElasticSearchClient();
$result = [];

if ($esClient instanceof Client) {
if ($this->doScrollRequest) {
$params = array_merge(['scroll' => $this->scrollRequestKeepAlive], $params);
//kind of dirty hack :/
$params['body']['size'] = $this->getLimit();
}

$result = $esClient->search($params)->asArray();

if ($this->doScrollRequest) {
$additionalHits = [];
$scrollId = $result['_scroll_id'];

while (true) {
$additionalResult = $esClient->scroll(['scroll_id' => $scrollId, 'scroll' => $this->scrollRequestKeepAlive])->asArray();

if (count($additionalResult['hits']['hits'])) {
$additionalHits = array_merge($additionalHits, $additionalResult['hits']['hits']);
$scrollId = $additionalResult['_scroll_id'];
} else {
break;
}
}
$result['hits']['hits'] = array_merge($result['hits']['hits'], $additionalHits);
}
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public function addCondition(array|string $condition, string $fieldname = '');
* Fieldname is optional but highly recommended - needed for resetting condition based on fieldname
* and exclude functionality in group by results
*
* @param string $condition
* @param string|array $condition
* @param string $fieldname
*/
public function addQueryCondition(string $condition, string $fieldname = '');
public function addQueryCondition(string|array $condition, string $fieldname = '');

/**
* Reset filter condition for fieldname
Expand Down
Loading

0 comments on commit 90e2d59

Please sign in to comment.