diff --git a/docs/Query/SpanContaining.md b/docs/Query/SpanContaining.md new file mode 100644 index 0000000000000000000000000000000000000000..95de170af980b0620e6f125e704128ae3d857a2c --- /dev/null +++ b/docs/Query/SpanContaining.md @@ -0,0 +1,45 @@ +# Span Containing query + +> More info about Boosting query is in the [official elasticsearch docs][1] + +Returns matches which enclose another span query. + +```JSON +{ + "span_containing" : { + "little" : { + "span_term" : { "field1" : "foo" } + }, + "big" : { + "span_near" : { + "clauses" : [ + { "span_term" : { "field1" : "bar" } }, + { "span_term" : { "field1" : "baz" } } + ], + "slop" : 5, + "in_order" : true + } + } + } +} +``` + +And now the query via DSL: + +```php +$spanTermQuery = new SpanTermQuery('field1', 'foo'); +$spanNearQuery = new SpanNearQuery(); + +$spanNearQuery->setSlop(5); +$spanNearQuery->addParameter('in_order', true); +$spanNearQuery->addQuery(new SpanTermQuery('field1', 'bar')); +$spanNearQuery->addQuery(new SpanTermQuery('field1', 'baz')); + +$spanContainingQuery = new SpanContainingQuery( + $spanTermQuery, + $spanNearQuery +); +``` + + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html diff --git a/src/Query/Span/SpanContainingQuery.php b/src/Query/Span/SpanContainingQuery.php new file mode 100644 index 0000000000000000000000000000000000000000..eaf149657eade62e363a521f1fd9388a5bfbfeef --- /dev/null +++ b/src/Query/Span/SpanContainingQuery.php @@ -0,0 +1,99 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Query\Span; + +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Elasticsearch span containing query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html + */ +class SpanContainingQuery implements SpanQueryInterface +{ + use ParametersTrait; + + /** + * @param SpanQueryInterface + */ + private $little; + + /** + * @param SpanQueryInterface + */ + private $big; + + /** + * @param SpanQueryInterface $little + * @param SpanQueryInterface $big + */ + public function __construct(SpanQueryInterface $little, SpanQueryInterface $big) + { + $this->setLittle($little); + $this->setBig($big); + } + + /** + * @return SpanQueryInterface + */ + public function getLittle() + { + return $this->little; + } + + /** + * @param SpanQueryInterface $little + */ + public function setLittle(SpanQueryInterface $little) + { + $this->little = $little; + } + + /** + * @return SpanQueryInterface + */ + public function getBig() + { + return $this->big; + } + + /** + * @param SpanQueryInterface $big + */ + public function setBig(SpanQueryInterface $big) + { + $this->big = $big; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'span_containing'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $output = [ + 'little' => $this->getLittle()->toArray(), + 'big' => $this->getBig()->toArray(), + ]; + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } +} diff --git a/tests/Query/Span/SpanContainingQueryTest.php b/tests/Query/Span/SpanContainingQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d3a934b7a703a1879d3e3c9ce5d630cf785227bd --- /dev/null +++ b/tests/Query/Span/SpanContainingQueryTest.php @@ -0,0 +1,57 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Tests\Query\Span; + +use ONGR\ElasticsearchDSL\Query\Span\SpanContainingQuery; + +/** + * Unit test for SpanContainingQuery. + */ +class SpanContainingQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests for toArray(). + */ + public function testToArray() + { + $query = new SpanContainingQuery( + $this->getSpanQueryMock('foo'), + $this->getSpanQueryMock('bar') + ); + $result = [ + 'span_containing' => [ + 'little' => [ + 'span_term' => ['user' => 'foo'], + ], + 'big' => [ + 'span_term' => ['user' => 'bar'], + ], + ], + ]; + $this->assertEquals($result, $query->toArray()); + } + + /** + * @param string $value + * + * @returns \PHPUnit_Framework_MockObject_MockObject + */ + private function getSpanQueryMock($value) + { + $mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface'); + $mock + ->expects($this->once()) + ->method('toArray') + ->willReturn(['span_term' => ['user' => $value]]); + return $mock; + } +}