|
8 | 8 | use Couchbase\ConjunctionSearchQuery; |
9 | 9 | use Couchbase\SearchOptions; |
10 | 10 | use Couchbase\TermSearchQuery; |
| 11 | +use Couchbase\Exception\CouchbaseException; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * @OA\Schema( |
@@ -247,12 +248,55 @@ public static function searchByName($name) |
247 | 248 | return $row['fields']; |
248 | 249 | }, $result->rows()); |
249 | 250 |
|
| 251 | + } catch (CouchbaseException $e) { |
| 252 | + if ($e->getCode() === 4) { |
| 253 | + // Search service not available, fallback to basic key-value query |
| 254 | + \Log::warning("Search service not available, falling back to basic query", [ |
| 255 | + 'error' => $e->getMessage() |
| 256 | + ]); |
| 257 | + return self::fallbackSearchByName($name); |
| 258 | + } |
| 259 | + throw $e; |
250 | 260 | } catch (\Exception $e) { |
251 | 261 | \Log::error($e->getMessage()); |
252 | 262 | throw $e; |
253 | 263 | } |
254 | 264 | } |
255 | 265 |
|
| 266 | + protected static function fallbackSearchByName($name) |
| 267 | + { |
| 268 | + try { |
| 269 | + $instance = new static; |
| 270 | + $options = new \Couchbase\QueryOptions(); |
| 271 | + |
| 272 | + // Use N1QL query as fallback |
| 273 | + $query = "SELECT name, title, description, country, city, state |
| 274 | + FROM `" . config('couchbase.bucket') . "`.inventory.hotel |
| 275 | + WHERE LOWER(name) LIKE $1 |
| 276 | + OR LOWER(title) LIKE $1"; |
| 277 | + |
| 278 | + $searchPattern = '%' . strtolower($name) . '%'; |
| 279 | + $result = $instance->cluster->query($query, $options->positionalParameters([$searchPattern])); |
| 280 | + |
| 281 | + return array_map(function ($row) { |
| 282 | + return [ |
| 283 | + 'name' => $row['name'], |
| 284 | + 'title' => $row['title'], |
| 285 | + 'description' => $row['description'], |
| 286 | + 'country' => $row['country'], |
| 287 | + 'city' => $row['city'], |
| 288 | + 'state' => $row['state'] |
| 289 | + ]; |
| 290 | + }, $result->rows()); |
| 291 | + |
| 292 | + } catch (\Exception $e) { |
| 293 | + \Log::error("Fallback search failed", [ |
| 294 | + 'error' => $e->getMessage() |
| 295 | + ]); |
| 296 | + throw $e; |
| 297 | + } |
| 298 | + } |
| 299 | + |
256 | 300 | public static function filter($filters, $offset, $limit) |
257 | 301 | { |
258 | 302 | try { |
@@ -280,9 +324,68 @@ public static function filter($filters, $offset, $limit) |
280 | 324 | return $row['fields']; |
281 | 325 | }, $result->rows()); |
282 | 326 |
|
| 327 | + } catch (CouchbaseException $e) { |
| 328 | + if ($e->getCode() === 4) { |
| 329 | + // Search service not available, fallback to basic key-value query |
| 330 | + \Log::warning("Search service not available, falling back to basic query", [ |
| 331 | + 'error' => $e->getMessage() |
| 332 | + ]); |
| 333 | + return self::fallbackFilter($filters, $offset, $limit); |
| 334 | + } |
| 335 | + throw $e; |
283 | 336 | } catch (\Exception $e) { |
284 | 337 | \Log::error($e->getMessage()); |
285 | 338 | throw $e; |
286 | 339 | } |
287 | 340 | } |
| 341 | + |
| 342 | + protected static function fallbackFilter($filters, $offset, $limit) |
| 343 | + { |
| 344 | + try { |
| 345 | + $instance = new static; |
| 346 | + $options = new \Couchbase\QueryOptions(); |
| 347 | + |
| 348 | + // Build WHERE clause dynamically |
| 349 | + $whereConditions = []; |
| 350 | + $params = []; |
| 351 | + $paramIndex = 1; |
| 352 | + |
| 353 | + foreach ($filters as $field => $value) { |
| 354 | + if ($value) { |
| 355 | + $whereConditions[] = "LOWER($field) = $$paramIndex"; |
| 356 | + $params[] = strtolower($value); |
| 357 | + $paramIndex++; |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + $whereClause = count($whereConditions) > 0 |
| 362 | + ? 'WHERE ' . implode(' AND ', $whereConditions) |
| 363 | + : ''; |
| 364 | + |
| 365 | + $query = "SELECT name, title, description, country, city, state |
| 366 | + FROM `" . config('couchbase.bucket') . "`.inventory.hotel |
| 367 | + $whereClause |
| 368 | + LIMIT $limit |
| 369 | + OFFSET $offset"; |
| 370 | + |
| 371 | + $result = $instance->cluster->query($query, $options->positionalParameters($params)); |
| 372 | + |
| 373 | + return array_map(function ($row) { |
| 374 | + return [ |
| 375 | + 'name' => $row['name'], |
| 376 | + 'title' => $row['title'], |
| 377 | + 'description' => $row['description'], |
| 378 | + 'country' => $row['country'], |
| 379 | + 'city' => $row['city'], |
| 380 | + 'state' => $row['state'] |
| 381 | + ]; |
| 382 | + }, $result->rows()); |
| 383 | + |
| 384 | + } catch (\Exception $e) { |
| 385 | + \Log::error("Fallback filter failed", [ |
| 386 | + 'error' => $e->getMessage() |
| 387 | + ]); |
| 388 | + throw $e; |
| 389 | + } |
| 390 | + } |
288 | 391 | } |
0 commit comments