-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add FULL TEXT SEARCH support #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9289bff
bff2c79
4e91eb4
b086dbc
cf3e10f
161b62c
99f3e4f
d53a995
8a0daaf
4898b5d
5a2a245
d20ae5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2019 Colopl Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Colopl\Spanner\Query\Concerns; | ||
|
|
||
| trait UsesFullTextSearch | ||
| { | ||
| /** | ||
| * @param string $tokens | ||
| * @param string $query | ||
| * @param array<string, scalar> $options | ||
| * @param string $boolean | ||
| * @return $this | ||
| */ | ||
| public function searchFullText( | ||
| string $tokens, | ||
| string $query, | ||
| array $options = [], | ||
| string $boolean = 'and', | ||
| ): static | ||
| { | ||
| $this->addSearchCondition('SearchFullText', $tokens, $query, $options, $boolean); | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $tokens | ||
| * @param string $query | ||
| * @param array<string, scalar> $options | ||
| * @param string $boolean | ||
| * @return $this | ||
| */ | ||
| public function searchNgrams( | ||
| string $tokens, | ||
| string $query, | ||
| array $options = [], | ||
| string $boolean = 'and', | ||
| ): static | ||
| { | ||
| $this->addSearchCondition('SearchNgrams', $tokens, $query, $options, $boolean); | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $tokens | ||
| * @param string $query | ||
| * @param array<string, scalar> $options | ||
| * @param string $boolean | ||
| * @return $this | ||
| */ | ||
| public function searchSubstring( | ||
| string $tokens, | ||
| string $query, | ||
| array $options = [], | ||
| string $boolean = 'and', | ||
| ): static | ||
| { | ||
| $this->addSearchCondition('SearchSubstring', $tokens, $query, $options, $boolean); | ||
| return $this; | ||
| } | ||
taka-oyama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @param string $type | ||
| * @param string $tokens | ||
| * @param string $query | ||
| * @param array<string, scalar> $options | ||
| * @param string $boolean | ||
| * @return void | ||
| */ | ||
| protected function addSearchCondition( | ||
| string $type, | ||
| string $tokens, | ||
| string $query, | ||
| array $options = [], | ||
| string $boolean = 'and', | ||
| ): void | ||
| { | ||
| $this->wheres[] = [ | ||
| 'type' => $type, | ||
| 'tokens' => $tokens, | ||
| 'boolean' => $boolean, | ||
| 'query' => $query, | ||
| 'options' => $options, | ||
| ]; | ||
| $this->addBinding($query); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,22 @@ public function timestampArray($column) | |
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $column | ||
| * @param TokenizerFunction $function | ||
| * @param string $target | ||
| * @param array $options | ||
| * @return ColumnDefinition | ||
| */ | ||
| public function tokenList(string $column, TokenizerFunction $function, string $target, array $options = []): ColumnDefinition | ||
| { | ||
| return $this->addColumn('tokenList', $column, [ | ||
| 'function' => $function, | ||
| 'target' => $target, | ||
| 'options' => $options, | ||
| ])->invisible()->nullable(); | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated use interleaveInParent instead. | ||
| * @param string $parentTableName | ||
|
|
@@ -269,6 +285,27 @@ public function interleaveInParent(string $table): InterleaveDefinition | |
| return $command; | ||
| } | ||
|
|
||
| /** | ||
| * @param string|list<string> $columns | ||
| * @param string|null $name | ||
| * @param string|null $algorithm | ||
| * @return SearchIndexDefinition | ||
| */ | ||
| public function fullText($columns, $name = null, $algorithm = null) | ||
| { | ||
| $type = 'fullText'; | ||
| $columns = (array) $columns; | ||
|
|
||
| $this->commands[] = $command = new SearchIndexDefinition([ | ||
| 'name' => $type, | ||
| 'index' => $name ?? $this->createIndexName($type, $columns), | ||
| 'columns' => $columns, | ||
| 'algorithm' => $algorithm, | ||
| ]); | ||
|
|
||
| return $command; | ||
| } | ||
|
Comment on lines
+288
to
+307
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add support for missing features mentioned in PR objectives. The current implementation doesn't support the following features mentioned in the PR objectives:
Consider extending the public function fullText($columns, $name = null, $algorithm = null)
{
$type = 'fullText';
$columns = (array) $columns;
$this->commands[] = $command = new SearchIndexDefinition([
'name' => $type,
'index' => $name ?? $this->createIndexName($type, $columns),
'columns' => $columns,
'algorithm' => $algorithm,
+ 'partition_by' => null, // Add support for PARTITION BY
+ 'order_by' => null, // Add support for ORDER BY
+ 'options' => [], // Add support for OPTIONS(sort_order_sharding)
]);
return $command;
}Example usage after implementing these features: $table->fullText('description')
->partitionBy(['user_id'])
->orderBy(['created_at'])
->options(['sort_order_sharding' => true]); |
||
|
|
||
| /** | ||
| * @see https://cloud.google.com/spanner/docs/ttl#defining_a_row_deletion_policy | ||
| * @param string $column | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Copyright 2019 Colopl Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Colopl\Spanner\Schema; | ||
|
|
||
| /** | ||
| * @property string $name | ||
| * @property string $index | ||
| * @property list<string> $columns | ||
| * @property string|list<string> $partitionBy | ||
| * @property list<string>|array<string, string> $orderBy | ||
| * @property bool|null $sortOrderSharding | ||
| * @property bool|null $disableAutomaticUidColumn | ||
| * @method $this partitionBy(string|string[] $columns) | ||
| * @method $this orderBy(string|string[] $columns) | ||
| * @method $this sortOrderSharding(bool $toggle = true) | ||
| * @method $this disableAutomaticUidColumn(bool $toggle = true) | ||
| */ | ||
| class SearchIndexDefinition extends IndexDefinition | ||
| { | ||
| /** | ||
| * @return array{ sortOrderSharding?: bool, disableAutomaticUidColumn?: bool } | ||
| */ | ||
| public function getOptions(): array | ||
| { | ||
| return array_filter([ | ||
| 'sortOrderSharding' => $this->sortOrderSharding, | ||
| 'disableAutomaticUidColumn' => $this->disableAutomaticUidColumn, | ||
| ], static fn($v) => $v !== null); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.