Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add span queries #1319

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file based on the

### Added

- Added `Query\SpanContaining`, `Query\SpanWithin` and `Query\SpanNot` [#1319](https://github.com/ruflin/Elastica/pull/1319)

### Improvements

### Deprecated
Expand Down
Empty file removed lib/Elastica/Query/Builder.php
Empty file.
49 changes: 49 additions & 0 deletions lib/Elastica/Query/SpanContaining.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Elastica\Query;

/**
* SpanContaining query.
*
* @author Alessandro Chitolina <[email protected]>
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html
*/
class SpanContaining extends AbstractSpanQuery
{
/**
* Constructs a SpanContaining query object.
*
* @param AbstractSpanQuery $little OPTIONAL
* @param AbstractSpanQuery $big OPTIONAL
*/
public function __construct(AbstractSpanQuery $little = null, AbstractSpanQuery $big = null)
{
if (null !== $little) {
$this->setLittle($little);
}

if (null !== $big) {
$this->setBig($big);
}
}

/**
* @param AbstractSpanQuery $little
*
* @return $this
*/
public function setLittle(AbstractSpanQuery $little)
{
return $this->setParam('little', $little);
}

/**
* @param AbstractSpanQuery $big
*
* @return $this
*/
public function setBig(AbstractSpanQuery $big)
{
return $this->setParam('big', $big);
}
}
8 changes: 6 additions & 2 deletions lib/Elastica/Query/SpanNear.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ public function __construct($clauses = [], $slop = 1, $inOrder = false)

/**
* @param int $slop
*
* @return $this
*/
public function setSlop($slop)
{
$this->setParam('slop', $slop);
return $this->setParam('slop', $slop);
}

/**
* @param bool $inOrder
*
* @return $this
*/
public function setInOrder($inOrder)
{
$this->setParam('in_order', $inOrder);
return $this->setParam('in_order', $inOrder);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions lib/Elastica/Query/SpanNot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Elastica\Query;

/**
* SpanNot query.
*
* @author Alessandro Chitolina <[email protected]>
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html
*/
class SpanNot extends AbstractSpanQuery
{
/**
* Constructs a SpanWithin query object.
*
* @param AbstractSpanQuery $include OPTIONAL
* @param AbstractSpanQuery $exclude OPTIONAL
*/
public function __construct(AbstractSpanQuery $include = null, AbstractSpanQuery $exclude = null)
{
if (null !== $include) {
$this->setInclude($include);
}

if (null !== $exclude) {
$this->setExclude($exclude);
}
}

/**
* @param AbstractSpanQuery $include
*
* @return $this
*/
public function setInclude(AbstractSpanQuery $include)
{
return $this->setParam('include', $include);
}

/**
* @param AbstractSpanQuery $exclude
*
* @return $this
*/
public function setExclude(AbstractSpanQuery $exclude)
{
return $this->setParam('exclude', $exclude);
}
}
49 changes: 49 additions & 0 deletions lib/Elastica/Query/SpanWithin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Elastica\Query;

/**
* SpanWithin query.
*
* @author Alessandro Chitolina <[email protected]>
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html
*/
class SpanWithin extends AbstractSpanQuery
{
/**
* Constructs a SpanWithin query object.
*
* @param AbstractSpanQuery $little OPTIONAL
* @param AbstractSpanQuery $big OPTIONAL
*/
public function __construct(AbstractSpanQuery $little = null, AbstractSpanQuery $big = null)
{
if (null !== $little) {
$this->setLittle($little);
}

if (null !== $big) {
$this->setBig($big);
}
}

/**
* @param AbstractSpanQuery $little
*
* @return $this
*/
public function setLittle(AbstractSpanQuery $little)
{
return $this->setParam('little', $little);
}

/**
* @param AbstractSpanQuery $big
*
* @return $this
*/
public function setBig(AbstractSpanQuery $big)
{
return $this->setParam('big', $big);
}
}
43 changes: 41 additions & 2 deletions lib/Elastica/QueryBuilder/DSL/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Elastica\QueryBuilder\DSL;

use Elastica\Exception\NotImplementedException;
use Elastica\Query\AbstractSpanQuery;
use Elastica\Query\BoolQuery;
use Elastica\Query\Boosting;
use Elastica\Query\Common;
Expand All @@ -26,11 +27,14 @@
use Elastica\Query\Range;
use Elastica\Query\Regexp;
use Elastica\Query\SimpleQueryString;
use Elastica\Query\SpanContaining;
use Elastica\Query\SpanFirst;
use Elastica\Query\SpanMulti;
use Elastica\Query\SpanNear;
use Elastica\Query\SpanNot;
use Elastica\Query\SpanOr;
use Elastica\Query\SpanTerm;
use Elastica\Query\SpanWithin;
use Elastica\Query\Term;
use Elastica\Query\Terms;
use Elastica\Query\Type;
Expand Down Expand Up @@ -401,11 +405,16 @@ public function span_near($clauses = [], $slop = 1, $inOrder = false)
/**
* span not query.
*
* @param AbstractSpanQuery|null $include
* @param AbstractSpanQuery|null $exclude
*
* @return SpanNot
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html
*/
public function span_not()
public function span_not(AbstractSpanQuery $include = null, AbstractSpanQuery $exclude = null)
{
throw new NotImplementedException();
return new SpanNot($include, $exclude);
}

/**
Expand Down Expand Up @@ -436,6 +445,36 @@ public function span_term(array $term = [])
return new SpanTerm($term);
}

/**
* span_containing query.
*
* @param AbstractSpanQuery|null $little
* @param AbstractSpanQuery|null $big
*
* @return SpanContaining
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html
*/
public function span_containing(AbstractSpanQuery $little = null, AbstractSpanQuery $big = null)
{
return new SpanContaining($little, $big);
}

/**
* span_within query.
*
* @param AbstractSpanQuery|null $little
* @param AbstractSpanQuery|null $big
*
* @return SpanWithin
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html
*/
public function span_within(AbstractSpanQuery $little = null, AbstractSpanQuery $big = null)
{
return new SpanWithin($little, $big);
}

/**
* term query.
*
Expand Down
87 changes: 87 additions & 0 deletions test/Elastica/Query/SpanContainingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Query\SpanContaining;
use Elastica\Query\SpanNear;
use Elastica\Query\SpanTerm;
use Elastica\Test\Base as BaseTest;

class SpanContainingTest extends BaseTest
{
/**
* @group unit
*/
public function testToArray()
{
$field = 'name';
$spanTermQuery1 = new SpanTerm([$field => 'nicolas']);
$spanTermQuery2 = new SpanTerm([$field => ['value' => 'alekitto', 'boost' => 1.5]]);
$spanTermQuery3 = new SpanTerm([$field => 'foobar']);
$spanNearQuery = new SpanNear([$spanTermQuery1, $spanTermQuery2], 5);

$spanContainingQuery = new SpanContaining($spanTermQuery3, $spanNearQuery);

$expected = [
'span_containing' => [
'big' => [
'span_near' => [
'clauses' => [
[
'span_term' => [
'name' => 'nicolas',
],
],
[
'span_term' => [
'name' => [
'value' => 'alekitto',
'boost' => 1.5,
],
],
],
],
'slop' => 5,
'in_order' => false,
],
],
'little' => [
'span_term' => [
'name' => 'foobar',
],
],
],
];

$this->assertEquals($expected, $spanContainingQuery->toArray());
}

/**
* @group functional
*/
public function testSpanContaining()
{
$field = 'lorem';
$value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse odio lacus, aliquam nec nulla quis, aliquam eleifend eros.';

$index = $this->_createIndex();
$type = $index->getType('test');

$docHitData = [$field => $value];
$doc = new Document(1, $docHitData);
$type->addDocument($doc);
$index->refresh();

$spanTermQuery1 = new SpanTerm([$field => 'adipiscing']);
$spanTermQuery2 = new SpanTerm([$field => 'lorem']);
$spanNearQuery = new SpanNear([$spanTermQuery1, $spanTermQuery2], 5);

$spanContainingQuery = new SpanContaining(new SpanTerm([$field => 'amet']), $spanNearQuery);
$resultSet = $type->search($spanContainingQuery);
$this->assertEquals(1, $resultSet->count());

$spanContainingQuery = new SpanContaining(new SpanTerm([$field => 'not-matching']), $spanNearQuery);
$resultSet = $type->search($spanContainingQuery);
$this->assertEquals(0, $resultSet->count());
}
}
Loading