Skip to content

Commit 273e153

Browse files
committed
added manual creation of hotel index
1 parent 32a83e5 commit 273e153

File tree

6 files changed

+221
-19
lines changed

6 files changed

+221
-19
lines changed

app/Http/Controllers/HotelController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,47 +75,47 @@ public function search(Request $request)
7575
* summary="Filter hotels by various attributes",
7676
* description="Returns a list of hotels that match the provided filters",
7777
* @OA\Parameter(
78-
* name="hotel[name]",
78+
* name="name",
7979
* in="query",
8080
* required=false,
8181
* @OA\Schema(
8282
* type="string"
8383
* )
8484
* ),
8585
* @OA\Parameter(
86-
* name="hotel[title]",
86+
* name="title",
8787
* in="query",
8888
* required=false,
8989
* @OA\Schema(
9090
* type="string"
9191
* )
9292
* ),
9393
* @OA\Parameter(
94-
* name="hotel[description]",
94+
* name="description",
9595
* in="query",
9696
* required=false,
9797
* @OA\Schema(
9898
* type="string"
9999
* )
100100
* ),
101101
* @OA\Parameter(
102-
* name="hotel[country]",
102+
* name="country",
103103
* in="query",
104104
* required=false,
105105
* @OA\Schema(
106106
* type="string"
107107
* )
108108
* ),
109109
* @OA\Parameter(
110-
* name="hotel[city]",
110+
* name="city",
111111
* in="query",
112112
* required=false,
113113
* @OA\Schema(
114114
* type="string"
115115
* )
116116
* ),
117117
* @OA\Parameter(
118-
* name="hotel[state]",
118+
* name="state",
119119
* in="query",
120120
* required=false,
121121
* @OA\Schema(

app/Providers/CouchbaseServiceProvider.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Illuminate\Support\ServiceProvider;
66
use Couchbase\ClusterOptions;
77
use Couchbase\Cluster;
8+
use Couchbase\Management\SearchIndex;
9+
use Couchbase\Exception\CouchbaseException;
10+
use Illuminate\Support\Facades\Storage;
811

912
class CouchbaseServiceProvider extends ServiceProvider
1013
{
@@ -31,7 +34,6 @@ public function register()
3134
return $cluster->bucket($config['bucket']);
3235
});
3336

34-
3537
$this->app->singleton('couchbase.airlineCollection', function ($app) {
3638
$bucket = $app->make('couchbase.bucket');
3739
return $bucket->scope('inventory')->collection('airline');
@@ -60,6 +62,56 @@ public function register()
6062
*/
6163
public function boot()
6264
{
63-
//
65+
$indexFilePath = 'hotel_search_index.json';
66+
67+
try {
68+
// Read the index configuration from the JSON file using Laravel's storage system
69+
if (!Storage::exists($indexFilePath)) {
70+
throw new \Exception("Index file not found at storage/app/{$indexFilePath}");
71+
}
72+
$indexContent = Storage::get($indexFilePath);
73+
$indexData = json_decode($indexContent, true);
74+
75+
// Get the cluster instance
76+
$cluster = $this->app->make('couchbase.cluster');
77+
78+
// Create an instance of the SearchIndexManager
79+
$searchIndexManager = $cluster->searchIndexes();
80+
81+
// Create a new SearchIndex instance
82+
$index = new SearchIndex($indexData['name'], $indexData['sourceName']);
83+
if (isset($indexData['uuid'])) {
84+
$index->setUuid($indexData['uuid']);
85+
}
86+
if (isset($indexData['type'])) {
87+
$index->setType($indexData['type']);
88+
}
89+
if (isset($indexData['params'])) {
90+
$index->setParams($indexData['params']);
91+
}
92+
if (isset($indexData['sourceUUID'])) {
93+
$index->setSourceUuid($indexData['sourceUUID']);
94+
}
95+
if (isset($indexData['sourceType'])) {
96+
$index->setSourceType($indexData['sourceType']);
97+
}
98+
if (isset($indexData['sourceParams'])) {
99+
$index->setSourceParams($indexData['sourceParams']);
100+
}
101+
if (isset($indexData['planParams'])) {
102+
$index->setPlanParams($indexData['planParams']);
103+
}
104+
105+
\Log::info("Upserting index: " . json_encode($indexData));
106+
107+
// Upsert (create or update) the index
108+
$searchIndexManager->upsertIndex($index);
109+
110+
echo "Hotel Search index created or updated successfully.\n";
111+
} catch (CouchbaseException $e) {
112+
\Log::error("Couchbase Exception Occurred: " . $e->getMessage());
113+
} catch (\Exception $e) {
114+
\Log::error("Exception Ocurred: " . $e->getMessage());
115+
}
64116
}
65117
}

storage/api-docs/api-docs.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,47 +617,47 @@
617617
"operationId": "filterHotels",
618618
"parameters": [
619619
{
620-
"name": "hotel[name]",
620+
"name": "name",
621621
"in": "query",
622622
"required": false,
623623
"schema": {
624624
"type": "string"
625625
}
626626
},
627627
{
628-
"name": "hotel[title]",
628+
"name": "title",
629629
"in": "query",
630630
"required": false,
631631
"schema": {
632632
"type": "string"
633633
}
634634
},
635635
{
636-
"name": "hotel[description]",
636+
"name": "description",
637637
"in": "query",
638638
"required": false,
639639
"schema": {
640640
"type": "string"
641641
}
642642
},
643643
{
644-
"name": "hotel[country]",
644+
"name": "country",
645645
"in": "query",
646646
"required": false,
647647
"schema": {
648648
"type": "string"
649649
}
650650
},
651651
{
652-
"name": "hotel[city]",
652+
"name": "city",
653653
"in": "query",
654654
"required": false,
655655
"schema": {
656656
"type": "string"
657657
}
658658
},
659659
{
660-
"name": "hotel[state]",
660+
"name": "state",
661661
"in": "query",
662662
"required": false,
663663
"schema": {

storage/app/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

storage/app/hotel_search_index.json

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"name": "hotel_search",
3+
"type": "fulltext-index",
4+
"sourceType": "gocbcore",
5+
"sourceName": "travel-sample",
6+
"planParams": {
7+
"indexPartitions": 1,
8+
"numReplicas": 0
9+
},
10+
"params": {
11+
"doc_config": {
12+
"docid_prefix_delim": "",
13+
"docid_regexp": "",
14+
"mode": "scope.collection.type_field",
15+
"type_field": "type"
16+
},
17+
"mapping": {
18+
"analysis": {
19+
"analyzers": {
20+
"edge_ngram": {
21+
"token_filters": [
22+
"to_lower",
23+
"edge_ngram_2_8"
24+
],
25+
"tokenizer": "unicode",
26+
"type": "custom"
27+
}
28+
},
29+
"token_filters": {
30+
"edge_ngram_2_8": {
31+
"back": false,
32+
"max": 8,
33+
"min": 2,
34+
"type": "edge_ngram"
35+
}
36+
}
37+
},
38+
"default_analyzer": "standard",
39+
"default_datetime_parser": "dateTimeOptional",
40+
"index_dynamic": false,
41+
"store_dynamic": false,
42+
"default_mapping": {
43+
"dynamic": true,
44+
"enabled": false
45+
},
46+
"types": {
47+
"inventory.hotel": {
48+
"dynamic": false,
49+
"enabled": true,
50+
"properties": {
51+
"title": {
52+
"enabled": true,
53+
"fields": [
54+
{
55+
"docvalues": true,
56+
"include_in_all": false,
57+
"include_term_vectors": false,
58+
"index": true,
59+
"name": "title",
60+
"store": true,
61+
"type": "text"
62+
}
63+
]
64+
},
65+
"name": {
66+
"enabled": true,
67+
"fields": [
68+
{
69+
"docvalues": true,
70+
"include_in_all": false,
71+
"include_term_vectors": false,
72+
"index": true,
73+
"name": "name",
74+
"store": true,
75+
"type": "text",
76+
"analyzer": "edge_ngram"
77+
},
78+
{
79+
"docvalues": true,
80+
"include_in_all": false,
81+
"include_term_vectors": false,
82+
"index": true,
83+
"name": "name_keyword",
84+
"store": false,
85+
"type": "text",
86+
"analyzer": "keyword"
87+
}
88+
]
89+
},
90+
"description": {
91+
"enabled": true,
92+
"fields": [
93+
{
94+
"docvalues": true,
95+
"include_in_all": false,
96+
"include_term_vectors": false,
97+
"index": true,
98+
"name": "description",
99+
"store": true,
100+
"type": "text",
101+
"analyzer": "en"
102+
}
103+
]
104+
},
105+
"city": {
106+
"enabled": true,
107+
"fields": [
108+
{
109+
"docvalues": true,
110+
"include_in_all": false,
111+
"include_term_vectors": false,
112+
"index": true,
113+
"name": "city",
114+
"store": true,
115+
"type": "text",
116+
"analyzer": "keyword"
117+
}
118+
]
119+
},
120+
"state": {
121+
"enabled": true,
122+
"fields": [
123+
{
124+
"docvalues": true,
125+
"include_in_all": false,
126+
"include_term_vectors": false,
127+
"index": true,
128+
"name": "state",
129+
"store": true,
130+
"type": "text",
131+
"analyzer": "keyword"
132+
}
133+
]
134+
},
135+
"country": {
136+
"enabled": true,
137+
"fields": [
138+
{
139+
"docvalues": true,
140+
"include_in_all": false,
141+
"include_term_vectors": false,
142+
"index": true,
143+
"name": "country",
144+
"store": true,
145+
"type": "text",
146+
"analyzer": "keyword"
147+
}
148+
]
149+
}
150+
}
151+
}
152+
}
153+
}
154+
}
155+
}

storage/app/public/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)