Skip to content

Commit 89ebd8e

Browse files
committed
added handling for not having search service
1 parent bcf756c commit 89ebd8e

File tree

2 files changed

+116
-3
lines changed

2 files changed

+116
-3
lines changed

app/Models/Hotel.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Couchbase\ConjunctionSearchQuery;
99
use Couchbase\SearchOptions;
1010
use Couchbase\TermSearchQuery;
11+
use Couchbase\Exception\CouchbaseException;
1112

1213
/**
1314
* @OA\Schema(
@@ -247,12 +248,55 @@ public static function searchByName($name)
247248
return $row['fields'];
248249
}, $result->rows());
249250

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;
250260
} catch (\Exception $e) {
251261
\Log::error($e->getMessage());
252262
throw $e;
253263
}
254264
}
255265

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+
256300
public static function filter($filters, $offset, $limit)
257301
{
258302
try {
@@ -280,9 +324,68 @@ public static function filter($filters, $offset, $limit)
280324
return $row['fields'];
281325
}, $result->rows());
282326

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;
283336
} catch (\Exception $e) {
284337
\Log::error($e->getMessage());
285338
throw $e;
286339
}
287340
}
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+
}
288391
}

app/Providers/CouchbaseServiceProvider.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,19 @@ private function createSearchIndex()
182182

183183
\Log::info("Upserting index: " . json_encode($indexData));
184184

185-
$searchIndexManager->upsertIndex($index);
186-
187-
\Log::info("Hotel Search index created or updated successfully.");
185+
try {
186+
$searchIndexManager->upsertIndex($index);
187+
\Log::info("Hotel Search index created or updated successfully.");
188+
} catch (CouchbaseException $e) {
189+
if ($e->getCode() === 4) {
190+
// Service not available - likely means search service is not enabled
191+
\Log::warning("Search service is not available on this cluster. Hotel search functionality will be limited.", [
192+
'error' => $e->getMessage()
193+
]);
194+
} else {
195+
throw $e;
196+
}
197+
}
188198
} catch (CouchbaseException $e) {
189199
if ($e->getCode() === 18) {
190200
\Log::warning("Search index already exists.");

0 commit comments

Comments
 (0)