Skip to content

Commit

Permalink
Merge pull request #1 from thomasjsn/query-experiment
Browse files Browse the repository at this point in the history
Get master up to date
  • Loading branch information
thomasjsn authored Feb 10, 2017
2 parents 26888e4 + f503fe9 commit f0b7a84
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ composer.lock
.DS_Store
Thumbs.db
/phpunit.xml
/build
/build
/.idea
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"keywords": ["laravel", "scout", "elasticsearch", "elastic"],
"require": {
"php": ">=5.6.4",
"laravel/scout": "^1.0",
"illuminate/support": "^5.3",
"illuminate/database": "^5.3",
"elasticsearch/elasticsearch": "^2.2"
"laravel/scout": "^3.0",
"elasticsearch/elasticsearch": "^5.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand All @@ -24,6 +22,9 @@
"ScoutEngines\\Elasticsearch\\Test\\": "tests"
}
},
"minimum-stability": "stable",
"suggest": {
"elasticsearch/elasticsearch": "Required to use the Elasticsearch engine (^5.0)."
},
"minimum-stability": "dev",
"prefer-stable": true
}
37 changes: 29 additions & 8 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function paginate(Builder $builder, $perPage, $page)
'size' => $perPage,
]);

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

return $result;
}
Expand All @@ -131,7 +131,14 @@ protected function performSearch(Builder $builder, array $options = [])
'body' => [
'query' => [
'bool' => [
'must' => [['query_string' => [ 'query' => "*{$builder->query}*"]]]
'must' => [
[
'query_string' => [
'query' => "{$builder->query}",
'default_operator' => "AND"
]
]
]
]
]
]
Expand All @@ -146,8 +153,7 @@ protected function performSearch(Builder $builder, array $options = [])
}

if (isset($options['numericFilters']) && count($options['numericFilters'])) {
$params['body']['query']['bool']['must'] = array_merge($params['body']['query']['bool']['must'],
$options['numericFilters']);
$params['body']['query']['bool']['filter'] = $options['numericFilters'];
}

return $this->elastic->search($params);
Expand All @@ -162,10 +168,21 @@ protected function performSearch(Builder $builder, array $options = [])
protected function filters(Builder $builder)
{
return collect($builder->wheres)->map(function ($value, $key) {
return ['match_phrase' => [$key => $value]];
return ['term' => [$key => $value]];
})->values()->all();
}

/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
{
return collect($results['hits']['hits'])->pluck('_id')->values();
}

/**
* Map the given results to instances of the given model.
*
Expand All @@ -180,15 +197,19 @@ public function map($results, $model)
}

$keys = collect($results['hits']['hits'])
->pluck('_id')->values()->all();
->pluck('_id')->values()->all();

$models = $model->whereIn(
$model->getKeyName(), $keys
)->get()->keyBy($model->getKeyName());

return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
return $models[$hit['_id']];
});
$key = $hit['_id'];

if (isset($models[$key])) {
return $models[$key];
}
})->filter();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ElasticsearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class ElasticsearchProvider extends ServiceProvider
*/
public function boot()
{
resolve(EngineManager::class)->extend('elasticsearch', function($app){
resolve(EngineManager::class)->extend('elasticsearch', function($app) {
return new ElasticsearchEngine(ElasticBuilder::create()
->setHosts(config('scout.elasticsearch.config.hosts'))
->setHosts(config('scout.elasticsearch.hosts'))
->build(),
config('scout.elasticsearch.index')
);
Expand Down
15 changes: 13 additions & 2 deletions tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,19 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
'query' => [
'bool' => [
'must' => [
['query_string' => ['query' => '*zonda*']],
['match_phrase' => ['foo' => 1]]
[
'query_string' => [
'query' => 'zonda',
'default_operator' => "AND"
]
]
],
'filter' => [
[
'term' => [
'foo' => 1
]
]
]
]
]
Expand Down

0 comments on commit f0b7a84

Please sign in to comment.