Skip to content

Commit

Permalink
Merge branch '2.11.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Nov 4, 2024
2 parents ea90362 + e870f32 commit af9de74
Show file tree
Hide file tree
Showing 17 changed files with 1,356 additions and 7 deletions.
45 changes: 41 additions & 4 deletions src/module-elasticsuite-core/Test/Unit/Cluster/ClusterInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,53 @@ class ClusterInfoTest extends \PHPUnit\Framework\TestCase
* @return void
*/
public function testGetServerVersion()
{
$clientMock = $this->getMockClient('1.0.0');

$clusterInfo = new ClusterInfo($clientMock);

$this->assertEquals('1.0.0', $clusterInfo->getServerVersion());
}

/**
* Test retrieving the server distributoin.
*
* @return void
*/
public function testGetServerDistribution()
{
$clientMock = $this->getMockClient('1.0.0', 'RandomDistribution');

$clusterInfo = new ClusterInfo($clientMock);
$this->assertEquals('RandomDistribution', $clusterInfo->getServerDistribution());

$clientMock = $this->getMockClient('1.2.3');

$clusterInfo = new ClusterInfo($clientMock);
$this->assertEquals(ClusterInfo::DISTRO_ES, $clusterInfo->getServerDistribution());
}

/**
* Return a mock of the client.
*
* @param string $versionNumber Version number.
* @param string|null $distribution Distribution (Elasticsearch, OpenSearch, ...)
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
private function getMockClient($versionNumber, $distribution = null)
{
$clientMock = $this->getMockBuilder(\Smile\ElasticsuiteCore\Api\Client\ClientInterface::class)
->disableOriginalConstructor()
->getMock();

$clientMock->method('info')
->will($this->returnValue(['version' => ['number' => '1.0.0']]));
$info = ['version' => ['number' => $versionNumber]];
if ($distribution) {
$info['version']['distribution'] = $distribution;
}

$clusterInfo = new ClusterInfo($clientMock);
$clientMock->method('info')->will($this->returnValue($info));

$this->assertEquals('1.0.0', $clusterInfo->getServerVersion());
return $clientMock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ protected function setUp(): void
*/
public function testAvailableLanguages()
{
$this->assertCount(5, $this->parsedData);
$this->assertCount(7, $this->parsedData);
$this->assertArrayHasKey('default', $this->parsedData);
$this->assertArrayHasKey('override_language', $this->parsedData);

$this->assertArrayHasKey('char_filter_generated_language', $this->parsedData);
$this->assertArrayHasKey('filter_generated_language', $this->parsedData);
$this->assertArrayHasKey('analyzer_generated_language', $this->parsedData);

$this->assertArrayHasKey('language_without_stemmer', $this->parsedData);
$this->assertArrayHasKey('language_with_stemmer', $this->parsedData);
}

