Skip to content

Commit

Permalink
[Core] Adding support for simple regexp queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Oct 2, 2024
1 parent 60ea1d9 commit 38c1ce0
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 0 deletions.
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 src/module-elasticsuite-core/Search/Request/Query/Regexp.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface QueryInterface extends \Magento\Framework\Search\Request\QueryInterfac
const TYPE_MORELIKETHIS = 'moreLikeThisQuery';
const TYPE_MATCHPHRASEPREFIX = 'matchPhrasePrefixQuery';
const TYPE_PREFIX = 'prefixQuery';
const TYPE_REGEXP = 'regexpQuery';

/**
* Set the query name
Expand Down
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();
}
}
3 changes: 3 additions & 0 deletions src/module-elasticsuite-core/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<item name="spanTermQuery" xsi:type="object">spanTermFactory</item>
<item name="spanWithinQuery" xsi:type="object">spanWithinFactory</item>
<item name="prefixQuery" xsi:type="object">prefixQueryFactory</item>
<item name="regexpQuery" xsi:type="object">regexpQueryFactory</item>
</argument>
</arguments>
</type>
Expand Down Expand Up @@ -151,6 +152,7 @@
<virtualType name="spanTermFactory" type="Smile\ElasticsuiteCore\Search\Request\Query\Span\SpanTermFactory" />
<virtualType name="spanWithinFactory" type="Smile\ElasticsuiteCore\Search\Request\Query\Span\SpanWithinFactory" />
<virtualType name="prefixQueryFactory" type="Smile\ElasticsuiteCore\Search\Request\Query\PrefixFactory" />
<virtualType name="regexpQueryFactory" type="Smile\ElasticsuiteCore\Search\Request\Query\RegexpFactory" />

<type name="Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder">
<arguments>
Expand Down Expand Up @@ -180,6 +182,7 @@
<item name="spanTermQuery" xsi:type="object">Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span\SpanTerm\Proxy</item>
<item name="spanWithinQuery" xsi:type="object">Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span\SpanWithin\Proxy</item>
<item name="prefixQuery" xsi:type="object">Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Prefix\Proxy</item>
<item name="regexpQuery" xsi:type="object">Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Regexp\Proxy</item>
</argument>
</arguments>
</type>
Expand Down

0 comments on commit 38c1ce0

Please sign in to comment.