Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
Support for "WHERE IN" searches if array is passed to where (#55)
Browse files Browse the repository at this point in the history
Right now passing an array results in: "illegal_state_exception: Can't get text on a START_ARRAY"
Solution based on http://stackoverflow.com/a/40737488/7362396
  • Loading branch information
kronthto authored and ErickTamayo committed Aug 8, 2017
1 parent 4128cbe commit 25be726
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ protected function performSearch(Builder $builder, array $options = [])
protected function filters(Builder $builder)
{
return collect($builder->wheres)->map(function ($value, $key) {
if (is_array($value)) {
return ['terms' => [$key => $value]];
}

return ['match_phrase' => [$key => $value]];
})->values()->all();
}
Expand Down
4 changes: 3 additions & 1 deletion tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
'bool' => [
'must' => [
['query_string' => ['query' => '*zonda*']],
['match_phrase' => ['foo' => 1]]
['match_phrase' => ['foo' => 1]],
['terms' => ['bar' => [1, 3]]],
]
]
],
Expand All @@ -76,6 +77,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
$engine = new ElasticsearchEngine($client, 'scout');
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda');
$builder->where('foo', 1);
$builder->where('bar', [1, 3]);
$builder->orderBy('id', 'desc');
$engine->search($builder);
}
Expand Down

0 comments on commit 25be726

Please sign in to comment.