Skip to content

Commit

Permalink
Merge branch 'feature-source-and-collapse'
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Jun 20, 2020
2 parents 28dd75e + b1c972f commit d4823c6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ $request->setSuggest([
]
]);

$request->setSource(['message', 'post_date']);

$request->setCollapse([
'field' => 'user'
]);

$request->setSort([
['post_date' => ['order' => 'asc']],
'_score'
Expand Down
40 changes: 34 additions & 6 deletions src/Search/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,33 @@ final class SearchRequest implements ArrayableInterface
*/
private $query;
/**
* @var array
* @var array|null
*/
private $highlight;
/**
* @var array
* @var array|null
*/
private $sort;
/**
* @var int
* @var int|null
*/
private $from;
/**
* @var int
* @var int|null
*/
private $size;
/**
* @var array
* @var array|null
*/
private $suggest;
/**
* @var bool|string|array|null
*/
private $source;
/**
* @var array|null
*/
private $collapse;

public function __construct(array $query)
{
Expand Down Expand Up @@ -67,18 +75,38 @@ public function setSuggest(array $suggest): self
return $this;
}

/**
* @param bool|string|array $source
* @return self
*/
public function setSource($source): self
{
$this->source = $source;
return $this;
}

public function setCollapse(array $collapse): self
{
$this->collapse = $collapse;
return $this;
}

public function toArray(): array
{
$request = [
'query' => $this->query
];

foreach (['highlight', 'sort', 'from', 'size', 'suggest'] as $property) {
foreach (['highlight', 'sort', 'from', 'size', 'suggest', 'collapse'] as $property) {
if (isset($this->$property)) {
$request[$property] = $this->$property;
}
}

if (isset($this->source)) {
$request['_source'] = $this->source;
}

return $request;
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Indices/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public function test_property_magic_setter(string $method, array $arguments): vo
], $mapping->toArray());
}

public function test_default_array_conversion(): void
public function test_default_array_casting(): void
{
$this->assertSame([], (new Mapping())->toArray());
}

public function test_configured_array_conversion(): void
public function test_configured_array_casting(): void
{
$mapping = (new Mapping())
->disableFieldNames()
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Indices/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function test_option_magic_setter(string $method, array $arguments): void
], $settings->toArray());
}

public function test_default_array_conversion(): void
public function test_default_array_casting(): void
{
$this->assertSame([], (new Settings())->toArray());
}

public function test_configured_array_conversion(): void
public function test_configured_array_casting(): void
{
$settings = (new Settings())
->index([
Expand Down
61 changes: 55 additions & 6 deletions tests/Unit/Search/SearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
final class SearchRequestTest extends TestCase
{
public function test_array_conversion_with_query(): void
public function test_array_casting_with_query(): void
{
$request = new SearchRequest([
'term' => [
Expand All @@ -29,7 +29,7 @@ public function test_array_conversion_with_query(): void
], $request->toArray());
}

public function test_array_conversion_with_query_and_highlight(): void
public function test_array_casting_with_query_and_highlight(): void
{
$request = new SearchRequest([
'match' => [
Expand Down Expand Up @@ -57,7 +57,7 @@ public function test_array_conversion_with_query_and_highlight(): void
], $request->toArray());
}

public function test_array_conversion_with_query_and_sort(): void
public function test_array_casting_with_query_and_sort(): void
{
$request = new SearchRequest([
'match_all' => new stdClass()
Expand All @@ -79,7 +79,7 @@ public function test_array_conversion_with_query_and_sort(): void
], $request->toArray());
}

public function test_array_conversion_with_query_and_from(): void
public function test_array_casting_with_query_and_from(): void
{
$request = new SearchRequest([
'match_all' => new stdClass()
Expand All @@ -95,7 +95,7 @@ public function test_array_conversion_with_query_and_from(): void
], $request->toArray());
}

public function test_array_conversion_with_query_and_size(): void
public function test_array_casting_with_query_and_size(): void
{
$request = new SearchRequest([
'match_all' => new stdClass()
Expand All @@ -111,7 +111,7 @@ public function test_array_conversion_with_query_and_size(): void
], $request->toArray());
}

public function test_array_conversion_with_query_and_suggest(): void
public function test_array_casting_with_query_and_suggest(): void
{
$request = new SearchRequest([
'match_none' => new stdClass()
Expand Down Expand Up @@ -140,4 +140,53 @@ public function test_array_conversion_with_query_and_suggest(): void
]
], $request->toArray());
}

public function sourceProvider(): array
{
return [
[false],
['obj1.*'],
[['obj1.*', 'obj2.*']],
[['includes' => ['obj1.*', 'obj2.*'], 'excludes' => ['*.description']]],
];
}

/**
* @dataProvider sourceProvider
*/
public function test_array_casting_with_source($source): void
{
$request = new SearchRequest([
'match_all' => new stdClass()
]);

$request->setSource($source);

$this->assertEquals([
'query' => [
'match_all' => new stdClass()
],
'_source' => $source
], $request->toArray());
}

public function test_array_casting_with_collapse(): void
{
$request = new SearchRequest([
'match_all' => new stdClass()
]);

$request->setCollapse([
'field' => 'user'
]);

$this->assertEquals([
'query' => [
'match_all' => new stdClass()
],
'collapse' => [
'field' => 'user'
]
], $request->toArray());
}
}

0 comments on commit d4823c6

Please sign in to comment.