0
# Search Index Management
1
2
The SearchIndexClient provides comprehensive management of search indexes, including schema definition, field configuration, analyzers, scoring profiles, vector search settings, and synonym maps. It handles the complete lifecycle of search indexes from creation to deletion.
3
4
## Capabilities
5
6
### Client Initialization
7
8
Create a SearchIndexClient to manage indexes across a search service.
9
10
```python { .api }
11
class SearchIndexClient:
12
def __init__(
13
self,
14
endpoint: str,
15
credential: Union[AzureKeyCredential, TokenCredential],
16
**kwargs
17
) -> None:
18
"""
19
Initialize SearchIndexClient for index management.
20
21
Parameters:
22
- endpoint (str): The URL endpoint of an Azure search service
23
- credential: A credential to authorize requests
24
- api_version (str, optional): The Search API version to use
25
- audience (str, optional): AAD audience for authentication
26
"""
27
28
def close(self) -> None:
29
"""Close the session."""
30
31
def __enter__(self) -> "SearchIndexClient": ...
32
def __exit__(self, *args) -> None: ...
33
```
34
35
### Search Client Factory
36
37
Get SearchClient instances for specific indexes.
38
39
```python { .api }
40
def get_search_client(self, index_name: str, **kwargs) -> SearchClient:
41
"""
42
Get a SearchClient for a specific index.
43
44
Parameters:
45
- index_name (str): Name of the index
46
47
Returns:
48
SearchClient: Client for the specified index
49
"""
50
```
51
52
### Index Listing and Retrieval
53
54
List and retrieve search indexes with optional field selection.
55
56
```python { .api }
57
def list_indexes(
58
self,
59
*,
60
select: Optional[List[str]] = None,
61
**kwargs
62
) -> ItemPaged[SearchIndex]:
63
"""
64
List all indexes in the search service.
65
66
Parameters:
67
- select (List[str], optional): Fields to include in results
68
69
Returns:
70
ItemPaged[SearchIndex]: Paginated list of indexes
71
"""
72
73
def list_index_names(self, **kwargs) -> ItemPaged[str]:
74
"""
75
List all index names in the search service.
76
77
Returns:
78
ItemPaged[str]: Paginated list of index names
79
"""
80
81
def get_index(self, name: str, **kwargs) -> SearchIndex:
82
"""
83
Retrieve a specific index definition.
84
85
Parameters:
86
- name (str): Name of the index to retrieve
87
88
Returns:
89
SearchIndex: The index definition
90
"""
91
```
92
93
### Index Statistics
94
95
Get statistics and usage information for indexes.
96
97
```python { .api }
98
def get_index_statistics(self, index_name: str, **kwargs) -> Dict[str, Any]:
99
"""
100
Get statistics for a specific index.
101
102
Parameters:
103
- index_name (str): Name of the index
104
105
Returns:
106
Dict[str, Any]: Index statistics including document count and storage size
107
"""
108
```
109
110
### Index Creation and Updates
111
112
Create new indexes and update existing ones with full schema support.
113
114
```python { .api }
115
def create_index(self, index: SearchIndex, **kwargs) -> SearchIndex:
116
"""
117
Create a new search index.
118
119
Parameters:
120
- index (SearchIndex): The index definition to create
121
122
Returns:
123
SearchIndex: The created index definition
124
"""
125
126
def create_or_update_index(
127
self,
128
index: SearchIndex,
129
*,
130
allow_index_downtime: Optional[bool] = None,
131
if_match: Optional[str] = None,
132
if_none_match: Optional[str] = None,
133
**kwargs
134
) -> SearchIndex:
135
"""
136
Create a new index or update an existing one.
137
138
Parameters:
139
- index (SearchIndex): The index definition
140
- allow_index_downtime (bool): Allow index to be offline during update
141
- if_match (str): ETag for conditional updates
142
- if_none_match (str): ETag for conditional creation
143
144
Returns:
145
SearchIndex: The created or updated index definition
146
"""
147
```
148
149
### Index Deletion
150
151
Delete search indexes with conditional support.
152
153
```python { .api }
154
def delete_index(
155
self,
156
index: Union[str, SearchIndex],
157
*,
158
if_match: Optional[str] = None,
159
if_none_match: Optional[str] = None,
160
**kwargs
161
) -> None:
162
"""
163
Delete a search index.
164
165
Parameters:
166
- index: Index name or SearchIndex object to delete
167
- if_match (str): ETag for conditional deletion
168
- if_none_match (str): ETag for conditional deletion
169
"""
170
```
171
172
### Text Analysis
173
174
Test and analyze text processing with index analyzers.
175
176
```python { .api }
177
def analyze_text(
178
self,
179
index_name: str,
180
analyze_request: AnalyzeTextOptions,
181
**kwargs
182
) -> AnalyzeResult:
183
"""
184
Analyze text using the specified analyzer from an index.
185
186
Parameters:
187
- index_name (str): Name of the index containing the analyzer
188
- analyze_request (AnalyzeTextOptions): Analysis configuration
189
190
Returns:
191
AnalyzeResult: Analysis results with tokens and positions
192
"""
193
```
194
195
### Synonym Map Management
196
197
Manage synonym maps for search term expansion and query enhancement.
198
199
```python { .api }
200
def get_synonym_maps(
201
self,
202
*,
203
select: Optional[List[str]] = None,
204
**kwargs
205
) -> List[SynonymMap]:
206
"""
207
List all synonym maps in the search service.
208
209
Parameters:
210
- select (List[str], optional): Fields to include in results
211
212
Returns:
213
List[SynonymMap]: List of synonym maps
214
"""
215
216
def get_synonym_map_names(self, **kwargs) -> List[str]:
217
"""
218
List all synonym map names.
219
220
Returns:
221
List[str]: List of synonym map names
222
"""
223
224
def get_synonym_map(self, name: str, **kwargs) -> SynonymMap:
225
"""
226
Retrieve a specific synonym map.
227
228
Parameters:
229
- name (str): Name of the synonym map
230
231
Returns:
232
SynonymMap: The synonym map definition
233
"""
234
235
def create_synonym_map(self, synonym_map: SynonymMap, **kwargs) -> SynonymMap:
236
"""
237
Create a new synonym map.
238
239
Parameters:
240
- synonym_map (SynonymMap): The synonym map definition
241
242
Returns:
243
SynonymMap: The created synonym map
244
"""
245
246
def create_or_update_synonym_map(
247
self,
248
synonym_map: SynonymMap,
249
*,
250
if_match: Optional[str] = None,
251
if_none_match: Optional[str] = None,
252
**kwargs
253
) -> SynonymMap:
254
"""
255
Create a new synonym map or update an existing one.
256
257
Parameters:
258
- synonym_map (SynonymMap): The synonym map definition
259
- if_match (str): ETag for conditional updates
260
- if_none_match (str): ETag for conditional creation
261
262
Returns:
263
SynonymMap: The created or updated synonym map
264
"""
265
266
def delete_synonym_map(
267
self,
268
synonym_map: Union[str, SynonymMap],
269
*,
270
if_match: Optional[str] = None,
271
if_none_match: Optional[str] = None,
272
**kwargs
273
) -> None:
274
"""
275
Delete a synonym map.
276
277
Parameters:
278
- synonym_map: Synonym map name or SynonymMap object
279
- if_match (str): ETag for conditional deletion
280
- if_none_match (str): ETag for conditional deletion
281
"""
282
```
283
284
## Usage Examples
285
286
### Create a Simple Index
287
288
```python
289
from azure.search.documents.indexes import SearchIndexClient
290
from azure.search.documents.indexes.models import SearchIndex, SearchField, SearchFieldDataType
291
from azure.core.credentials import AzureKeyCredential
292
293
client = SearchIndexClient(
294
endpoint="https://service.search.windows.net",
295
credential=AzureKeyCredential("admin-key")
296
)
297
298
# Define index schema
299
fields = [
300
SearchField(name="id", type=SearchFieldDataType.String, key=True),
301
SearchField(name="title", type=SearchFieldDataType.String, searchable=True),
302
SearchField(name="content", type=SearchFieldDataType.String, searchable=True),
303
SearchField(name="category", type=SearchFieldDataType.String, filterable=True, facetable=True),
304
SearchField(name="rating", type=SearchFieldDataType.Double, filterable=True, sortable=True)
305
]
306
307
# Create index
308
index = SearchIndex(name="hotels", fields=fields)
309
created_index = client.create_index(index)
310
print(f"Created index: {created_index.name}")
311
```
312
313
### Vector Search Index
314
315
```python
316
from azure.search.documents.indexes.models import (
317
SearchIndex, SearchField, SearchFieldDataType, VectorSearch,
318
VectorSearchProfile, VectorSearchAlgorithmConfiguration, VectorSearchAlgorithmKind
319
)
320
321
# Define fields including vector field
322
fields = [
323
SearchField(name="id", type=SearchFieldDataType.String, key=True),
324
SearchField(name="content", type=SearchFieldDataType.String, searchable=True),
325
SearchField(
326
name="content_vector",
327
type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
328
vector_search_dimensions=1536,
329
vector_search_profile_name="my-vector-profile"
330
)
331
]
332
333
# Configure vector search
334
vector_search = VectorSearch(
335
profiles=[
336
VectorSearchProfile(
337
name="my-vector-profile",
338
algorithm_configuration_name="my-hnsw-config"
339
)
340
],
341
algorithms=[
342
VectorSearchAlgorithmConfiguration(
343
name="my-hnsw-config",
344
kind=VectorSearchAlgorithmKind.HNSW
345
)
346
]
347
)
348
349
# Create index with vector search
350
index = SearchIndex(
351
name="vector-index",
352
fields=fields,
353
vector_search=vector_search
354
)
355
client.create_index(index)
356
```
357
358
### Index with Custom Analyzer
359
360
```python
361
from azure.search.documents.indexes.models import (
362
SearchIndex, SearchField, CustomAnalyzer, StandardTokenizer,
363
LowercaseTokenFilter, StopwordsTokenFilter
364
)
365
366
# Define custom analyzer
367
custom_analyzer = CustomAnalyzer(
368
name="my_analyzer",
369
tokenizer_name=StandardTokenizer(),
370
token_filters=[
371
LowercaseTokenFilter(),
372
StopwordsTokenFilter(stopwords_list="english")
373
]
374
)
375
376
fields = [
377
SearchField(name="id", type=SearchFieldDataType.String, key=True),
378
SearchField(
379
name="description",
380
type=SearchFieldDataType.String,
381
searchable=True,
382
analyzer_name="my_analyzer"
383
)
384
]
385
386
index = SearchIndex(
387
name="custom-analyzer-index",
388
fields=fields,
389
analyzers=[custom_analyzer]
390
)
391
client.create_index(index)
392
```
393
394
### Synonym Map Management
395
396
```python
397
from azure.search.documents.indexes.models import SynonymMap
398
399
# Create synonym map
400
synonym_map = SynonymMap(
401
name="my-synonyms",
402
synonyms="USA,United States,US\nNY,New York"
403
)
404
client.create_synonym_map(synonym_map)
405
406
# Use in field definition
407
SearchField(
408
name="location",
409
type=SearchFieldDataType.String,
410
searchable=True,
411
synonym_map_names=["my-synonyms"]
412
)
413
```
414
415
### Text Analysis Testing
416
417
```python
418
from azure.search.documents.indexes.models import AnalyzeTextOptions
419
420
# Analyze text with index analyzer
421
analyze_request = AnalyzeTextOptions(
422
text="The quick brown fox jumps over the lazy dog",
423
analyzer_name="en.microsoft"
424
)
425
426
result = client.analyze_text("my-index", analyze_request)
427
for token in result.tokens:
428
print(f"Token: {token.token}, Position: {token.start_offset}-{token.end_offset}")
429
```
430
431
## Common Types
432
433
```python { .api }
434
# Index definition
435
class SearchIndex:
436
name: str
437
fields: List[SearchField]
438
scoring_profiles: Optional[List[ScoringProfile]] = None
439
default_scoring_profile: Optional[str] = None
440
cors_options: Optional[CorsOptions] = None
441
suggesters: Optional[List[Suggester]] = None
442
analyzers: Optional[List[LexicalAnalyzer]] = None
443
tokenizers: Optional[List[LexicalTokenizer]] = None
444
token_filters: Optional[List[TokenFilter]] = None
445
char_filters: Optional[List[CharFilter]] = None
446
encryption_key: Optional[SearchResourceEncryptionKey] = None
447
similarity: Optional[SimilarityAlgorithm] = None
448
semantic_search: Optional[SemanticSearch] = None
449
vector_search: Optional[VectorSearch] = None
450
e_tag: Optional[str] = None
451
452
# Field definition
453
class SearchField:
454
name: str
455
type: SearchFieldDataType
456
key: bool = False
457
retrievable: bool = True
458
searchable: bool = False
459
filterable: bool = False
460
sortable: bool = False
461
facetable: bool = False
462
analyzer_name: Optional[str] = None
463
search_analyzer_name: Optional[str] = None
464
index_analyzer_name: Optional[str] = None
465
synonym_map_names: Optional[List[str]] = None
466
fields: Optional[List["SearchField"]] = None
467
468
# Synonym map
469
class SynonymMap:
470
name: str
471
synonyms: str
472
encryption_key: Optional[SearchResourceEncryptionKey] = None
473
e_tag: Optional[str] = None
474
475
# Analysis result
476
class AnalyzeResult:
477
tokens: List[AnalyzedTokenInfo]
478
479
class AnalyzedTokenInfo:
480
token: str
481
start_offset: int
482
end_offset: int
483
position: int
484
```