Skip to content

Commit a562144

Browse files
committed
Fix #2
Cast nulls to empty strings in `toVector()`.
1 parent f452308 commit a562144

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to `pmatseykanets/laravel-hipchat-slash` will be documented
44

55
## [Unreleased]
66

7+
## [0.2.1](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.2.1) - 2016-12-23
8+
### Changed
9+
- Fixed #2. Cast nulls to empty strings in `toVector()`.
10+
711
## [0.2.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.2.0) - 2016-10-07
812
### Added
913
- Implemented `getTotalCount()` method to support length aware pagination.

src/PostgresEngine.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,18 @@ protected function performUpdate(Model $model)
9999
*/
100100
protected function toVector(Model $model)
101101
{
102-
$fields = collect($model->toSearchableArray());
103-
104-
$select = $fields->map(function ($value) {
105-
return $value === null ? '' : $value;
106-
})->map(function ($_, $key) use ($model) {
107-
if ($label = $this->rankFieldWeightLabel($model, $key)) {
108-
return "setweight(to_tsvector(?), '$label')";
109-
}
110-
111-
return 'to_tsvector(?)';
102+
$fields = collect($model->toSearchableArray())
103+
->map(function ($value) {
104+
return $value === null ? '' : $value;
105+
});
106+
107+
$select = $fields->keys()
108+
->map(function ($key) use ($model) {
109+
$vector = "to_tsvector(?)";
110+
if ($label = $this->rankFieldWeightLabel($model, $key)) {
111+
$vector = "setweight($vector, '$label')";
112+
}
113+
return $vector;
112114
})->implode(' || ');
113115

114116
return $this->database
@@ -322,7 +324,8 @@ protected function rankFieldWeightLabel(Model $model, $field)
322324
{
323325
$label = $this->option($model, "rank.fields.$field");
324326

325-
return collect(['A', 'B', 'C', 'D'])->contains($label) ? $label : '';
327+
return collect(['A', 'B', 'C', 'D'])
328+
->contains($label) ? $label : '';
326329
}
327330

328331
/**

tests/PostgresEngineTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function test_update_adds_object_to_index()
2626
$db->shouldReceive('query')
2727
->andReturn($query = Mockery::mock('stdClass'));
2828
$query->shouldReceive('selectRaw')
29-
->with('to_tsvector(?) AS tsvector', ['Foo'])
29+
->with("to_tsvector(?) || setweight(to_tsvector(?), 'B') AS tsvector", ['Foo', ''])
3030
->andReturnSelf();
3131
$query->shouldReceive('value')
3232
->with('tsvector')
@@ -154,7 +154,13 @@ class TestModel extends Model
154154

155155
public $text = 'Foo';
156156

157-
protected $searchableOptions = [];
157+
protected $searchableOptions = [
158+
'rank' => [
159+
'fields' => [
160+
'nullable' => 'B',
161+
],
162+
],
163+
];
158164

159165
protected $searchableAdditionalArray = [];
160166

@@ -180,7 +186,10 @@ public function getTable()
180186

181187
public function toSearchableArray()
182188
{
183-
return ['text' => $this->text];
189+
return [
190+
'text' => $this->text,
191+
'nullable' => null,
192+
];
184193
}
185194

186195
public function searchableOptions()

0 commit comments

Comments
 (0)