Skip to content
Snippets Groups Projects
Commit 5fe4dca5 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas Committed by GitHub
Browse files

Merge pull request #101 from einorler/query_span_within

added span within query
parents 49d9be02 e3d35016
No related branches found
No related tags found
No related merge requests found
# 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
<?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';
}
}
<?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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment