|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | + |
| 17 | +# [START contentwarehouse_quickstart] |
| 18 | + |
| 19 | +from google.cloud import contentwarehouse |
| 20 | + |
| 21 | +# TODO(developer): Uncomment these variables before running the sample. |
| 22 | +# project_number = 'YOUR_PROJECT_NUMBER' |
| 23 | +# location = 'YOUR_PROJECT_LOCATION' # Format is 'us' or 'eu' |
| 24 | + |
| 25 | + |
| 26 | +def quickstart(project_number: str, location: str) -> None: |
| 27 | + |
| 28 | + # Create a Schema Service client |
| 29 | + document_schema_client = contentwarehouse.DocumentSchemaServiceClient() |
| 30 | + |
| 31 | + # The full resource name of the location, e.g.: |
| 32 | + # projects/{project_number}/locations/{location} |
| 33 | + parent = document_schema_client.common_location_path( |
| 34 | + project=project_number, location=location |
| 35 | + ) |
| 36 | + |
| 37 | + # Define Schema Property of Text Type |
| 38 | + property_definition = contentwarehouse.PropertyDefinition( |
| 39 | + name="stock_symbol", # Must be unique within a document schema (case insensitive) |
| 40 | + display_name="Searchable text", |
| 41 | + is_searchable=True, |
| 42 | + text_type_options=contentwarehouse.TextTypeOptions(), |
| 43 | + ) |
| 44 | + |
| 45 | + # Define Document Schema Request |
| 46 | + create_document_schema_request = contentwarehouse.CreateDocumentSchemaRequest( |
| 47 | + parent=parent, |
| 48 | + document_schema=contentwarehouse.DocumentSchema( |
| 49 | + display_name="My Test Schema", |
| 50 | + property_definitions=[property_definition], |
| 51 | + ), |
| 52 | + ) |
| 53 | + |
| 54 | + # Create a Document schema |
| 55 | + document_schema = document_schema_client.create_document_schema( |
| 56 | + request=create_document_schema_request |
| 57 | + ) |
| 58 | + |
| 59 | + # Create a Document Service client |
| 60 | + document_client = contentwarehouse.DocumentServiceClient() |
| 61 | + |
| 62 | + # The full resource name of the location, e.g.: |
| 63 | + # projects/{project_number}/locations/{location} |
| 64 | + parent = document_client.common_location_path( |
| 65 | + project=project_number, location=location |
| 66 | + ) |
| 67 | + |
| 68 | + # Define Document Property Value |
| 69 | + document_property = contentwarehouse.Property( |
| 70 | + name=document_schema.property_definitions[0].name, |
| 71 | + text_values=contentwarehouse.TextArray(values=["GOOG"]), |
| 72 | + ) |
| 73 | + |
| 74 | + # Define Document |
| 75 | + document = contentwarehouse.Document( |
| 76 | + display_name="My Test Document", |
| 77 | + document_schema_name=document_schema.name, |
| 78 | + plain_text="This is a sample of a document's text.", |
| 79 | + properties=[document_property], |
| 80 | + ) |
| 81 | + |
| 82 | + # Define Request |
| 83 | + create_document_request = contentwarehouse.CreateDocumentRequest( |
| 84 | + parent=parent, document=document |
| 85 | + ) |
| 86 | + |
| 87 | + # Create a Document for the given schema |
| 88 | + response = document_client.create_document(request=create_document_request) |
| 89 | + |
| 90 | + # Read the output |
| 91 | + print(f"Rule Engine Output: {response.rule_engine_output}") |
| 92 | + print(f"Document Created: {response.document}") |
| 93 | + |
| 94 | + |
| 95 | +# [END contentwarehouse_quickstart] |
0 commit comments