-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/module-elasticsuite-core/Search/Adapter/Elasticsuite/Request/Query/Builder/Regexp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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\Elasticsuite | ||
* @author Richard BAYET <[email protected]> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder; | ||
|
||
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* Build an ES regexp query. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <[email protected]> | ||
*/ | ||
class Regexp implements BuilderInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const DEFAULT_FLAGS = 'NONE'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function buildQuery(QueryInterface $query) | ||
{ | ||
if ($query->getType() !== QueryInterface::TYPE_REGEXP) { | ||
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}"); | ||
} | ||
|
||
$searchQueryParams = [ | ||
'value' => $query->getValue(), | ||
'boost' => $query->getBoost(), | ||
'flags' => self::DEFAULT_FLAGS, | ||
]; | ||
|
||
$searchQuery = ['regexp' => [$query->getField() => $searchQueryParams]]; | ||
|
||
if ($query->getName()) { | ||
$searchQuery['regexp']['_name'] = $query->getName(); | ||
} | ||
|
||
return $searchQuery; | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/module-elasticsuite-core/Search/Request/Query/Regexp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?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\Elasticsuite | ||
* @author Richard BAYET <[email protected]> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Search\Request\Query; | ||
|
||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* ElasticSuite minimal regexp query implementation. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <[email protected]> | ||
*/ | ||
class Regexp implements QueryInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private $boost; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $field; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $value Query value ie the regular expression. | ||
* @param string $field Query field. | ||
* @param string $name Name of the query. | ||
* @param integer $boost Query boost. | ||
*/ | ||
public function __construct($value, $field, $name = null, $boost = QueryInterface::DEFAULT_BOOST_VALUE) | ||
{ | ||
$this->name = $name; | ||
$this->value = $value; | ||
$this->field = $field; | ||
$this->boost = $boost; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setName($name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getBoost() | ||
{ | ||
return $this->boost; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getType() | ||
{ | ||
return QueryInterface::TYPE_REGEXP; | ||
} | ||
|
||
/** | ||
* Search value. | ||
* | ||
* @return string | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* Search field. | ||
* | ||
* @return string | ||
*/ | ||
public function getField() | ||
{ | ||
return $this->field; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...sticsuite-core/Test/Unit/Search/Adapter/Elasticsuite/Request/Query/Builder/RegexpTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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\Elasticsuite | ||
* @author Richard BAYET <[email protected]> | ||
* @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\Request\Query\Regexp as RegexpQuery; | ||
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Regexp as RegexpQueryBuilder; | ||
|
||
/** | ||
* Regexp search query test case. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <[email protected]> | ||
*/ | ||
class RegexpTest extends AbstractSimpleQueryBuilderTest | ||
{ | ||
/** | ||
* Test the builder with mandatory params only. | ||
* | ||
* @return void | ||
*/ | ||
public function testAnonymousRegexpQueryBuilder() | ||
{ | ||
$builder = $this->getQueryBuilder(); | ||
|
||
$regexpQuery = new RegexpQuery('value', 'field'); | ||
$query = $builder->buildQuery($regexpQuery); | ||
|
||
$this->assertArrayHasKey('regexp', $query); | ||
$this->assertArrayHasKey('field', $query['regexp']); | ||
$this->assertArrayHasKey('value', $query['regexp']['field']); | ||
$this->assertEquals('value', $query['regexp']['field']['value']); | ||
|
||
$this->assertArrayHasKey('boost', $query['regexp']['field']); | ||
$this->assertEquals(RegexpQuery::DEFAULT_BOOST_VALUE, $query['regexp']['field']['boost']); | ||
|
||
$this->assertArrayHasKey('flags', $query['regexp']['field']); | ||
$this->assertEquals($builder::DEFAULT_FLAGS, $query['regexp']['field']['flags']); | ||
|
||
$this->assertArrayNotHasKey('_name', $query['regexp']); | ||
} | ||
|
||
/** | ||
* Test the builder with mandatory + name params. | ||
* | ||
* @return void | ||
*/ | ||
public function testNamedRegexpQueryBuilder() | ||
{ | ||
$builder = $this->getQueryBuilder(); | ||
|
||
$regexpQuery = new RegexpQuery('value', 'field', 'queryName'); | ||
$query = $builder->buildQuery($regexpQuery); | ||
|
||
$this->assertArrayHasKey('regexp', $query); | ||
$this->assertArrayHasKey('_name', $query['regexp']); | ||
$this->assertEquals('queryName', $query['regexp']['_name']); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getQueryBuilder() | ||
{ | ||
return new RegexpQueryBuilder(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters