|
8 | 8 | "## Document Permissions in Azure AI Search" |
9 | 9 | ] |
10 | 10 | }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "f445040a", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## 1. Load Connections" |
| 17 | + ] |
| 18 | + }, |
11 | 19 | { |
12 | 20 | "cell_type": "code", |
13 | | - "execution_count": 10, |
| 21 | + "execution_count": 33, |
14 | 22 | "id": "0b40bb5b", |
15 | 23 | "metadata": {}, |
16 | 24 | "outputs": [], |
|
30 | 38 | "adls_gen2_account_name = os.getenv(\"AZURE_STORAGE_ACCOUNT_NAME\", \"documentpermissionssample\")\n", |
31 | 39 | "adls_gen2_container_name = os.getenv(\"AZURE_STORAGE_CONTAINER_NAME\", \"documentpermissionssample\")\n", |
32 | 40 | "adls_gen2_connection_string = os.environ[\"AZURE_STORAGE_CONNECTION_STRING\"]\n", |
| 41 | + "adls_gen2_resource_id = os.environ[\"AZURE_STORAGE_RESOURCE_ID\"]\n", |
33 | 42 | "token_provider = get_bearer_token_provider(credential, \"https://search.azure.com/.default\")" |
34 | 43 | ] |
35 | 44 | }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "id": "2d46b940", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "## 2. Create Index" |
| 51 | + ] |
| 52 | + }, |
36 | 53 | { |
37 | 54 | "cell_type": "code", |
38 | | - "execution_count": 2, |
| 55 | + "execution_count": 34, |
39 | 56 | "id": "2f981cad", |
40 | 57 | "metadata": {}, |
41 | 58 | "outputs": [ |
|
56 | 73 | " name=index_name,\n", |
57 | 74 | " fields=[\n", |
58 | 75 | " SearchField(name=\"id\", type=\"Edm.String\", key=True, filterable=True, sortable=True),\n", |
59 | | - " SearchField(name=\"oid\", type=\"Collection(Edm.String)\", filterable=True, permission_filter=PermissionFilter.USER_IDS),\n", |
60 | | - " SearchField(name=\"group\", type=\"Collection(Edm.String)\", filterable=True, permission_filter=PermissionFilter.GROUP_IDS),\n", |
| 76 | + " SearchField(name=\"content\", type=\"Edm.String\", searchable=True, filterable=False, sortable=False),\n", |
| 77 | + " SearchField(name=\"oids\", type=\"Collection(Edm.String)\", filterable=True, permission_filter=PermissionFilter.USER_IDS),\n", |
| 78 | + " SearchField(name=\"groups\", type=\"Collection(Edm.String)\", filterable=True, permission_filter=PermissionFilter.GROUP_IDS),\n", |
61 | 79 | " SearchField(name=\"metadata_storage_path\", type=\"Edm.String\", searchable=True),\n", |
62 | 80 | " SearchField(name=\"metadata_storage_name\", type=\"Edm.String\", searchable=True)\n", |
63 | 81 | " ],\n", |
|
68 | 86 | "print(f\"Index '{index_name}' created with permission filter option enabled.\")" |
69 | 87 | ] |
70 | 88 | }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "2b8945a2", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "## 3. Create data source" |
| 95 | + ] |
| 96 | + }, |
71 | 97 | { |
72 | 98 | "cell_type": "code", |
73 | | - "execution_count": 11, |
| 99 | + "execution_count": 35, |
74 | 100 | "id": "b25aaf7b", |
75 | 101 | "metadata": {}, |
76 | 102 | "outputs": [ |
|
89 | 115 | "datasource = SearchIndexerDataSourceConnection(\n", |
90 | 116 | " name=datasource_name,\n", |
91 | 117 | " type=SearchIndexerDataSourceType.ADLS_GEN2,\n", |
92 | | - " connection_string=adls_gen2_connection_string,\n", |
| 118 | + " connection_string=f\"ResourceId={adls_gen2_resource_id};\",\n", |
93 | 119 | " container=SearchIndexerDataContainer(name=adls_gen2_container_name),\n", |
94 | 120 | " indexer_permission_options=[IndexerPermissionOption.GROUP_IDS]\n", |
95 | 121 | ")\n", |
|
98 | 124 | "print(f\"Datasource '{datasource_name}' created with permission filter option enabled.\")" |
99 | 125 | ] |
100 | 126 | }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "id": "ff5b912d", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "## 4. Get group id" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": 36, |
| 138 | + "id": "329fe160", |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "from msgraph import GraphServiceClient\n", |
| 143 | + "client = GraphServiceClient(credentials=credential, scopes=[\"https://graph.microsoft.com/.default\"])\n", |
| 144 | + "\n", |
| 145 | + "groups = await client.me.member_of.get()\n", |
| 146 | + "group_id = groups.value[0].id " |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "id": "20588dc3", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "## 5. Upload sample directory and file" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 37, |
| 160 | + "id": "acd28b29", |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [ |
| 163 | + { |
| 164 | + "data": { |
| 165 | + "text/plain": [ |
| 166 | + "{'counters': {'directories_successful': 2, 'files_successful': 1, 'failure_count': 0}, 'continuation': None}" |
| 167 | + ] |
| 168 | + }, |
| 169 | + "execution_count": 37, |
| 170 | + "metadata": {}, |
| 171 | + "output_type": "execute_result" |
| 172 | + } |
| 173 | + ], |
| 174 | + "source": [ |
| 175 | + "from azure.storage.filedatalake import DataLakeServiceClient\n", |
| 176 | + "\n", |
| 177 | + "service = DataLakeServiceClient.from_connection_string(adls_gen2_connection_string, credential=credential)\n", |
| 178 | + "container = service.get_file_system_client(adls_gen2_container_name)\n", |
| 179 | + "if not container.exists():\n", |
| 180 | + " container.create_file_system()\n", |
| 181 | + "data_dir_client = container.get_directory_client(\"data\")\n", |
| 182 | + "data_dir_client.create_directory()\n", |
| 183 | + "file_client = data_dir_client.create_file(\"sample.txt\")\n", |
| 184 | + "file_client.upload_data(\"This is a sample file.\", overwrite=True)\n", |
| 185 | + "\n", |
| 186 | + "root_dir_client = container.get_directory_client(\"/\")\n", |
| 187 | + "root_dir_client.update_access_control_recursive(f\"group:{group_id}:rwx\")\n" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "id": "ca6de2ad", |
| 193 | + "metadata": {}, |
| 194 | + "source": [ |
| 195 | + "## 6. Run indexer" |
| 196 | + ] |
| 197 | + }, |
101 | 198 | { |
102 | 199 | "cell_type": "code", |
103 | | - "execution_count": 15, |
| 200 | + "execution_count": 39, |
104 | 201 | "id": "2ce7eb5e", |
105 | 202 | "metadata": {}, |
106 | 203 | "outputs": [ |
|
120 | 217 | " target_index_name=index_name,\n", |
121 | 218 | " data_source_name=datasource_name,\n", |
122 | 219 | " field_mappings=[\n", |
123 | | - " FieldMapping(source_field_name=\"metadata_group_ids\", target_field_name=\"group\"),\n", |
124 | | - " FieldMapping(source_field_name=\"metadata_user_ids\", target_field_name=\"oid\"),\n", |
| 220 | + " FieldMapping(source_field_name=\"metadata_group_ids\", target_field_name=\"groups\"),\n", |
| 221 | + " FieldMapping(source_field_name=\"metadata_user_ids\", target_field_name=\"oids\"),\n", |
125 | 222 | " ]\n", |
126 | 223 | ")\n", |
127 | 224 | "\n", |
128 | 225 | "indexer_client.create_or_update_indexer(indexer)\n", |
129 | 226 | "print(f\"Indexer '{indexer_name}' created\")\n" |
130 | 227 | ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "markdown", |
| 231 | + "id": "987dd496", |
| 232 | + "metadata": {}, |
| 233 | + "source": [ |
| 234 | + "## 7. Search sample data using x-ms-query-source-authorization " |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "execution_count": 43, |
| 240 | + "id": "7a899da1", |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [ |
| 243 | + { |
| 244 | + "name": "stdout", |
| 245 | + "output_type": "stream", |
| 246 | + "text": [ |
| 247 | + "Path: https://magotteiadlsgen2.blob.core.windows.net/documentpermissionssample/data/sample.txt, OID: ['none'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n" |
| 248 | + ] |
| 249 | + } |
| 250 | + ], |
| 251 | + "source": [ |
| 252 | + "from azure.search.documents import SearchClient\n", |
| 253 | + "search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)\n", |
| 254 | + "\n", |
| 255 | + "results = search_client.search(search_text=\"*\", x_ms_query_source_authorization=token_provider(), select=\"metadata_storage_path,oids,groups\", order_by=\"id asc\")\n", |
| 256 | + "for result in results:\n", |
| 257 | + " print(f\"Path: {result['metadata_storage_path']}, OID: {result['oids']}, Group: {result['groups']}\")" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "markdown", |
| 262 | + "id": "c712ab8c", |
| 263 | + "metadata": {}, |
| 264 | + "source": [ |
| 265 | + "## 8. Search sample data without x-ms-query-source-authorization " |
| 266 | + ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "code", |
| 270 | + "execution_count": 44, |
| 271 | + "id": "72d203f0", |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [ |
| 274 | + { |
| 275 | + "name": "stdout", |
| 276 | + "output_type": "stream", |
| 277 | + "text": [ |
| 278 | + "Path: https://magotteiadlsgen2.blob.core.windows.net/documentpermissionssample/data/sample.txt, OID: ['none'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n" |
| 279 | + ] |
| 280 | + } |
| 281 | + ], |
| 282 | + "source": [ |
| 283 | + "from azure.search.documents import SearchClient\n", |
| 284 | + "search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)\n", |
| 285 | + "\n", |
| 286 | + "results = search_client.search(search_text=\"*\", x_ms_query_source_authorization=None, select=\"metadata_storage_path,oids,groups\", order_by=\"id asc\")\n", |
| 287 | + "for result in results:\n", |
| 288 | + " print(f\"Path: {result['metadata_storage_path']}, OID: {result['oids']}, Group: {result['groups']}\")" |
| 289 | + ] |
131 | 290 | } |
132 | 291 | ], |
133 | 292 | "metadata": { |
|
0 commit comments