Skip to content

Commit 1494ce9

Browse files
authored
PCBC-970 SDK Support for Scoped Search Indexes (#147)
1 parent 410bd8f commit 1494ce9

14 files changed

+1250
-19
lines changed

Couchbase/Cluster.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ public function searchQuery(string $indexName, SearchQuery $query, SearchOptions
170170
return new SearchResult($result);
171171
}
172172

173+
/**
174+
* Executes a search query against the full text search services.
175+
*
176+
* This can be used to perform a traditional FTS query, and/or a vector search.
177+
*
178+
* @param string $indexName the cluster-level FTS index to use for the search request
179+
* @param SearchRequest $request The search request to run
180+
* @param SearchOptions|null $options The options to use when executing the search request
181+
*
182+
* @return SearchResult
183+
* @throws InvalidArgumentException
184+
* @since 4.1.7
185+
*
186+
* @VOLATILE: This API is subject to change at any time.
187+
*/
173188
public function search(string $indexName, SearchRequest $request, SearchOptions $options = null): SearchResult
174189
{
175190
$exportedRequest = SearchRequest::export($request);
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014-Present Couchbase, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
declare(strict_types=1);
20+
21+
namespace Couchbase\Management;
22+
23+
use Couchbase\Extension;
24+
25+
class ScopeSearchIndexManager implements ScopeSearchIndexManagerInterface
26+
{
27+
private $core;
28+
private string $bucketName;
29+
private string $scopeName;
30+
31+
public function __construct($core, string $bucketName, string $scopeName)
32+
{
33+
$this->core = $core;
34+
$this->bucketName = $bucketName;
35+
$this->scopeName = $scopeName;
36+
}
37+
38+
/**
39+
* Fetches scope-level index with the specified name from the server
40+
*
41+
* @param string $indexName
42+
* @param GetSearchIndexOptions|null $options
43+
* @return SearchIndex
44+
*
45+
* @since 4.1.7
46+
*
47+
* @VOLATILE: This API is subject to change at any time.
48+
*/
49+
public function getIndex(string $indexName, GetSearchIndexOptions $options = null): SearchIndex
50+
{
51+
$result = Extension\scopeSearchIndexGet($this->core, $this->bucketName, $this->scopeName, $indexName, GetSearchIndexOptions::export($options));
52+
53+
return SearchIndex::import($result);
54+
}
55+
56+
/**
57+
* Fetches all scope-level indexes from the server
58+
*
59+
* @param GetAllSearchIndexesOptions|null $options
60+
* @return array
61+
*
62+
* @since 4.1.7
63+
*
64+
* @VOLATILE: This API is subject to change at any time.
65+
*/
66+
public function getAllIndexes(GetAllSearchIndexesOptions $options = null): array
67+
{
68+
$result = Extension\scopeSearchIndexGetAll($this->core, $this->bucketName, $this->scopeName, GetAllSearchIndexesOptions::export($options));
69+
$indexes = [];
70+
foreach ($result as $index) {
71+
$indexes[] = SearchIndex::import($index);
72+
}
73+
return $indexes;
74+
}
75+
76+
/**
77+
* Upserts scope-level index to the server
78+
*
79+
* @param SearchIndex $indexDefinition
80+
* @param UpsertSearchIndexOptions|null $options
81+
*
82+
* @since 4.1.7
83+
*
84+
* @VOLATILE: This API is subject to change at any time.
85+
*/
86+
public function upsertIndex(SearchIndex $indexDefinition, UpsertSearchIndexOptions $options = null)
87+
{
88+
Extension\scopeSearchIndexUpsert($this->core, $this->bucketName, $this->scopeName, SearchIndex::export($indexDefinition), UpsertSearchIndexOptions::export($options));
89+
}
90+
91+
/**
92+
* Drops a scope-level index from the server
93+
*
94+
* @param string $name
95+
* @param DropSearchIndexOptions|null $options
96+
*
97+
* @since 4.1.7
98+
*
99+
* @VOLATILE: This API is subject to change at any time.
100+
*/
101+
public function dropIndex(string $name, DropSearchIndexOptions $options = null)
102+
{
103+
Extension\scopeSearchIndexDrop($this->core, $this->bucketName, $this->scopeName, $name, DropSearchIndexOptions::export($options));
104+
}
105+
106+
/**
107+
* Retrieves the number of documents that have been indexed for the scope-level index
108+
*
109+
* @param string $indexName
110+
* @param GetIndexedSearchIndexOptions|null $options
111+
*
112+
* @return int
113+
* @since 4.1.7
114+
*
115+
* @VOLATILE: This API is subject to change at any time.
116+
*/
117+
public function getIndexedDocumentsCount(string $indexName, GetIndexedSearchIndexOptions $options = null): int
118+
{
119+
$result = Extension\scopeSearchIndexGetDocumentsCount($this->core, $this->bucketName, $this->scopeName, $indexName, GetIndexedSearchIndexOptions::export($options));
120+
return $result['count'];
121+
}
122+
123+
/**
124+
* Pauses updates and maintenance for the scope-level index
125+
*
126+
* @param string $indexName
127+
* @param PauseIngestSearchIndexOptions|null $options
128+
*
129+
* @since 4.1.7
130+
*
131+
* @VOLATILE: This API is subject to change at any time.
132+
*/
133+
public function pauseIngest(string $indexName, PauseIngestSearchIndexOptions $options = null)
134+
{
135+
Extension\scopeSearchIndexIngestPause($this->core, $this->bucketName, $this->scopeName, $indexName, PauseIngestSearchIndexOptions::export($options));
136+
}
137+
138+
/**
139+
* Resumes updates and maintenance for the scope-level index
140+
*
141+
* @param string $indexName
142+
* @param ResumeIngestSearchIndexOptions|null $options
143+
*
144+
* @since 4.1.7
145+
*
146+
* @VOLATILE: This API is subject to change at any time.
147+
*/
148+
public function resumeIngest(string $indexName, ResumeIngestSearchIndexOptions $options = null)
149+
{
150+
Extension\scopeSearchIndexIngestResume($this->core, $this->bucketName, $this->scopeName, $indexName, ResumeIngestSearchIndexOptions::export($options));
151+
}
152+
153+
/**
154+
* Allows querying against the scope-level index
155+
*
156+
* @param string $indexName
157+
* @param AllowQueryingSearchIndexOptions|null $options
158+
*
159+
* @since 4.1.7
160+
*
161+
* @VOLATILE: This API is subject to change at any time.
162+
*/
163+
public function allowQuerying(string $indexName, AllowQueryingSearchIndexOptions $options = null)
164+
{
165+
Extension\scopeSearchIndexQueryingAllow($this->core, $this->bucketName, $this->scopeName, $indexName, AllowQueryingSearchIndexOptions::export($options));
166+
}
167+
168+
/**
169+
* Disallows querying against the scope-level index
170+
*
171+
* @param string $indexName
172+
* @param DisallowQueryingSearchIndexOptions|null $options
173+
*
174+
* @since 4.1.7
175+
*
176+
* @VOLATILE: This API is subject to change at any time.
177+
*/
178+
public function disallowQuerying(string $indexName, DisallowQueryingSearchIndexOptions $options = null)
179+
{
180+
Extension\scopeSearchIndexQueryingDisallow($this->core, $this->bucketName, $this->scopeName, $indexName, DisallowQueryingSearchIndexOptions::export($options));
181+
}
182+
183+
/**
184+
* Freezes the assigment of scope-level index partitions to nodes
185+
*
186+
* @param string $indexName
187+
* @param FreezePlanSearchIndexOptions|null $options
188+
*
189+
* @since 4.1.7
190+
*
191+
* @VOLATILE: This API is subject to change at any time.
192+
*/
193+
public function freezePlan(string $indexName, FreezePlanSearchIndexOptions $options = null)
194+
{
195+
Extension\scopeSearchIndexPlanFreeze($this->core, $this->bucketName, $this->scopeName, $indexName, FreezePlanSearchIndexOptions::export($options));
196+
}
197+
198+
/**
199+
* Unfreezes the assignment of index partitions to nodes
200+
*
201+
* @param string $indexName
202+
* @param UnfreezePlanSearchIndexOptions|null $options
203+
*
204+
* @since 4.1.7
205+
*
206+
* @VOLATILE: This API is subject to change at any time.
207+
*/
208+
public function unfreezePlan(string $indexName, UnfreezePlanSearchIndexOptions $options = null)
209+
{
210+
Extension\scopeSearchIndexPlanUnfreeze($this->core, $this->bucketName, $this->scopeName, $indexName, UnfreezePlanSearchIndexOptions::export($options));
211+
}
212+
213+
/**
214+
* Fetches the analysis of a document against a specific scope-level index
215+
*
216+
* @param string $indexName
217+
* @param $document
218+
* @param AnalyzeDocumentOptions|null $options
219+
*
220+
* @return array
221+
* @since 4.1.7
222+
*
223+
* @VOLATILE: This API is subject to change at any time.
224+
*/
225+
public function analyzeDocument(string $indexName, $document, AnalyzeDocumentOptions $options = null): array
226+
{
227+
$result = Extension\scopeSearchIndexDocumentAnalyze($this->core, $this->bucketName, $this->scopeName, $indexName, json_encode($document), AnalyzeDocumentOptions::export($options));
228+
return json_decode($result["analysis"], true);
229+
}
230+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Couchbase\Management;
4+
5+
interface ScopeSearchIndexManagerInterface
6+
{
7+
public function getIndex(string $indexName, GetSearchIndexOptions $options = null): SearchIndex;
8+
9+
public function getAllIndexes(GetAllSearchIndexesOptions $options = null): array;
10+
11+
public function upsertIndex(SearchIndex $indexDefinition, UpsertSearchIndexOptions $options = null);
12+
13+
public function dropIndex(string $name, DropSearchIndexOptions $options = null);
14+
15+
public function getIndexedDocumentsCount(string $indexName, GetIndexedSearchIndexOptions $options = null): int;
16+
17+
public function pauseIngest(string $indexName, PauseIngestSearchIndexOptions $options = null);
18+
19+
public function resumeIngest(string $indexName, ResumeIngestSearchIndexOptions $options = null);
20+
21+
public function allowQuerying(string $indexName, AllowQueryingSearchIndexOptions $options = null);
22+
23+
public function disallowQuerying(string $indexName, DisallowQueryingSearchIndexOptions $options = null);
24+
25+
public function freezePlan(string $indexName, FreezePlanSearchIndexOptions $options = null);
26+
27+
public function unfreezePlan(string $indexName, UnfreezePlanSearchIndexOptions $options = null);
28+
29+
public function analyzeDocument(string $indexName, $document, AnalyzeDocumentOptions $options = null): array;
30+
}

Couchbase/Scope.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
namespace Couchbase;
2222

2323
use Couchbase\Exception\CouchbaseException;
24+
use Couchbase\Exception\InvalidArgumentException;
2425
use Couchbase\Exception\TimeoutException;
26+
use Couchbase\Management\ScopeSearchIndexManager;
27+
use Couchbase\Management\ScopeSearchIndexManagerInterface;
2528

2629
/**
2730
* Scope is an object for providing access to collections.
@@ -110,4 +113,54 @@ public function analyticsQuery(string $statement, AnalyticsOptions $options = nu
110113

111114
return new AnalyticsResult($result, AnalyticsOptions::getTranscoder($options));
112115
}
116+
117+
/**
118+
* Executes a search query against the full text search services.
119+
*
120+
* This can be used to perform a traditional FTS query, and/or a vector search.
121+
*
122+
* @param string $indexName the scope-level fts index to use for the search request
123+
* @param SearchRequest $request The search request to run
124+
* @param SearchOptions|null $options The options to use when executing the search request
125+
*
126+
* @return SearchResult
127+
* @throws InvalidArgumentException
128+
* @since 4.1.7
129+
*
130+
* @VOLATILE: This API is subject to change at any time.
131+
*/
132+
public function search(string $indexName, SearchRequest $request, SearchOptions $options = null): SearchResult
133+
{
134+
$exportedRequest = SearchRequest::export($request);
135+
$exportedOptions = SearchOptions::export($options);
136+
137+
$exportedOptions['bucketName'] = $this->bucketName;
138+
$exportedOptions['scopeName'] = $this->name;
139+
140+
$exportedOptions["showRequest"] = false;
141+
$query = $exportedRequest['searchQuery'];
142+
143+
if (!$exportedRequest['vectorSearch']) {
144+
$result = Extension\searchQuery($this->core, $indexName, json_encode($query), $exportedOptions);
145+
return new SearchResult($result);
146+
}
147+
148+
$vectorSearch = $exportedRequest['vectorSearch'];
149+
$result = Extension\vectorSearch($this->core, $indexName, json_encode($query), json_encode($vectorSearch), $exportedOptions, VectorSearchOptions::export($vectorSearch->options()));
150+
return new SearchResult($result);
151+
}
152+
153+
/**
154+
* Provides access to search index management services at the scope level
155+
*
156+
* @return ScopeSearchIndexManagerInterface
157+
*
158+
* @since 4.1.7
159+
*
160+
* @VOLATILE: This API is subject to change at any time.
161+
*/
162+
public function searchIndexes(): ScopeSearchIndexManagerInterface
163+
{
164+
return new ScopeSearchIndexManager($this->core, $this->bucketName, $this->name);
165+
}
113166
}

Couchbase/ScopeInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace Couchbase;
2222

23+
use Couchbase\Management\ScopeSearchIndexManagerInterface;
24+
2325
/**
2426
* Scope is an object for providing access to collections.
2527
*/
@@ -31,5 +33,9 @@ public function collection(string $name): CollectionInterface;
3133

3234
public function query(string $statement, QueryOptions $options = null): QueryResult;
3335

36+
public function search(string $indexName, SearchRequest $request, SearchOptions $options = null): SearchResult;
37+
3438
public function analyticsQuery(string $statement, AnalyticsOptions $options = null): AnalyticsResult;
39+
40+
public function searchIndexes(): ScopeSearchIndexManagerInterface;
3541
}

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@
208208
<file role="php" name="RoleAndDescription.php"/>
209209
<file role="php" name="RoleAndOrigin.php"/>
210210
<file role="php" name="S3ExternalAnalyticsLink.php"/>
211+
<file role="php" name="ScopeSearchIndexManager.php"/>
212+
<file role="php" name="ScopeSearchIndexManagerInterface.php"/>
211213
<file role="php" name="ScopeSpec.php"/>
212214
<file role="php" name="SearchIndex.php"/>
213215
<file role="php" name="SearchIndexManager.php"/>
@@ -1264,6 +1266,8 @@
12641266
<file role="src" name="encoded_value.hxx"/>
12651267
<file role="src" name="json_transcoder.hxx"/>
12661268
<file role="src" name="raw_binary_transcoder.hxx"/>
1269+
<file role="src" name="raw_json_transcoder.hxx"/>
1270+
<file role="src" name="raw_string_transcoder.hxx"/>
12671271
<file role="src" name="serializer_traits.hxx"/>
12681272
<file role="src" name="tao_json_serializer.hxx"/>
12691273
<file role="src" name="transcoder_traits.hxx"/>
@@ -1500,6 +1504,7 @@
15001504
<file role="src" name="retry_request.hxx"/>
15011505
<file role="src" name="retry_strategy.hxx"/>
15021506
<file role="src" name="scope.hxx"/>
1507+
<file role="src" name="scope_search_index_manager.hxx"/>
15031508
<file role="src" name="search_date_range.hxx"/>
15041509
<file role="src" name="search_error_context.hxx"/>
15051510
<file role="src" name="search_facet.hxx"/>

0 commit comments

Comments
 (0)