/**
Expand Down Expand Up @@ -151,4 +155,28 @@ public function testAnalyzers()
$this->assertContains('filter', $defaultAnalyzers['analyzer']['filter']);
$this->assertContains('filter_with_params', $defaultAnalyzers['analyzer']['filter']);
}

/**
* Test analyzers with or without a stemmer depending on their availability in a given language.
*
* @return void
*/
public function testStemmerPresence()
{
$this->assertArrayHasKey('language_without_stemmer', $this->parsedData);
$this->assertArrayHasKey('analyzer', $this->parsedData['language_without_stemmer']);
$this->assertArrayHasKey('stemmed_analyzer', $this->parsedData['language_without_stemmer']['analyzer']);

$stemmedAnalyzer = $this->parsedData['language_without_stemmer']['analyzer']['stemmed_analyzer'];
$this->assertArrayHasKey('filter', $stemmedAnalyzer);
$this->assertNotContains('stemmer', $stemmedAnalyzer['filter']);

$this->assertArrayHasKey('language_with_stemmer', $this->parsedData);
$this->assertArrayHasKey('analyzer', $this->parsedData['language_with_stemmer']);
$this->assertArrayHasKey('stemmed_analyzer', $this->parsedData['language_with_stemmer']['analyzer']);

$stemmedAnalyzer = $this->parsedData['language_with_stemmer']['analyzer']['stemmed_analyzer'];
$this->assertArrayHasKey('filter', $stemmedAnalyzer);
$this->assertContains('stemmer', $stemmedAnalyzer['filter']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
</filter>
<filter name="dummy" type="dummy" language="filter_generated_language"/>
<filter name="filter" type="filter_type_language_override" language="override_language" />
<filter name="stemmer" type="stemmer" language="language_with_stemmer">
<language>stemmed_language</language>
</filter>
</filters>

<analyzers>
Expand All @@ -61,5 +64,38 @@
<filter ref="filter_with_params" />
</filters>
</analyzer>
<analyzer name="stemmed_analyzer" tokenizer="dummy" language="language_without_stemmer">
<char_filters>
<char_filter ref="char_filter" />
</char_filters>
<filters>
<filter ref="filter" />
<filter ref="filter_with_params" />
<filter ref="stemmer" />
</filters>
</analyzer>
<analyzer name="stemmed_analyzer" tokenizer="dummy" language="language_with_stemmer">
<char_filters>
<char_filter ref="char_filter" />
</char_filters>
<filters>
<filter ref="filter" />
<filter ref="filter_with_params" />
<filter ref="stemmer" />
</filters>
</analyzer>
</analyzers>

<stemmers>
<group language="language_with_stemmer" title="Language with Stemmer">
<stemmer identifier="langlight" recommended="true">
<label>Language Light</label>
</stemmer>
<stemmer identifier="langporter">
<label>Language Porter</label>
<extra_config_json><![CDATA[{"param1":0,"param2":2}]]></extra_config_json>
<extra_config_plain><![CDATA[0,2]]></extra_config_plain>
</stemmer>
</group>
</stemmers>
</analysis>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public function testAnonymousFilteredQueryBuilder()
{
$builder = $this->getQueryBuilder();

$filteredQuery = new FilteredQuery($this->getSubQueryMock('baseQuery'), $this->getSubQueryMock('filterQuery'));
$filteredQuery = new FilteredQuery(
$this->getSubQueryMock('baseQuery'),
$this->getSubQueryMock('filterQuery')
);
$query = $builder->buildQuery($filteredQuery);

$this->assertArrayHasKey('bool', $query);
Expand All @@ -59,13 +62,56 @@ public function testNamedFilteredQueryBuilder()
{
$builder = $this->getQueryBuilder();

$filteredQuery = new FilteredQuery($this->getSubQueryMock('baseQuery'), $this->getSubQueryMock('filterQuery'), 'queryName');
$filteredQuery = new FilteredQuery(
$this->getSubQueryMock('baseQuery'),
$this->getSubQueryMock('filterQuery'),
'queryName'
);
$query = $builder->buildQuery($filteredQuery);

$this->assertArrayHasKey('_name', $query['bool']);
$this->assertEquals('queryName', $query['bool']['_name']);
}

/**
* Test the builder with a filter query but an empty base query.
*
* @return void
*/
public function testQueryLessFilteredQueryBuilder(): void
{
$builder = $this->getQueryBuilder();

$filteredQuery = new FilteredQuery(
null,
$this->getSubQueryMock('filterQuery')
);
$query = $builder->buildQuery($filteredQuery);

$this->assertArrayHasKey('constant_score', $query);

$this->assertArrayHasKey('filter', $query['constant_score']);
$this->assertEquals('filterQuery', $query['constant_score']['filter']);
}

/**
* Test the builder with an empty filter query on an empty base query.
*
* @return void
*/
public function testEmptyFilteredQueryBuilder(): void
{
$builder = $this->getQueryBuilder();

$filteredQuery = new FilteredQuery();
$query = $builder->buildQuery($filteredQuery);

$this->assertArrayHasKey('constant_score', $query);

$this->assertArrayHasKey('filter', $query['constant_score']);
$this->assertEquals(['match_all' => new \stdClass()], $query['constant_score']['filter']);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <richard.bayet@smile.fr>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Test\Unit\Search\Adapter\Elasticsuite\Request\Query\Builder;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\MatchPhrasePrefix as MatchPhrasePrefixQueryBuilder;
use Smile\ElasticsuiteCore\Search\Request\Query\MatchPhrasePrefix as MatchPhrasePrefixQuery;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* MatchPhrasePrefix search request query building test case.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <richard.bayet@smile.fr>
*/
class MatchPhrasePrefixTest extends AbstractSimpleQueryBuilderTest
{
/**
* Test the builder with mandatory params only.
*/
public function testAnonymousMatchPhrasePrefixQueryAssembler(): void
{
$assembler = $this->getQueryBuilder();

$matchPhrasePrefixQuery = new MatchPhrasePrefixQuery('search text', 'searchField');
$query = $assembler->buildQuery($matchPhrasePrefixQuery);

$this->checkDefaultStructure($query);

$this->assertEquals('search text', $query['match_phrase_prefix']['searchField']['query']);
$this->assertEquals(QueryInterface::DEFAULT_BOOST_VALUE, $query['match_phrase_prefix']['searchField']['boost']);
$this->assertEquals(10, $query['match_phrase_prefix']['searchField']['max_expansions']);

$this->assertArrayNotHasKey('_name', $query['match_phrase_prefix']);
}

/**
* Test the assembler with mandatory + name params.
*/
public function testNamedMatchPhrasePrefixQueryAssembler(): void
{
$assembler = $this->getQueryBuilder();

$matchPhrasePrefixQuery = new MatchPhrasePrefixQuery(
'search text',
'searchField',
10,
'queryName',
QueryInterface::DEFAULT_BOOST_VALUE
);
$query = $assembler->buildQuery($matchPhrasePrefixQuery);

$this->checkDefaultStructure($query);
$this->assertArrayHasKey('_name', $query['match_phrase_prefix']);

$this->assertEquals('search text', $query['match_phrase_prefix']['searchField']['query']);
$this->assertEquals(MatchPhrasePrefixQuery::DEFAULT_BOOST_VALUE, $query['match_phrase_prefix']['searchField']['boost']);
$this->assertEquals(10, $query['match_phrase_prefix']['searchField']['max_expansions']);
$this->assertEquals('queryName', $query['match_phrase_prefix']['_name']);
}

/**
* {@inheritDoc}
*/
protected function getQueryBuilder()
{
return new MatchPhrasePrefixQueryBuilder();
}

/**
* Check the minimum structure of the built query.
*
* @param array $query Built query
*/
private function checkDefaultStructure(array $query): void
{
$this->assertArrayHasKey('match_phrase_prefix', $query);
$this->assertArrayHasKey('searchField', $query['match_phrase_prefix']);
$this->assertArrayHasKey('query', $query['match_phrase_prefix']['searchField']);
$this->assertArrayHasKey('boost', $query['match_phrase_prefix']['searchField']);
$this->assertArrayHasKey('max_expansions', $query['match_phrase_prefix']['searchField']);
}
}
Loading

0 comments on commit af9de74

Please sign in to comment.