mirror of https://github.com/kcal-app/kcal.git
Add support for a fallback when `SCOUT_DRIVE` is NULL
This commit is contained in:
parent
c9e578858f
commit
f67ae25b13
|
|
@ -20,7 +20,20 @@ class IngredientPickerController extends Controller
|
||||||
$results = new Collection();
|
$results = new Collection();
|
||||||
$term = $request->query->get('term');
|
$term = $request->query->get('term');
|
||||||
if (!empty($term)) {
|
if (!empty($term)) {
|
||||||
$results = Food::boolSearch()
|
$results = match (env('SCOUT_DRIVER')) {
|
||||||
|
'elastic' => $this->searchWithElasticSearch($term),
|
||||||
|
default => $this->searchWithDatabaseLike($term),
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
return response()->json($results->values());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search using an ElasticSearch service.
|
||||||
|
*/
|
||||||
|
private function searchWithElasticSearch(string $term): Collection {
|
||||||
|
return Food::boolSearch()
|
||||||
->join(Recipe::class)
|
->join(Recipe::class)
|
||||||
|
|
||||||
// Attempt to match exact phrase first.
|
// Attempt to match exact phrase first.
|
||||||
|
|
@ -42,6 +55,19 @@ class IngredientPickerController extends Controller
|
||||||
->execute()
|
->execute()
|
||||||
->models();
|
->models();
|
||||||
}
|
}
|
||||||
return response()->json($results->values());
|
|
||||||
|
/**
|
||||||
|
* Search using basic database WHERE ... LIKE queries.
|
||||||
|
*/
|
||||||
|
private function searchWithDatabaseLike(string $term): Collection {
|
||||||
|
$foods = Food::query()->where('foods.name', 'like', "%{$term}%")
|
||||||
|
->orWhere('foods.detail', 'like', "%{$term}%")
|
||||||
|
->orWhere('foods.brand', 'like', "%{$term}%")
|
||||||
|
->get();
|
||||||
|
$recipes = Recipe::query()->where('recipes.name', 'like', "%{$term}%")
|
||||||
|
->orWhere('recipes.description', 'like', "%{$term}%")
|
||||||
|
->orWhere('recipes.source', 'like', "%{$term}%")
|
||||||
|
->get();
|
||||||
|
return $foods->merge($recipes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue