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

Merge pull request #102 from einorler/query_span_containing

added SpanContaining query
parents dc569d21 8eb502a2
No related branches found
No related tags found
No related merge requests found
# 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
<?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];
}
}
<?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;
}
}
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