Skip to content

Commit 5a1a464

Browse files
committed
Various changes
More docs for the getter chain inside Result
1 parent aca9b01 commit 5a1a464

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

Diff for: README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ The returned `response` object is a rich wrapper around the JSON returned from E
120120

121121
Each *hit* is wrapped in the `Result` class.
122122

123+
`Result` has a dynamic getter:
124+
125+
* index, type, id, score and source are pulled from the top-level of the hit.
126+
e.g. index is hit[_index], type is hit[_type], etc
127+
* if not one of the above, it looks for an existing item in the top-level hit.
128+
e.g. _version is hit[_version], etc
129+
* if not one of the above, it looks for an existing item in hit[_source] (the document).
130+
e.g. title is hit[_source][title]
131+
* if nothing resolves from above, it triggers a notice and returns null
132+
123133
The `response` object delegates to an internal `Collection`, so it supports all the usual methods: `map`, `filter`, `each`, etc.
124134

125135
```php
@@ -202,7 +212,8 @@ You can implement pagination with the `from` and `size` search parameters. Howev
202212
# Delegates to the results on page 2 with 20 per page
203213
$response->perPage(20)->page(2);
204214

205-
# Records on page 2 with 20 per page (in the same order as results)
215+
# Records on page 2 with 20 per page; records ordered the same as results
216+
# Order of the `page` and `perPage` calls doesn't matter
206217
$response->page(2)->perPage(20)->records();
207218

208219
# Results on page 2 with (default) 15 results per page

Diff for: ReadmeTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Datashaman\Elasticsearch\Model\Tests;
44

55
use Datashaman\Elasticsearch\Model\ElasticsearchModel;
6+
use Datashaman\Elasticsearch\Model\Response\Result;
67
use Illuminate\Database\Eloquent\Model as Eloquent;
78
use Illuminate\Database\Schema\Blueprint;
89
use Schema;
@@ -102,5 +103,48 @@ public function testSearch()
102103
'Quick brown fox',
103104
'Fast black dogs',
104105
], $ordered);
106+
107+
/**
108+
* Only have 3 results so perPage is going to be 1 for all these examples.
109+
*/
110+
$response = Article::search('*', [
111+
'sort' => [
112+
'title',
113+
],
114+
]);
115+
116+
/**
117+
* Just so it's clear these are in the expected title order,
118+
*/
119+
$this->assertEquals([
120+
'Fast black dogs',
121+
'Quick brown fox',
122+
'Swift green frogs',
123+
], $response->map(function ($a) { return $a->title; })->all());
124+
125+
/** Response can be used as an array (of the results) */
126+
$page = $response->perPage(1)->page(2);
127+
128+
$this->assertEquals(1, count($page));
129+
130+
$this->assertInstanceOf(Result::class, $page[0]);
131+
132+
/**
133+
* Result has a dynamic getter:
134+
*
135+
* index, type, id, score and source are pulled from the top-level of the hit.
136+
* e.g. index is hit[_index], type is hit[_type], etc
137+
*
138+
* if not one of the above, it looks for an existing item in the top-level hit.
139+
* e.g. _version is hit[_version], etc
140+
*
141+
* if not one of the above, it looks for an existing item in hit[_source] (the document).
142+
* e.g. title is hit[_source][title]
143+
*
144+
* if nothing resolves from above, it triggers a notice and returns null
145+
*/
146+
$article = $page[0];
147+
148+
$this->assertEquals('Quick brown fox', $article->title);
105149
}
106150
}

Diff for: src/Response/Result.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct($hit = [])
1313

1414
public function __get($name)
1515
{
16-
if ($name == 'id' || $name == 'type') {
16+
if (in_array($name, ['index', 'type', 'id', 'score', 'source'])) {
1717
return array_get($this->hit, '_'.$name);
1818
}
1919

@@ -33,5 +33,12 @@ public function __get($name)
3333
' in '.$trace[0]['file'].
3434
' on line '.$trace[0]['line'],
3535
E_USER_NOTICE);
36+
37+
return null;
38+
}
39+
40+
public function toArray()
41+
{
42+
return $this->hit;
3643
}
3744
}

Diff for: tests/Response/ResultTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ public function testGetterEmitsErrorAndReturnsNull()
5151
$result = new Result([]);
5252
$this->assertNull($result->foo);
5353
}
54+
55+
public function testToArray()
56+
{
57+
$result = new Result(['foo' => 'bar', '_id' => 1]);
58+
$this->assertEquals(['foo' => 'bar', '_id' => 1], $result->toArray());
59+
}
5460
}

Diff for: watch-test.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#/usr/bin/env bash
22

3-
inotifywait -m -r -e close_write src/ tests/ | while read LINE
3+
# inotifywait -m -r -e close_write src/ tests/ | while read LINE
4+
inotifywait -m -r -e close_write src/ ReadmeTest.php | while read LINE
45
do
56
echo $LINE
6-
phpunit
7+
# phpunit
8+
phpunit ReadmeTest.php
79
done

0 commit comments

Comments
 (0)