Skip to content

Commit d804c06

Browse files
author
Alessandro Chitolina
committed
add span_first query
1 parent 6ee9a13 commit d804c06

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace Elastica\Query;
3+
4+
/**
5+
* Abstract query object. Should be extended by all query types.
6+
*
7+
* @author Alessandro Chitolina <[email protected]>
8+
*/
9+
abstract class AbstractSpanQuery extends AbstractQuery
10+
{
11+
}

lib/Elastica/Query/SpanFirst.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace Elastica\Query;
3+
4+
use Elastica\Exception\InvalidException;
5+
6+
/**
7+
* SpanFirst query.
8+
*
9+
* @author Alessandro Chitolina <[email protected]>
10+
*
11+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html
12+
*/
13+
class SpanFirst extends AbstractSpanQuery
14+
{
15+
/**
16+
* Set the query to be wrapped into the span multi query.
17+
*
18+
* @param \Elastica\Query\AbstractSpanQuery|array $args Matching query
19+
*
20+
* @return $this
21+
*/
22+
public function setMatch($args)
23+
{
24+
return $this->_addQuery('match', $args);
25+
}
26+
27+
/**
28+
* Set the maximum end position for the match query.
29+
*
30+
* @param int $end
31+
*
32+
* @return $this
33+
*/
34+
public function setEnd($end)
35+
{
36+
$this->setParam('end', $end);
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* Adds a query to the current object.
43+
*
44+
* @param string $type Query type
45+
* @param \Elastica\Query\AbstractQuery|array $args Query
46+
*
47+
* @throws \Elastica\Exception\InvalidException If not valid query
48+
*
49+
* @return $this
50+
*/
51+
protected function _addQuery($type, $args)
52+
{
53+
if (!is_array($args) && !($args instanceof AbstractSpanQuery)) {
54+
throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractSpanQuery');
55+
}
56+
57+
return $this->setParam($type, $args);
58+
}
59+
}

lib/Elastica/Query/SpanMulti.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
/**
77
* SpanMulti query.
88
*
9+
* @author Alessandro Chitolina <[email protected]>
10+
*
911
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html
1012
*/
11-
class SpanMulti extends AbstractQuery
13+
class SpanMulti extends AbstractSpanQuery
1214
{
1315
/**
1416
* Set the query to be wrapped into the span multi query.

lib/Elastica/Query/SpanTerm.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
/**
55
* SpanTerm query.
66
*
7+
* @author Alessandro Chitolina <[email protected]>
8+
*
79
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html
810
*/
9-
class SpanTerm extends AbstractQuery
11+
class SpanTerm extends AbstractSpanQuery
1012
{
1113
/**
1214
* Constructs the SpanTerm query object.

test/Elastica/Query/SpanFirstTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Elastica\Test\Query;
3+
4+
use Elastica\Query\SpanFirst;
5+
use Elastica\Query\SpanTerm;
6+
use Elastica\Test\Base as BaseTest;
7+
8+
class SpanFirstTest extends BaseTest
9+
{
10+
/**
11+
* @group unit
12+
*/
13+
public function testToArray()
14+
{
15+
$query = new SpanFirst();
16+
$query->setMatch(new SpanTerm(['user' => 'kimchy']));
17+
$query->setEnd(3);
18+
19+
$data = $query->toArray();
20+
21+
$this->assertEquals([
22+
'span_first' => [
23+
'match' => [
24+
'span_term' => ['user' => 'kimchy'],
25+
],
26+
'end' => 3,
27+
],
28+
], $data);
29+
}
30+
}

test/Elastica/Query/SpanMultiTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public function testToArray()
2020
$this->assertEquals([
2121
'span_multi' => [
2222
'match' => [
23-
'prefix' => ['user' => ['value' => 'ki']]
24-
]
25-
]
23+
'prefix' => ['user' => ['value' => 'ki']],
24+
],
25+
],
2626
], $data);
2727
}
2828
}

0 commit comments

Comments
 (0)