Skip to content

Commit 507477b

Browse files
authored
Merge pull request #3582 from rbayet/feat-support-for-opensearch-2.x-in-2.10.x
[Core] Adding support for OpenSearch 2.x in 2.10.x
2 parents 4b337d1 + 72e1b78 commit 507477b

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

src/module-elasticsuite-core/Index/AsyncIndexOperation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ class AsyncIndexOperation extends IndexOperation implements AsyncIndexOperationI
4747
* @param \Smile\ElasticsuiteCore\Api\Client\ClientInterface $client ES client.
4848
* @param \Smile\ElasticsuiteCore\Api\Client\ClientConfigurationInterface $clientConfiguration ES client configuration.
4949
* @param \Smile\ElasticsuiteCore\Api\Index\IndexSettingsInterface $indexSettings ES settings.
50+
* @param \Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface $clusterInfo ES cluster information.
5051
* @param \Psr\Log\LoggerInterface $logger Logger access.
5152
*/
5253
public function __construct(
5354
\Magento\Framework\ObjectManagerInterface $objectManager,
5455
\Smile\ElasticsuiteCore\Api\Client\ClientInterface $client,
5556
\Smile\ElasticsuiteCore\Api\Client\ClientConfigurationInterface $clientConfiguration,
5657
\Smile\ElasticsuiteCore\Api\Index\IndexSettingsInterface $indexSettings,
58+
\Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface $clusterInfo,
5759
\Psr\Log\LoggerInterface $logger
5860
) {
5961
$this->client = $client;
6062
$this->parallelHandles = $clientConfiguration->getMaxParallelHandles();
61-
parent::__construct($objectManager, $client, $indexSettings, $logger);
63+
parent::__construct($objectManager, $client, $indexSettings, $clusterInfo, $logger);
6264
}
6365

6466
/**

src/module-elasticsuite-core/Index/Bulk/BulkRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BulkRequest implements BulkRequestInterface
3232
*
3333
* @var array
3434
*/
35-
private $bulkData = [];
35+
protected $bulkData = [];
3636

