Skip to content

Commit

Permalink
order of sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Yavuz Selim Bilgin committed Oct 16, 2017
1 parent ed9577e commit b72e91e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "thomasjsn/laravel-scout-elastic",
"name": "bilginpro/laravel-scout-elastic",
"description": "Elastic Driver for Laravel Scout",
"homepage": "https://github.com/thomasjsn/laravel-scout-elastic",
"homepage": "https://github.com/bilginpro/laravel-scout-elastic",
"license": "MIT",
"keywords": ["laravel", "scout", "elasticsearch", "elastic"],
"require": {
Expand Down
11 changes: 11 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
env('ELASTICSEARCH_HOST', 'http://localhost'),
],

/*
|--------------------------------------------------------------------------
| Prior sorting parameter
|--------------------------------------------------------------------------
|
| predefined or query_builder
|
*/

'prior_sorting_param' => 'query_builder',

/*
|--------------------------------------------------------------------------
| Queries and query parameters
Expand Down
49 changes: 28 additions & 21 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ public function __construct(Elastic $elastic, $queryConfig)
/**
* Update the given model in the index.
*
* @param Collection $models
* @param Collection $models
* @return void
*/
public function update($models)
{
$params['body'] = [];

$models->each(function($model) use (&$params)
{
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'update' => [
'_id' => $model->getKey(),
Expand All @@ -64,15 +63,14 @@ public function update($models)
/**
* Remove the given model from the index.
*
* @param Collection $models
* @param Collection $models
* @return void
*/
public function delete($models)
{
$params['body'] = [];

$models->each(function($model) use (&$params)
{
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'delete' => [
'_id' => $model->getKey(),
Expand All @@ -88,7 +86,7 @@ public function delete($models)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param Builder $builder
* @return mixed
*/
public function search(Builder $builder)
Expand All @@ -103,9 +101,9 @@ public function search(Builder $builder)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param int $perPage
* @param int $page
* @param Builder $builder
* @param int $perPage
* @param int $page
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page)
Expand All @@ -117,16 +115,16 @@ public function paginate(Builder $builder, $perPage, $page)
'size' => $perPage,
]);

$result['nbPages'] = $result['hits']['total']/$perPage;
$result['nbPages'] = $result['hits']['total'] / $perPage;

return $result;
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param array $options
* @param Builder $builder
* @param array $options
* @return mixed
*/
protected function performSearch(Builder $builder, array $options = [])
Expand Down Expand Up @@ -172,9 +170,18 @@ protected function performSearch(Builder $builder, array $options = [])
}

// Sorting
if(isset($options['sorting']) && count($options['sorting'])) {
$params['body']['sort'] = array_merge($params['body']['sort'],
$options['sorting']);
if (isset($options['sorting']) && count($options['sorting'])) {
if (config('elasticsearch.prior_sorting_param', 'predefined') == 'predefined') {
$params['body']['sort'] = array_merge(
$params['body']['sort'],
$options['sorting']
);
} else {
$params['body']['sort'] = array_merge(
$options['sorting'],
$params['body']['sort']
);
}
}

return $this->elastic->search($params);
Expand All @@ -183,7 +190,7 @@ protected function performSearch(Builder $builder, array $options = [])
/**
* Get the filter array for the query.
*
* @param Builder $builder
* @param Builder $builder
* @return array
*/
protected function filters(Builder $builder)
Expand All @@ -207,7 +214,7 @@ protected function sorting(Builder $builder)
/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @param mixed $results
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
Expand All @@ -218,8 +225,8 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return Collection
*/
public function map($results, $model)
Expand Down Expand Up @@ -247,7 +254,7 @@ public function map($results, $model)
/**
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
* @param mixed $results
* @return int
*/
public function getTotalCount($results)
Expand Down

0 comments on commit b72e91e

Please sign in to comment.