Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit c8f6622

Browse files
committed
Added ability to specify what fields you want to select
1 parent eaf1008 commit c8f6622

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Basic search usage example:
191191
```php
192192
// set query string
193193
App\MyModel::search('phone')
194+
// specify columns to select
195+
->select(['title', 'price'])
194196
// filter
195197
->where('color', 'red')
196198
// sort

src/Builders/FilterBuilder.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class FilterBuilder extends Builder
2020
*/
2121
public $collapse;
2222

23+
/**
24+
* @var array
25+
*/
26+
public $select = [];
27+
2328
public function __construct($model, $callback = null)
2429
{
2530
$this->model = $model;
@@ -175,9 +180,9 @@ public function whereGeoPolygon($field, array $points)
175180
return $this;
176181
}
177182

178-
public function orderBy($column, $direction = 'asc')
183+
public function orderBy($field, $direction = 'asc')
179184
{
180-
$this->orders[] = [$column => strtolower($direction) == 'asc' ? 'asc' : 'desc'];
185+
$this->orders[] = [$field => strtolower($direction) == 'asc' ? 'asc' : 'desc'];
181186

182187
return $this;
183188
}
@@ -251,4 +256,18 @@ public function collapse(string $field)
251256

252257
return $this;
253258
}
259+
260+
/**
261+
* @param mixed $fields
262+
* @return $this
263+
*/
264+
public function select($fields)
265+
{
266+
$this->select = array_merge(
267+
$this->select,
268+
array_wrap($fields)
269+
);
270+
271+
return $this;
272+
}
254273
}

src/ElasticEngine.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ protected function buildSearchQueryPayload(Builder $builder, $queryPayload, arra
7575
}
7676

7777
$payload = (new TypePayload($builder->model))
78+
->setIfNotEmpty('body._source', $builder->select)
7879
->setIfNotEmpty('body.query.bool', $queryPayload)
7980
->setIfNotEmpty('body.collapse.field', $builder->collapse)
8081
->setIfNotEmpty('body.sort', $builder->orders)
@@ -193,21 +194,33 @@ public function mapIds($results)
193194
return array_pluck($results['hits']['hits'], '_id');
194195
}
195196

197+
/**
198+
* @inheritdoc
199+
*/
196200
public function map($results, $model)
197201
{
198202
if ($this->getTotalCount($results) == 0) {
199203
return Collection::make();
200204
}
201205

202-
$ids = $this->mapIds($results);
206+
$hits = $results['hits']['hits'];
207+
$firstHit = reset($hits);
208+
209+
$primaryKey = $model->getKeyName();
203210

204-
$modelKey = $model->getKeyName();
211+
$columns = array_merge(
212+
array_keys($firstHit['_source']),
213+
[$primaryKey]
214+
);
215+
216+
$ids = $this->mapIds($results);
205217

206-
$models = $model->whereIn($modelKey, $ids)
207-
->get()
208-
->keyBy($modelKey);
218+
$models = $model
219+
->whereIn($primaryKey, $ids)
220+
->get($columns)
221+
->keyBy($primaryKey);
209222

210-
return Collection::make($results['hits']['hits'])->map(function($hit) use ($models) {
223+
return Collection::make($hits)->map(function($hit) use ($models) {
211224
$id = $hit['_id'];
212225

213226
if (isset($models[$id])) {

tests/Builders/FilterBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,16 @@ public function testCollapse()
385385
$this->builder->collapse
386386
);
387387
}
388+
389+
public function testSelect()
390+
{
391+
$this
392+
->builder
393+
->select(['foo', 'bar']);
394+
395+
$this->assertEquals(
396+
['foo', 'bar'],
397+
$this->builder->select
398+
);
399+
}
388400
}

tests/ElasticEngineTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function testBuildSearchQueryPayloadCollection()
4444
]
4545
];
4646
})
47+
->select('title')
48+
->select(['price', 'color'])
4749
->where('id', '>', 20)
4850
->orderBy('id', 'asc')
4951
->collapse('brand')
@@ -60,6 +62,11 @@ public function testBuildSearchQueryPayloadCollection()
6062
'index' => 'test',
6163
'type' => 'test',
6264
'body' => [
65+
'_source' => [
66+
'title',
67+
'price',
68+
'color'
69+
],
6370
'query' => [
6471
'bool' => [
6572
'must' => [
@@ -98,6 +105,11 @@ public function testBuildSearchQueryPayloadCollection()
98105
'index' => 'test',
99106
'type' => 'test',
100107
'body' => [
108+
'_source' => [
109+
'title',
110+
'price',
111+
'color'
112+
],
101113
'query' => [
102114
'bool' => [
103115
'must' => [
@@ -249,8 +261,18 @@ public function testMap()
249261
'hits' => [
250262
'total' => 2,
251263
'hits' => [
252-
['_id' => 1],
253-
['_id' => 2]
264+
[
265+
'_id' => 1,
266+
'_source' => [
267+
'title' => 'foo'
268+
]
269+
],
270+
[
271+
'_id' => 2,
272+
'_source' => [
273+
'title' => 'bar'
274+
]
275+
]
254276
]
255277
]
256278
];

0 commit comments

Comments
 (0)