3737
/**
3838
* {@inheritdoc}
@@ -55,7 +55,7 @@ public function getOperations()
5555
*/
5656
public function addDocument(IndexInterface $index, $docId, array $data)
5757
{
58-
$this->bulkData[] = ['index' => ['_index' => $index->getName(), '_type' => '_doc', '_id' => $docId]];
58+
$this->bulkData[] = ['index' => ['_index' => $index->getName(), '_id' => $docId]];
5959
$this->bulkData[] = $data;
6060

6161
return $this;
@@ -78,7 +78,7 @@ public function addDocuments(IndexInterface $index, array $data)
7878
*/
7979
public function deleteDocument(IndexInterface $index, $docId)
8080
{
81-
$this->bulkData[] = ['delete' => ['_index' => $index->getName(), '_type' => '_doc', '_id' => $docId]];
81+
$this->bulkData[] = ['delete' => ['_index' => $index->getName(), '_id' => $docId]];
8282

8383
return $this;
8484
}
@@ -100,7 +100,7 @@ public function deleteDocuments(IndexInterface $index, array $docIds)
100100
*/
101101
public function updateDocument(IndexInterface $index, $docId, array $data)
102102
{
103-
$this->bulkData[] = ['update' => ['_index' => $index->getName(), '_type' => '_doc', '_id' => $docId]];
103+
$this->bulkData[] = ['update' => ['_index' => $index->getName(), '_id' => $docId]];
104104
$this->bulkData[] = ['doc' => $data];
105105

106106
return $this;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* DISCLAIMER
4+
*
5+
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
6+
*
7+
* @category Smile
8+
* @package Smile\ElasticsuiteCore
9+
* @author Richard BAYET <[email protected]>
10+
* @copyright 2025 Smile
11+
* @license Open Software License ("OSL") v. 3.0
12+
*/
13+
14+
namespace Smile\ElasticsuiteCore\Index\Bulk\Deprecation;
15+
16+
use Smile\ElasticsuiteCore\Api\Index\IndexInterface;
17+
use Smile\ElasticsuiteCore\Index\Bulk\BulkRequest as BaseBulkRequest;
18+
19+
/**
20+
* ES bulk (Smile\ElasticsuiteCore\Api\Index\BulkInterface) implementation for ES < 7.
21+
*
22+
* @category Smile
23+
* @package Smile\ElasticsuiteCore
24+
* @author Richard BAYET <[email protected]>
25+
*/
26+
class BulkRequest extends BaseBulkRequest
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function addDocument(IndexInterface $index, $docId, array $data)
32+
{
33+
$this->bulkData[] = ['index' => ['_index' => $index->getName(), '_type' => '_doc', '_id' => $docId]];
34+
$this->bulkData[] = $data;
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function deleteDocument(IndexInterface $index, $docId)
43+
{
44+
$this->bulkData[] = ['delete' => ['_index' => $index->getName(), '_type' => '_doc', '_id' => $docId]];
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function updateDocument(IndexInterface $index, $docId, array $data)
53+
{
54+
$this->bulkData[] = ['update' => ['_index' => $index->getName(), '_type' => '_doc', '_id' => $docId]];
55+
$this->bulkData[] = ['doc' => $data];
56+
57+
return $this;
58+
}
59+
}

src/module-elasticsuite-core/Index/IndexOperation.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Smile\ElasticsuiteCore\Api\Index\Bulk\BulkResponseInterface;
1818
use Smile\ElasticsuiteCore\Api\Index\IndexOperationInterface;
19+
use Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface;
1920

2021
/**
2122
* Default implementation of operation on indices (\Smile\ElasticsuiteCore\Api\Index\IndexOperationInterface).
@@ -51,6 +52,16 @@ class IndexOperation implements IndexOperationInterface
5152
*/
5253
private $client;
5354

55+
/**
56+
* @var \Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface
57+
*/
58+
private $clusterInfo;
59+
60+
/**
61+
* @var boolean
62+
*/
63+
private $useDeprecatedBulk;
64+
5465
/**
5566
* @var \Psr\Log\LoggerInterface
5667
*/
@@ -62,18 +73,21 @@ class IndexOperation implements IndexOperationInterface
6273
* @param \Magento\Framework\ObjectManagerInterface $objectManager Object manager.
6374
* @param \Smile\ElasticsuiteCore\Api\Client\ClientInterface $client ES client.
6475
* @param \Smile\ElasticsuiteCore\Api\Index\IndexSettingsInterface $indexSettings ES settings.
76+
* @param \Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface $clusterInfo ES cluster information.
6577
* @param \Psr\Log\LoggerInterface $logger Logger access.
6678
*/
6779
public function __construct(
6880
\Magento\Framework\ObjectManagerInterface $objectManager,
6981
\Smile\ElasticsuiteCore\Api\Client\ClientInterface $client,
7082
\Smile\ElasticsuiteCore\Api\Index\IndexSettingsInterface $indexSettings,
83+
\Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface $clusterInfo,
7184
\Psr\Log\LoggerInterface $logger
7285
) {
7386
$this->objectManager = $objectManager;
7487
$this->client = $client;
7588
$this->indexSettings = $indexSettings;
7689
$this->indicesConfiguration = $indexSettings->getIndicesConfig();
90+
$this->clusterInfo = $clusterInfo;
7791
$this->logger = $logger;
7892
}
7993

@@ -202,6 +216,10 @@ public function installIndex(\Smile\ElasticsuiteCore\Api\Index\IndexInterface $i
202216
*/
203217
public function createBulk()
204218
{
219+
if ($this->useDeprecatedBulk()) {
220+
return $this->objectManager->create(\Smile\ElasticsuiteCore\Index\Bulk\Deprecation\BulkRequest::class);
221+
}
222+
205223
return $this->objectManager->create('Smile\ElasticsuiteCore\Api\Index\Bulk\BulkRequestInterface');
206224
}
207225

@@ -366,4 +384,24 @@ private function initIndex($indexIdentifier, $store, $existingIndex)
366384

367385
return $this->indicesByIdentifier[$indexAlias];
368386
}
387+
388+
/**
389+
* Returns true if the deprecated version of the bulk request object should be used
390+
* for compatibility with Elasticsearch < 7.0
391+
*
392+
* @return bool
393+
*/
394+
private function useDeprecatedBulk()
395+
{
396+
if ($this->useDeprecatedBulk === null) {
397+
$this->useDeprecatedBulk = false;
398+
if ($this->clusterInfo->getServerDistribution() === ClusterInfoInterface::DISTRO_ES) {
399+
if (version_compare($this->clusterInfo->getServerVersion(), '7.0', '<')) {
400+
$this->useDeprecatedBulk = true;
401+
}
402+
}
403+
}
404+
405+
return $this->useDeprecatedBulk;
406+
}
369407
}

src/module-elasticsuite-core/Test/Unit/Index/IndexOperationTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,16 @@ protected function setUp(): void
5252

5353
$objectManager = $this->getObjectManagerMock();
5454
$indexSettings = $this->getIndexSettingsMock();
55+
$clusterInfo = $this->getClusterInfoMock();
5556
$logger = $this->getLoggerMock();
5657

57-
$this->indexOperation = new IndexOperation($objectManager, $this->clientMock, $indexSettings, $logger);
58+
$this->indexOperation = new IndexOperation(
59+
$objectManager,
60+
$this->clientMock,
61+
$indexSettings,
62+
$clusterInfo,
63+
$logger
64+
);
5865
}
5966

6067
/**
@@ -265,6 +272,16 @@ private function getIndexSettingsMock()
265272
return $indexSettingsMock;
266273
}
267274

275+
/**
276+
* Cluster information mocking.
277+
*
278+
* @return \PHPUnit\Framework\MockObject\MockObject
279+
*/
280+
private function getClusterInfoMock()
281+
{
282+
return $this->getMockBuilder(\Smile\ElasticsuiteCore\Api\Cluster\ClusterInfoInterface::class)->getMock();
283+
}
284+
268285
/**
269286
* Logger mocking.
270287
*

0 commit comments

Comments
 (0)