|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "810ce279", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## ACL Quickstart for Azure AI Search" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "b6585426", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## 1. Load Connections" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 1, |
| 22 | + "id": "2975a7f5", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "from dotenv import load_dotenv\n", |
| 27 | + "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", |
| 28 | + "import os\n", |
| 29 | + "\n", |
| 30 | + "load_dotenv(override=True) # take environment variables from .env.\n", |
| 31 | + "\n", |
| 32 | + "# The following variables from your .env file are used in this notebook\n", |
| 33 | + "endpoint = os.environ[\"AZURE_SEARCH_ENDPOINT\"]\n", |
| 34 | + "credential = DefaultAzureCredential()\n", |
| 35 | + "index_name = os.getenv(\"AZURE_SEARCH_INDEX\", \"acl-sample\")\n", |
| 36 | + "token_provider = get_bearer_token_provider(credential, \"https://search.azure.com/.default\")\n" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "id": "9327cf01", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "## 2. Create Sample Index" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 2, |
| 50 | + "id": "9863061f", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "name": "stdout", |
| 55 | + "output_type": "stream", |
| 56 | + "text": [ |
| 57 | + "Index 'acl-sample' created with permission filter option enabled.\n" |
| 58 | + ] |
| 59 | + } |
| 60 | + ], |
| 61 | + "source": [ |
| 62 | + "from azure.search.documents.indexes.models import SearchField, SearchIndex, PermissionFilter, SearchIndexPermissionFilterOption\n", |
| 63 | + "from azure.search.documents.indexes import SearchIndexClient\n", |
| 64 | + "\n", |
| 65 | + "index_client = SearchIndexClient(endpoint=endpoint, credential=credential)\n", |
| 66 | + "index = SearchIndex(\n", |
| 67 | + " name=index_name,\n", |
| 68 | + " fields=[\n", |
| 69 | + " SearchField(name=\"id\", type=\"Edm.String\", key=True, filterable=True, sortable=True),\n", |
| 70 | + " SearchField(name=\"oid\", type=\"Collection(Edm.String)\", filterable=True, permission_filter=PermissionFilter.USER_IDS),\n", |
| 71 | + " SearchField(name=\"group\", type=\"Collection(Edm.String)\", filterable=True, permission_filter=PermissionFilter.GROUP_IDS),\n", |
| 72 | + " SearchField(name=\"name\", type=\"Edm.String\", searchable=True)\n", |
| 73 | + " ],\n", |
| 74 | + " permission_filter_option=SearchIndexPermissionFilterOption.ENABLED\n", |
| 75 | + ")\n", |
| 76 | + "\n", |
| 77 | + "index_client.create_index(index=index)\n", |
| 78 | + "print(f\"Index '{index_name}' created with permission filter option enabled.\")" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "id": "f5cf4169", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "## 3. Connect to Graph to find your oid and groups" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 3, |
| 92 | + "id": "63904f09", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "from msgraph import GraphServiceClient\n", |
| 97 | + "client = GraphServiceClient(credentials=credential, scopes=[\"https://graph.microsoft.com/.default\"])\n", |
| 98 | + "\n", |
| 99 | + "groups = await client.me.member_of.get()\n", |
| 100 | + "me = await client.me.get()\n", |
| 101 | + "oid = me.id" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "id": "a9ce6d0f", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "## 4. Upload Sample Data" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 14, |
| 115 | + "id": "8fb830a1", |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [ |
| 118 | + { |
| 119 | + "name": "stdout", |
| 120 | + "output_type": "stream", |
| 121 | + "text": [ |
| 122 | + "Documents uploaded to the index.\n" |
| 123 | + ] |
| 124 | + } |
| 125 | + ], |
| 126 | + "source": [ |
| 127 | + "from azure.search.documents import SearchClient\n", |
| 128 | + "search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)\n", |
| 129 | + "\n", |
| 130 | + "documents = [\n", |
| 131 | + " { \"id\": \"1\", \"oid\": [oid], \"group\": [groups.value[0].id], \"name\": \"Document 1\" },\n", |
| 132 | + " { \"id\": \"2\", \"oid\": [\"all\"], \"group\": [groups.value[0].id], \"name\": \"Document 2\" },\n", |
| 133 | + " { \"id\": \"3\", \"oid\": [oid], \"group\": [\"all\"], \"name\": \"Document 3\" },\n", |
| 134 | + " { \"id\": \"4\", \"oid\": [\"none\"], \"group\": [\"none\"], \"name\": \"Document 4\" },\n", |
| 135 | + " { \"id\": \"5\", \"oid\": [\"none\"], \"group\": [groups.value[0].id], \"name\": \"Document 5\" },\n", |
| 136 | + "]\n", |
| 137 | + "search_client.upload_documents(documents=documents)\n", |
| 138 | + "print(\"Documents uploaded to the index.\")\n" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "id": "e5c93f76", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "## 5. Search sample data with x-ms-query-source-authorization" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": 16, |
| 152 | + "id": "cd872e8c", |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [ |
| 155 | + { |
| 156 | + "name": "stdout", |
| 157 | + "output_type": "stream", |
| 158 | + "text": [ |
| 159 | + "Name: Document 1, OID: ['44e5f686-35a1-4e2c-9ff3-7b6adafcd3c3'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n", |
| 160 | + "Name: Document 2, OID: ['all'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n", |
| 161 | + "Name: Document 3, OID: ['44e5f686-35a1-4e2c-9ff3-7b6adafcd3c3'], Group: ['all']\n", |
| 162 | + "Name: Document 5, OID: ['none'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n" |
| 163 | + ] |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "results = search_client.search(search_text=\"*\", x_ms_query_source_authorization=token_provider(), select=\"name,oid,group\", order_by=\"id asc\")\n", |
| 168 | + "\n", |
| 169 | + "for result in results:\n", |
| 170 | + " print(f\"Name: {result['name']}, OID: {result['oid']}, Group: {result['group']}\")" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "id": "d31b67d8", |
| 176 | + "metadata": {}, |
| 177 | + "source": [ |
| 178 | + "## 6. Search sample data without x-ms-query-source-authorization" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": 17, |
| 184 | + "id": "a1f2f2a0", |
| 185 | + "metadata": {}, |
| 186 | + "outputs": [ |
| 187 | + { |
| 188 | + "name": "stdout", |
| 189 | + "output_type": "stream", |
| 190 | + "text": [ |
| 191 | + "Name: Document 1, OID: ['44e5f686-35a1-4e2c-9ff3-7b6adafcd3c3'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n", |
| 192 | + "Name: Document 2, OID: ['all'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n", |
| 193 | + "Name: Document 3, OID: ['44e5f686-35a1-4e2c-9ff3-7b6adafcd3c3'], Group: ['all']\n", |
| 194 | + "Name: Document 4, OID: ['none'], Group: ['none']\n", |
| 195 | + "Name: Document 5, OID: ['none'], Group: ['ec5aece9-33fc-4b2e-abe1-aedf771357a3']\n" |
| 196 | + ] |
| 197 | + } |
| 198 | + ], |
| 199 | + "source": [ |
| 200 | + "results = search_client.search(search_text=\"*\", x_ms_query_source_authorization=None, select=\"name,oid,group\", order_by=\"id asc\")\n", |
| 201 | + "\n", |
| 202 | + "for result in results:\n", |
| 203 | + " print(f\"Name: {result['name']}, OID: {result['oid']}, Group: {result['group']}\")" |
| 204 | + ] |
| 205 | + } |
| 206 | + ], |
| 207 | + "metadata": { |
| 208 | + "kernelspec": { |
| 209 | + "display_name": ".venv", |
| 210 | + "language": "python", |
| 211 | + "name": "python3" |
| 212 | + }, |
| 213 | + "language_info": { |
| 214 | + "codemirror_mode": { |
| 215 | + "name": "ipython", |
| 216 | + "version": 3 |
| 217 | + }, |
| 218 | + "file_extension": ".py", |
| 219 | + "mimetype": "text/x-python", |
| 220 | + "name": "python", |
| 221 | + "nbconvert_exporter": "python", |
| 222 | + "pygments_lexer": "ipython3", |
| 223 | + "version": "3.12.10" |
| 224 | + } |
| 225 | + }, |
| 226 | + "nbformat": 4, |
| 227 | + "nbformat_minor": 5 |
| 228 | +} |
0 commit comments