Skip to content

Commit

Permalink
Merge pull request #2 from 4nd/master
Browse files Browse the repository at this point in the history
Add support for Laravel Scout 'where' clauses
  • Loading branch information
bytexr authored Jun 3, 2024
2 parents 2bf1df1 + 4f6f535 commit 5fa375b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
23 changes: 22 additions & 1 deletion src/Engines/OpenSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ protected function performSearch(Builder $builder, array $options = [])
);
}

$filters = $this->filters($builder);

return $this->openSearch->search(
$index,
$builder->query,
$options
array_merge_recursive($options, $filters),
);
}

Expand Down Expand Up @@ -177,6 +179,25 @@ public function lazyMap(Builder $builder, $results, $model)
})->values();
}

protected function filters(Builder $builder): array
{
if(empty($builder->wheres)) {
return [];
}

return [
'body' => [
'query' => [
'bool' => [
'filter' => [
['term' => $builder->wheres]
]
]
]
]
];
}

public function getTotalCount($results)
{
return $results['hits']['total']['value'];
Expand Down
22 changes: 14 additions & 8 deletions src/Services/OpenSearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,23 @@ public function bulkDelete(string $index, Collection $keys): callable|array

public function search(string $index, string $query, array $options = [])
{
return $this->client->search(array_merge([
$params = array_merge_recursive([
'index' => $index,
'body' => [
'size' => 1000,
'body' => [
'size' => 1000,
'query' => [
'simple_query_string' => [
'query' => "$query* | \"$query\"",
'default_operator' => 'and'
],
'bool' => [
'must' => [
'simple_query_string' => [
'query' => "$query* | \"$query\"",
'default_operator' => 'and'
],
]
]
],
],
], $options));
], $options);

return $this->client->search($params);
}
}
12 changes: 10 additions & 2 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(new \OpenSearch\ClientBuilder())
->setHosts(["https://127.0.0.1:9200"])
->setSSLVerification(false)
->setBasicAuthentication("admin", "admin")
->setBasicAuthentication("admin", "oDLx8Bfs4G86")
->build()
);
$this->engine = new OpenSearchEngine($this->openSearch);
Expand Down Expand Up @@ -90,4 +90,12 @@
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "456"));

expect($count)->toBe(1);
});
});

it('can search with a scout where clause', function () {
$builder = new \Laravel\Scout\Builder(stdClass::class, 'Isle');
$builder->index = $this->index;
$builder->where('rating', 5);
$results = $this->engine->search($builder);
expect($results['hits']['total']['value'])->toBe(2);
});
12 changes: 6 additions & 6 deletions tests/data/documents.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{ "objectID": 1, "name": "Greenwood Paper Factory", "address_line_1": "123 Shore road", "address_postcode": "AX1 BT2", "email": "[email protected]" },
{ "objectID": 2, "name": "Blackwood Paper Factory", "address_line_1": "456 London street", "address_postcode": "TY8 XC3", "email": "[email protected]" },
{ "objectID": 3, "name": "Isle of Green Company", "address_line_1": "756 Brussels avenue", "address_postcode": "HY7 TJ8", "email": "[email protected]" },
{ "objectID": 4, "name": "Isle of Purple Company", "address_line_1": "7 Purple avenue", "address_postcode": "XV1 KI9", "email": "[email protected]" },
{ "objectID": 5, "name": "True storage solutions", "address_line_1": "Isle of green", "address_postcode": "UI8 GH5", "email": "[email protected]" }
]
{ "objectID": 1, "name": "Greenwood Paper Factory", "address_line_1": "123 Shore road", "address_postcode": "AX1 BT2", "email": "[email protected]", "rating": 1 },
{ "objectID": 2, "name": "Blackwood Paper Factory", "address_line_1": "456 London street", "address_postcode": "TY8 XC3", "email": "[email protected]", "rating": 5},
{ "objectID": 3, "name": "Isle of Green Company", "address_line_1": "756 Brussels avenue", "address_postcode": "HY7 TJ8", "email": "[email protected]", "rating": 5 },
{ "objectID": 4, "name": "Isle of Purple Company", "address_line_1": "7 Purple avenue", "address_postcode": "XV1 KI9", "email": "[email protected]", "rating": 5 },
{ "objectID": 5, "name": "True storage solutions", "address_line_1": "Isle of green", "address_postcode": "UI8 GH5", "email": "[email protected]", "rating": 2 }
]

0 comments on commit 5fa375b

Please sign in to comment.