Skip to content

Commit

Permalink
Implement span queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Mar 28, 2023
1 parent 600627d commit 52f773a
Show file tree
Hide file tree
Showing 20 changed files with 1,455 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\AbstractComplexBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_containing query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanContaining extends AbstractComplexBuilder implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_CONTAINING) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
'span_containing' => [
'little' => $this->parentBuilder->buildQuery($query->getLittle()),
'big' => $this->parentBuilder->buildQuery($query->getBig()),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\AbstractComplexBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_field_masking query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanFieldMasking extends AbstractComplexBuilder implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_FIELD_MASKING) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
'span_field_masking' => [
'query' => $this->parentBuilder->buildQuery($query->getQuery()),
'field' => $query->getField(),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\AbstractComplexBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_first query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanFirst extends AbstractComplexBuilder implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_FIRST) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
'span_first' => [
'match' => $this->parentBuilder->buildQuery($query->getMatch()),
'end' => $query->getEnd(),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\AbstractComplexBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_multi query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanMultiTerm extends AbstractComplexBuilder implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_MULTI_TERM) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
'span_multi' => [
'match' => $this->parentBuilder->buildQuery($query->getMatch()),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_near query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanNear implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_NEAR) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

$clauses = array_map([$this->parentBuilder, 'buildQuery'], $query->getClauses());

return [
'span_near' => [
'clauses' => array_filter($clauses),
'slop' => $query->getSlop(),
'in_order' => $query->getInOrder(),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\AbstractComplexBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_not query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanNot extends AbstractComplexBuilder implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_NOT) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
'span_not' => [
'include' => $this->parentBuilder->buildQuery($query->getInclude()),
'exclude' => $this->parentBuilder->buildQuery($query->getExclude()),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_or query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanOr implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_OR) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

$clauses = array_map([$this->parentBuilder, 'buildQuery'], $query->getClauses());

return [
'span_or' => [
'clauses' => array_filter($clauses),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2023 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder\Span;

use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\SpanQueryInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Build an ES span_term query.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class SpanTerm implements BuilderInterface
{
/**
* {@inheritDoc}
*/
public function buildQuery(QueryInterface $query)
{
if ($query->getType() !== SpanQueryInterface::TYPE_SPAN_TERM) {
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

$searchQueryParams = [
'value' => $query->getValue(),
'boost' => $query->getBoost(),
];

$searchQuery = ['span_term' => [$query->getField() => $searchQueryParams]];

if ($query->getName()) {
$searchQuery['span_term']['_name'] = $query->getName();
}

return $searchQuery;
}
}
Loading

0 comments on commit 52f773a

Please sign in to comment.