Skip to content

Commit

Permalink
v3.0.2
Browse files Browse the repository at this point in the history
bumping to php 5.6.6 (Earliest version that can support php elasticsearch) (ErickTamayo#48)
`within` now is being taken into account when specified ErickTamayo#31
Now if the record is not found on the db will not throw undefined offset ErickTamayo#36
Implemented orderBy ErickTamayo#46
  • Loading branch information
ErickTamayo authored Mar 31, 2017
1 parent 4bf38de commit 6b4c806
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
language: php
php:
- 7.0
- 5.6
- 5.6.6

before_script:
- sleep 10
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script: vendor/phpunit/phpunit/phpunit --verbose
script: vendor/phpunit/phpunit/phpunit --verbose

services:
- elasticsearch

27 changes: 24 additions & 3 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function performSearch(Builder $builder, array $options = [])
{
$params = [
'index' => $this->index,
'type' => $builder->model->searchableAs(),
'type' => $builder->index ?: $builder->model->searchableAs(),
'body' => [
'query' => [
'bool' => [
Expand All @@ -137,6 +137,10 @@ protected function performSearch(Builder $builder, array $options = [])
]
];

if ($sort = $this->sort($builder)) {
$params['body']['sort'] = $sort;
}

if (isset($options['from'])) {
$params['body']['from'] = $options['from'];
}
Expand Down Expand Up @@ -198,8 +202,8 @@ public function map($results, $model)
)->get()->keyBy($model->getKeyName());

return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
return $models[$hit['_id']];
});
return isset($models[$hit['_id']]) ? $models[$hit['_id']] : null;
})->filter();
}

/**
Expand All @@ -212,4 +216,21 @@ public function getTotalCount($results)
{
return $results['hits']['total'];
}

/**
* Generates the sort if theres any.
*
* @param Builder $builder
* @return array|null
*/
protected function sort($builder)
{
if (count($builder->orders) == 0) {
return null;
}

return collect($builder->orders)->map(function($order) {
return [$order['column'] => $order['direction']];
})->toArray();
}
}
4 changes: 4 additions & 0 deletions tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
['match_phrase' => ['foo' => 1]]
]
]
],
'sort' => [
['id' => 'desc']
]
]
]);

$engine = new ElasticsearchEngine($client, 'scout');
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda');
$builder->where('foo', 1);
$builder->orderBy('id', 'desc');
$engine->search($builder);
}

Expand Down

0 comments on commit 6b4c806

Please sign in to comment.