diff --git a/docs/Query/SpanWithin.md b/docs/Query/SpanWithin.md new file mode 100644 index 0000000000000000000000000000000000000000..4d5758bca347ee707aae4a9412d04af638862419 --- /dev/null +++ b/docs/Query/SpanWithin.md @@ -0,0 +1,50 @@ +# Span Within query + +> More info about Boosting query is in the [official elasticsearch docs][1] + +Returns matches which are enclosed inside another span query. + +```JSON +{ + "span_within" : { + "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')); + +$spanWithinQuery = new SpanWithinQuery( + $spanTermQuery, + $spanNearQuery +); + +$search = new Search(); +$search->addQuery($spanWithinQuery); + +$queryArray = $search->toArray(); +``` + + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html diff --git a/src/Query/Span/SpanWithinQuery.php b/src/Query/Span/SpanWithinQuery.php new file mode 100644 index 0000000000000000000000000000000000000000..c7d8ade1efbdd33acca99510ed3d007427962947 --- /dev/null +++ b/src/Query/Span/SpanWithinQuery.php @@ -0,0 +1,28 @@ +<?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; + +/** + * Elasticsearch span within query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html + */ +class SpanWithinQuery extends SpanContainingQuery +{ + /** + * {@inheritdoc} + */ + public function getType() + { + return 'span_within'; + } +} diff --git a/tests/Query/Span/SpanWithinQueryTest.php b/tests/Query/Span/SpanWithinQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3a933c4da1a63dd923059a536767a481a8ff5386 --- /dev/null +++ b/tests/Query/Span/SpanWithinQueryTest.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\SpanWithinQuery; + +/** + * Unit test for SpanWithinQuery. + */ +class SpanWithinQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests for toArray(). + */ + public function testToArray() + { + $query = new SpanWithinQuery( + $this->getSpanQueryMock('foo'), + $this->getSpanQueryMock('bar') + ); + $result = [ + 'span_within' => [ + '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; + } +}