-
Notifications
You must be signed in to change notification settings - Fork 22
[DOC-9567] - Add scoped index management examples #190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from couchbase.auth import PasswordAuthenticator | ||
| from couchbase.cluster import Cluster | ||
| from couchbase.exceptions import QueryIndexAlreadyExistsException | ||
| from couchbase.management.queries import (BuildDeferredQueryIndexOptions, | ||
| CreatePrimaryQueryIndexOptions, | ||
| CreateQueryIndexOptions, | ||
| DropPrimaryQueryIndexOptions, | ||
| DropQueryIndexOptions, | ||
| WatchQueryIndexOptions) | ||
|
|
||
|
|
||
| def primary_index(query_index_mgr): | ||
| print("Example - [primary]") | ||
| # tag::primary[] | ||
| try: | ||
| query_index_mgr.create_primary_index("travel-sample", CreatePrimaryQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users", | ||
| # Set this if you wish to use a custom name | ||
| # index_name="custom_name", | ||
| ignore_if_exists=True | ||
| )) | ||
| except QueryIndexAlreadyExistsException: | ||
| print("Index already exists") | ||
| # end::primary[] | ||
|
|
||
|
|
||
| def secondary_index(query_index_mgr): | ||
| print("Example - [secondary]") | ||
| # tag::secondary[] | ||
| try: | ||
| query_index_mgr.create_index("travel-sample", "tenant_agent_01_users_email", ["preferred_email"], CreateQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users" | ||
| )) | ||
| except QueryIndexAlreadyExistsException: | ||
| print("Index already exists") | ||
| # end::secondary[] | ||
|
|
||
|
|
||
| def defer_and_watch_index(query_index_mgr): | ||
| print("Example - [defer-indexes]") | ||
| # tag::defer-indexes[] | ||
| try: | ||
| # Create a deferred index | ||
| query_index_mgr.create_index("travel-sample", "tenant_agent_01_users_phone", ["preferred_phone"], CreateQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users", | ||
| deferred=True | ||
| )) | ||
|
|
||
| # Build any deferred indexes within `travel-sample`.tenant_agent_01.users | ||
| query_index_mgr.build_deferred_indexes("travel-sample", BuildDeferredQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users" | ||
| )) | ||
|
|
||
| # Wait for indexes to come online | ||
| query_index_mgr.watch_indexes("travel-sample", ["tenant_agent_01_users_phone"], WatchQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users", | ||
| timeout=timedelta(seconds=30) | ||
| )) | ||
| except QueryIndexAlreadyExistsException: | ||
| print("Index already exists") | ||
| # end::defer-indexes[] | ||
|
|
||
|
|
||
| def drop_primary_and_secondary_index(query_index_mgr): | ||
| print("Example - [drop-primary-or-secondary-index]") | ||
| # tag::drop-primary-or-secondary-index[] | ||
| # Drop a primary index | ||
| query_index_mgr.drop_primary_index("travel-sample", DropPrimaryQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users" | ||
| )) | ||
|
|
||
| # Drop a secondary index | ||
| query_index_mgr.drop_index("travel-sample", "tenant_agent_01_users_email", DropQueryIndexOptions( | ||
| scope_name="tenant_agent_01", | ||
| collection_name="users" | ||
| )) | ||
| # end::drop-primary-or-secondary-index[] | ||
|
|
||
|
|
||
| # tag::creating-index-mgr[] | ||
| cluster = Cluster( | ||
| "couchbase://localhost", | ||
| authenticator=PasswordAuthenticator("Administrator", "password") | ||
| ) | ||
|
|
||
| query_index_mgr = cluster.query_indexes() | ||
| # end::creating-index-mgr[] | ||
|
|
||
| primary_index(query_index_mgr) | ||
| secondary_index(query_index_mgr) | ||
| defer_and_watch_index(query_index_mgr) | ||
| drop_primary_and_secondary_index(query_index_mgr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a thought (I realise this is already in -common, so not requesting a change now) but might it be more useful to store the URL / URL + description separately from the "BucketManager --" text? hmm... well, not important anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I think it would be nice to actually have a separate improvements ticket for API docs references.
So maybe we can collate all this there, and cleanup how we use these variables.
Maybe we can chat about it on a Friday meeting.