| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // <auto-generated/> |
| | 5 | |
|
| | 6 | | #nullable disable |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Text.Json; |
| | 11 | | using System.Threading; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | | using Azure; |
| | 14 | | using Azure.Core; |
| | 15 | | using Azure.Core.Pipeline; |
| | 16 | | using Azure.Search.Documents.Models; |
| | 17 | |
|
| | 18 | | namespace Azure.Search.Documents |
| | 19 | | { |
| | 20 | | internal partial class DocumentsRestClient |
| | 21 | | { |
| | 22 | | private string endpoint; |
| | 23 | | private string indexName; |
| | 24 | | private Guid? xMsClientRequestId; |
| | 25 | | private string apiVersion; |
| | 26 | | private ClientDiagnostics _clientDiagnostics; |
| | 27 | | private HttpPipeline _pipeline; |
| | 28 | |
|
| | 29 | | /// <summary> Initializes a new instance of DocumentsRestClient. </summary> |
| | 30 | | /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param> |
| | 31 | | /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param> |
| | 32 | | /// <param name="endpoint"> The endpoint URL of the search service. </param> |
| | 33 | | /// <param name="indexName"> The name of the index. </param> |
| | 34 | | /// <param name="xMsClientRequestId"> The tracking ID sent with the request to help with debugging. </param> |
| | 35 | | /// <param name="apiVersion"> Api Version. </param> |
| | 36 | | /// <exception cref="ArgumentNullException"> <paramref name="endpoint"/>, <paramref name="indexName"/>, or <para |
| 248 | 37 | | public DocumentsRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint, string i |
| | 38 | | { |
| 248 | 39 | | if (endpoint == null) |
| | 40 | | { |
| 0 | 41 | | throw new ArgumentNullException(nameof(endpoint)); |
| | 42 | | } |
| 248 | 43 | | if (indexName == null) |
| | 44 | | { |
| 0 | 45 | | throw new ArgumentNullException(nameof(indexName)); |
| | 46 | | } |
| 248 | 47 | | if (apiVersion == null) |
| | 48 | | { |
| 0 | 49 | | throw new ArgumentNullException(nameof(apiVersion)); |
| | 50 | | } |
| | 51 | |
|
| 248 | 52 | | this.endpoint = endpoint; |
| 248 | 53 | | this.indexName = indexName; |
| 248 | 54 | | this.xMsClientRequestId = xMsClientRequestId; |
| 248 | 55 | | this.apiVersion = apiVersion; |
| 248 | 56 | | _clientDiagnostics = clientDiagnostics; |
| 248 | 57 | | _pipeline = pipeline; |
| 248 | 58 | | } |
| | 59 | |
|
| | 60 | | internal HttpMessage CreateCountRequest() |
| | 61 | | { |
| 40 | 62 | | var message = _pipeline.CreateMessage(); |
| 40 | 63 | | var request = message.Request; |
| 40 | 64 | | request.Method = RequestMethod.Get; |
| 40 | 65 | | var uri = new RawRequestUriBuilder(); |
| 40 | 66 | | uri.AppendRaw(endpoint, false); |
| 40 | 67 | | uri.AppendRaw("/indexes('", false); |
| 40 | 68 | | uri.AppendRaw(indexName, true); |
| 40 | 69 | | uri.AppendRaw("')", false); |
| 40 | 70 | | uri.AppendPath("/docs/$count", false); |
| 40 | 71 | | uri.AppendQuery("api-version", apiVersion, true); |
| 40 | 72 | | request.Uri = uri; |
| 40 | 73 | | if (xMsClientRequestId != null) |
| | 74 | | { |
| 0 | 75 | | request.Headers.Add("x-ms-client-request-id", xMsClientRequestId.Value); |
| | 76 | | } |
| 40 | 77 | | request.Headers.Add("Accept", "application/json; odata.metadata=none"); |
| 40 | 78 | | return message; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> Queries the number of documents in the index. </summary> |
| | 82 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 83 | | public async Task<Response<long>> CountAsync(CancellationToken cancellationToken = default) |
| | 84 | | { |
| 20 | 85 | | using var message = CreateCountRequest(); |
| 20 | 86 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 20 | 87 | | switch (message.Response.Status) |
| | 88 | | { |
| | 89 | | case 200: |
| | 90 | | { |
| | 91 | | long value = default; |
| 19 | 92 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 19 | 93 | | value = document.RootElement.GetInt64(); |
| 19 | 94 | | return Response.FromValue(value, message.Response); |
| | 95 | | } |
| | 96 | | default: |
| 1 | 97 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 98 | | } |
| 19 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> Queries the number of documents in the index. </summary> |
| | 102 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 103 | | public Response<long> Count(CancellationToken cancellationToken = default) |
| | 104 | | { |
| 20 | 105 | | using var message = CreateCountRequest(); |
| 20 | 106 | | _pipeline.Send(message, cancellationToken); |
| 20 | 107 | | switch (message.Response.Status) |
| | 108 | | { |
| | 109 | | case 200: |
| | 110 | | { |
| | 111 | | long value = default; |
| 19 | 112 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 19 | 113 | | value = document.RootElement.GetInt64(); |
| 19 | 114 | | return Response.FromValue(value, message.Response); |
| | 115 | | } |
| | 116 | | default: |
| 1 | 117 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 118 | | } |
| 19 | 119 | | } |
| | 120 | |
|
| | 121 | | internal HttpMessage CreateSearchPostRequest(SearchOptions searchRequest) |
| | 122 | | { |
| 87 | 123 | | var message = _pipeline.CreateMessage(); |
| 87 | 124 | | var request = message.Request; |
| 87 | 125 | | request.Method = RequestMethod.Post; |
| 87 | 126 | | var uri = new RawRequestUriBuilder(); |
| 87 | 127 | | uri.AppendRaw(endpoint, false); |
| 87 | 128 | | uri.AppendRaw("/indexes('", false); |
| 87 | 129 | | uri.AppendRaw(indexName, true); |
| 87 | 130 | | uri.AppendRaw("')", false); |
| 87 | 131 | | uri.AppendPath("/docs/search.post.search", false); |
| 87 | 132 | | uri.AppendQuery("api-version", apiVersion, true); |
| 87 | 133 | | request.Uri = uri; |
| 87 | 134 | | if (xMsClientRequestId != null) |
| | 135 | | { |
| 0 | 136 | | request.Headers.Add("x-ms-client-request-id", xMsClientRequestId.Value); |
| | 137 | | } |
| 87 | 138 | | request.Headers.Add("Accept", "application/json; odata.metadata=none"); |
| 87 | 139 | | request.Headers.Add("Content-Type", "application/json"); |
| 87 | 140 | | var content = new Utf8JsonRequestContent(); |
| 87 | 141 | | content.JsonWriter.WriteObjectValue(searchRequest); |
| 87 | 142 | | request.Content = content; |
| 87 | 143 | | return message; |
| | 144 | | } |
| | 145 | |
|
| | 146 | | /// <summary> Searches for documents in the index. </summary> |
| | 147 | | /// <param name="searchRequest"> The definition of the Search request. </param> |
| | 148 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 149 | | /// <exception cref="ArgumentNullException"> <paramref name="searchRequest"/> is null. </exception> |
| | 150 | | public async Task<Response<SearchDocumentsResult>> SearchPostAsync(SearchOptions searchRequest, CancellationToke |
| | 151 | | { |
| 0 | 152 | | if (searchRequest == null) |
| | 153 | | { |
| 0 | 154 | | throw new ArgumentNullException(nameof(searchRequest)); |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | using var message = CreateSearchPostRequest(searchRequest); |
| 0 | 158 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 159 | | switch (message.Response.Status) |
| | 160 | | { |
| | 161 | | case 200: |
| | 162 | | { |
| | 163 | | SearchDocumentsResult value = default; |
| 0 | 164 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 0 | 165 | | value = SearchDocumentsResult.DeserializeSearchDocumentsResult(document.RootElement); |
| 0 | 166 | | return Response.FromValue(value, message.Response); |
| | 167 | | } |
| | 168 | | default: |
| 0 | 169 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 170 | | } |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> Searches for documents in the index. </summary> |
| | 174 | | /// <param name="searchRequest"> The definition of the Search request. </param> |
| | 175 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 176 | | /// <exception cref="ArgumentNullException"> <paramref name="searchRequest"/> is null. </exception> |
| | 177 | | public Response<SearchDocumentsResult> SearchPost(SearchOptions searchRequest, CancellationToken cancellationTok |
| | 178 | | { |
| 0 | 179 | | if (searchRequest == null) |
| | 180 | | { |
| 0 | 181 | | throw new ArgumentNullException(nameof(searchRequest)); |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | using var message = CreateSearchPostRequest(searchRequest); |
| 0 | 185 | | _pipeline.Send(message, cancellationToken); |
| 0 | 186 | | switch (message.Response.Status) |
| | 187 | | { |
| | 188 | | case 200: |
| | 189 | | { |
| | 190 | | SearchDocumentsResult value = default; |
| 0 | 191 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 0 | 192 | | value = SearchDocumentsResult.DeserializeSearchDocumentsResult(document.RootElement); |
| 0 | 193 | | return Response.FromValue(value, message.Response); |
| | 194 | | } |
| | 195 | | default: |
| 0 | 196 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 197 | | } |
| 0 | 198 | | } |
| | 199 | |
|
| | 200 | | internal HttpMessage CreateGetRequest(string key, IEnumerable<string> selectedFields) |
| | 201 | | { |
| 56 | 202 | | var message = _pipeline.CreateMessage(); |
| 56 | 203 | | var request = message.Request; |
| 56 | 204 | | request.Method = RequestMethod.Get; |
| 56 | 205 | | var uri = new RawRequestUriBuilder(); |
| 56 | 206 | | uri.AppendRaw(endpoint, false); |
| 56 | 207 | | uri.AppendRaw("/indexes('", false); |
| 56 | 208 | | uri.AppendRaw(indexName, true); |
| 56 | 209 | | uri.AppendRaw("')", false); |
| 56 | 210 | | uri.AppendPath("/docs('", false); |
| 56 | 211 | | uri.AppendPath(key, true); |
| 56 | 212 | | uri.AppendPath("')", false); |
| 56 | 213 | | if (selectedFields != null) |
| | 214 | | { |
| 12 | 215 | | uri.AppendQueryDelimited("$select", selectedFields, ",", true); |
| | 216 | | } |
| 56 | 217 | | uri.AppendQuery("api-version", apiVersion, true); |
| 56 | 218 | | request.Uri = uri; |
| 56 | 219 | | if (xMsClientRequestId != null) |
| | 220 | | { |
| 0 | 221 | | request.Headers.Add("x-ms-client-request-id", xMsClientRequestId.Value); |
| | 222 | | } |
| 56 | 223 | | request.Headers.Add("Accept", "application/json; odata.metadata=none"); |
| 56 | 224 | | return message; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | /// <summary> Retrieves a document from the index. </summary> |
| | 228 | | /// <param name="key"> The key of the document to retrieve. </param> |
| | 229 | | /// <param name="selectedFields"> List of field names to retrieve for the document; Any field not retrieved will |
| | 230 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 231 | | /// <exception cref="ArgumentNullException"> <paramref name="key"/> is null. </exception> |
| | 232 | | public async Task<Response<IReadOnlyDictionary<string, object>>> GetAsync(string key, IEnumerable<string> select |
| | 233 | | { |
| 0 | 234 | | if (key == null) |
| | 235 | | { |
| 0 | 236 | | throw new ArgumentNullException(nameof(key)); |
| | 237 | | } |
| | 238 | |
|
| 0 | 239 | | using var message = CreateGetRequest(key, selectedFields); |
| 0 | 240 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 241 | | switch (message.Response.Status) |
| | 242 | | { |
| | 243 | | case 200: |
| | 244 | | { |
| | 245 | | IReadOnlyDictionary<string, object> value = default; |
| 0 | 246 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 0 | 247 | | Dictionary<string, object> dictionary = new Dictionary<string, object>(); |
| 0 | 248 | | foreach (var property in document.RootElement.EnumerateObject()) |
| | 249 | | { |
| 0 | 250 | | dictionary.Add(property.Name, property.Value.GetObject()); |
| | 251 | | } |
| 0 | 252 | | value = dictionary; |
| 0 | 253 | | return Response.FromValue(value, message.Response); |
| | 254 | | } |
| | 255 | | default: |
| 0 | 256 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 257 | | } |
| 0 | 258 | | } |
| | 259 | |
|
| | 260 | | /// <summary> Retrieves a document from the index. </summary> |
| | 261 | | /// <param name="key"> The key of the document to retrieve. </param> |
| | 262 | | /// <param name="selectedFields"> List of field names to retrieve for the document; Any field not retrieved will |
| | 263 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 264 | | /// <exception cref="ArgumentNullException"> <paramref name="key"/> is null. </exception> |
| | 265 | | public Response<IReadOnlyDictionary<string, object>> Get(string key, IEnumerable<string> selectedFields = null, |
| | 266 | | { |
| 0 | 267 | | if (key == null) |
| | 268 | | { |
| 0 | 269 | | throw new ArgumentNullException(nameof(key)); |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | using var message = CreateGetRequest(key, selectedFields); |
| 0 | 273 | | _pipeline.Send(message, cancellationToken); |
| 0 | 274 | | switch (message.Response.Status) |
| | 275 | | { |
| | 276 | | case 200: |
| | 277 | | { |
| | 278 | | IReadOnlyDictionary<string, object> value = default; |
| 0 | 279 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 0 | 280 | | Dictionary<string, object> dictionary = new Dictionary<string, object>(); |
| 0 | 281 | | foreach (var property in document.RootElement.EnumerateObject()) |
| | 282 | | { |
| 0 | 283 | | dictionary.Add(property.Name, property.Value.GetObject()); |
| | 284 | | } |
| 0 | 285 | | value = dictionary; |
| 0 | 286 | | return Response.FromValue(value, message.Response); |
| | 287 | | } |
| | 288 | | default: |
| 0 | 289 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 290 | | } |
| 0 | 291 | | } |
| | 292 | |
|
| | 293 | | internal HttpMessage CreateSuggestPostRequest(SuggestOptions suggestRequest) |
| | 294 | | { |
| 26 | 295 | | var message = _pipeline.CreateMessage(); |
| 26 | 296 | | var request = message.Request; |
| 26 | 297 | | request.Method = RequestMethod.Post; |
| 26 | 298 | | var uri = new RawRequestUriBuilder(); |
| 26 | 299 | | uri.AppendRaw(endpoint, false); |
| 26 | 300 | | uri.AppendRaw("/indexes('", false); |
| 26 | 301 | | uri.AppendRaw(indexName, true); |
| 26 | 302 | | uri.AppendRaw("')", false); |
| 26 | 303 | | uri.AppendPath("/docs/search.post.suggest", false); |
| 26 | 304 | | uri.AppendQuery("api-version", apiVersion, true); |
| 26 | 305 | | request.Uri = uri; |
| 26 | 306 | | if (xMsClientRequestId != null) |
| | 307 | | { |
| 0 | 308 | | request.Headers.Add("x-ms-client-request-id", xMsClientRequestId.Value); |
| | 309 | | } |
| 26 | 310 | | request.Headers.Add("Accept", "application/json; odata.metadata=none"); |
| 26 | 311 | | request.Headers.Add("Content-Type", "application/json"); |
| 26 | 312 | | var content = new Utf8JsonRequestContent(); |
| 26 | 313 | | content.JsonWriter.WriteObjectValue(suggestRequest); |
| 26 | 314 | | request.Content = content; |
| 26 | 315 | | return message; |
| | 316 | | } |
| | 317 | |
|
| | 318 | | /// <summary> Suggests documents in the index that match the given partial query text. </summary> |
| | 319 | | /// <param name="suggestRequest"> The Suggest request. </param> |
| | 320 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 321 | | /// <exception cref="ArgumentNullException"> <paramref name="suggestRequest"/> is null. </exception> |
| | 322 | | public async Task<Response<SuggestDocumentsResult>> SuggestPostAsync(SuggestOptions suggestRequest, Cancellation |
| | 323 | | { |
| 0 | 324 | | if (suggestRequest == null) |
| | 325 | | { |
| 0 | 326 | | throw new ArgumentNullException(nameof(suggestRequest)); |
| | 327 | | } |
| | 328 | |
|
| 0 | 329 | | using var message = CreateSuggestPostRequest(suggestRequest); |
| 0 | 330 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 331 | | switch (message.Response.Status) |
| | 332 | | { |
| | 333 | | case 200: |
| | 334 | | { |
| | 335 | | SuggestDocumentsResult value = default; |
| 0 | 336 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 0 | 337 | | value = SuggestDocumentsResult.DeserializeSuggestDocumentsResult(document.RootElement); |
| 0 | 338 | | return Response.FromValue(value, message.Response); |
| | 339 | | } |
| | 340 | | default: |
| 0 | 341 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 342 | | } |
| 0 | 343 | | } |
| | 344 | |
|
| | 345 | | /// <summary> Suggests documents in the index that match the given partial query text. </summary> |
| | 346 | | /// <param name="suggestRequest"> The Suggest request. </param> |
| | 347 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 348 | | /// <exception cref="ArgumentNullException"> <paramref name="suggestRequest"/> is null. </exception> |
| | 349 | | public Response<SuggestDocumentsResult> SuggestPost(SuggestOptions suggestRequest, CancellationToken cancellatio |
| | 350 | | { |
| 0 | 351 | | if (suggestRequest == null) |
| | 352 | | { |
| 0 | 353 | | throw new ArgumentNullException(nameof(suggestRequest)); |
| | 354 | | } |
| | 355 | |
|
| 0 | 356 | | using var message = CreateSuggestPostRequest(suggestRequest); |
| 0 | 357 | | _pipeline.Send(message, cancellationToken); |
| 0 | 358 | | switch (message.Response.Status) |
| | 359 | | { |
| | 360 | | case 200: |
| | 361 | | { |
| | 362 | | SuggestDocumentsResult value = default; |
| 0 | 363 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 0 | 364 | | value = SuggestDocumentsResult.DeserializeSuggestDocumentsResult(document.RootElement); |
| 0 | 365 | | return Response.FromValue(value, message.Response); |
| | 366 | | } |
| | 367 | | default: |
| 0 | 368 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 369 | | } |
| 0 | 370 | | } |
| | 371 | |
|
| | 372 | | internal HttpMessage CreateIndexRequest(IndexBatch batch) |
| | 373 | | { |
| 0 | 374 | | var message = _pipeline.CreateMessage(); |
| 0 | 375 | | var request = message.Request; |
| 0 | 376 | | request.Method = RequestMethod.Post; |
| 0 | 377 | | var uri = new RawRequestUriBuilder(); |
| 0 | 378 | | uri.AppendRaw(endpoint, false); |
| 0 | 379 | | uri.AppendRaw("/indexes('", false); |
| 0 | 380 | | uri.AppendRaw(indexName, true); |
| 0 | 381 | | uri.AppendRaw("')", false); |
| 0 | 382 | | uri.AppendPath("/docs/search.index", false); |
| 0 | 383 | | uri.AppendQuery("api-version", apiVersion, true); |
| 0 | 384 | | request.Uri = uri; |
| 0 | 385 | | if (xMsClientRequestId != null) |
| | 386 | | { |
| 0 | 387 | | request.Headers.Add("x-ms-client-request-id", xMsClientRequestId.Value); |
| | 388 | | } |
| 0 | 389 | | request.Headers.Add("Accept", "application/json; odata.metadata=none"); |
| 0 | 390 | | request.Headers.Add("Content-Type", "application/json"); |
| 0 | 391 | | var content = new Utf8JsonRequestContent(); |
| 0 | 392 | | content.JsonWriter.WriteObjectValue(batch); |
| 0 | 393 | | request.Content = content; |
| 0 | 394 | | return message; |
| | 395 | | } |
| | 396 | |
|
| | 397 | | /// <summary> Sends a batch of document write actions to the index. </summary> |
| | 398 | | /// <param name="batch"> The batch of index actions. </param> |
| | 399 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 400 | | /// <exception cref="ArgumentNullException"> <paramref name="batch"/> is null. </exception> |
| | 401 | | public async Task<Response<IndexDocumentsResult>> IndexAsync(IndexBatch batch, CancellationToken cancellationTok |
| | 402 | | { |
| 0 | 403 | | if (batch == null) |
| | 404 | | { |
| 0 | 405 | | throw new ArgumentNullException(nameof(batch)); |
| | 406 | | } |
| | 407 | |
|
| 0 | 408 | | using var message = CreateIndexRequest(batch); |
| 0 | 409 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 0 | 410 | | switch (message.Response.Status) |
| | 411 | | { |
| | 412 | | case 200: |
| | 413 | | case 207: |
| | 414 | | { |
| | 415 | | IndexDocumentsResult value = default; |
| 0 | 416 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 0 | 417 | | value = IndexDocumentsResult.DeserializeIndexDocumentsResult(document.RootElement); |
| 0 | 418 | | return Response.FromValue(value, message.Response); |
| | 419 | | } |
| | 420 | | default: |
| 0 | 421 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 422 | | } |
| 0 | 423 | | } |
| | 424 | |
|
| | 425 | | /// <summary> Sends a batch of document write actions to the index. </summary> |
| | 426 | | /// <param name="batch"> The batch of index actions. </param> |
| | 427 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 428 | | /// <exception cref="ArgumentNullException"> <paramref name="batch"/> is null. </exception> |
| | 429 | | public Response<IndexDocumentsResult> Index(IndexBatch batch, CancellationToken cancellationToken = default) |
| | 430 | | { |
| 0 | 431 | | if (batch == null) |
| | 432 | | { |
| 0 | 433 | | throw new ArgumentNullException(nameof(batch)); |
| | 434 | | } |
| | 435 | |
|
| 0 | 436 | | using var message = CreateIndexRequest(batch); |
| 0 | 437 | | _pipeline.Send(message, cancellationToken); |
| 0 | 438 | | switch (message.Response.Status) |
| | 439 | | { |
| | 440 | | case 200: |
| | 441 | | case 207: |
| | 442 | | { |
| | 443 | | IndexDocumentsResult value = default; |
| 0 | 444 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 0 | 445 | | value = IndexDocumentsResult.DeserializeIndexDocumentsResult(document.RootElement); |
| 0 | 446 | | return Response.FromValue(value, message.Response); |
| | 447 | | } |
| | 448 | | default: |
| 0 | 449 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 450 | | } |
| 0 | 451 | | } |
| | 452 | |
|
| | 453 | | internal HttpMessage CreateAutocompletePostRequest(AutocompleteOptions autocompleteRequest) |
| | 454 | | { |
| 36 | 455 | | var message = _pipeline.CreateMessage(); |
| 36 | 456 | | var request = message.Request; |
| 36 | 457 | | request.Method = RequestMethod.Post; |
| 36 | 458 | | var uri = new RawRequestUriBuilder(); |
| 36 | 459 | | uri.AppendRaw(endpoint, false); |
| 36 | 460 | | uri.AppendRaw("/indexes('", false); |
| 36 | 461 | | uri.AppendRaw(indexName, true); |
| 36 | 462 | | uri.AppendRaw("')", false); |
| 36 | 463 | | uri.AppendPath("/docs/search.post.autocomplete", false); |
| 36 | 464 | | uri.AppendQuery("api-version", apiVersion, true); |
| 36 | 465 | | request.Uri = uri; |
| 36 | 466 | | if (xMsClientRequestId != null) |
| | 467 | | { |
| 0 | 468 | | request.Headers.Add("x-ms-client-request-id", xMsClientRequestId.Value); |
| | 469 | | } |
| 36 | 470 | | request.Headers.Add("Accept", "application/json; odata.metadata=none"); |
| 36 | 471 | | request.Headers.Add("Content-Type", "application/json"); |
| 36 | 472 | | var content = new Utf8JsonRequestContent(); |
| 36 | 473 | | content.JsonWriter.WriteObjectValue(autocompleteRequest); |
| 36 | 474 | | request.Content = content; |
| 36 | 475 | | return message; |
| | 476 | | } |
| | 477 | |
|
| | 478 | | /// <summary> Autocompletes incomplete query terms based on input text and matching terms in the index. </summar |
| | 479 | | /// <param name="autocompleteRequest"> The definition of the Autocomplete request. </param> |
| | 480 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 481 | | /// <exception cref="ArgumentNullException"> <paramref name="autocompleteRequest"/> is null. </exception> |
| | 482 | | public async Task<Response<AutocompleteResults>> AutocompletePostAsync(AutocompleteOptions autocompleteRequest, |
| | 483 | | { |
| 18 | 484 | | if (autocompleteRequest == null) |
| | 485 | | { |
| 0 | 486 | | throw new ArgumentNullException(nameof(autocompleteRequest)); |
| | 487 | | } |
| | 488 | |
|
| 18 | 489 | | using var message = CreateAutocompletePostRequest(autocompleteRequest); |
| 18 | 490 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 18 | 491 | | switch (message.Response.Status) |
| | 492 | | { |
| | 493 | | case 200: |
| | 494 | | { |
| | 495 | | AutocompleteResults value = default; |
| 16 | 496 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 16 | 497 | | value = AutocompleteResults.DeserializeAutocompleteResults(document.RootElement); |
| 16 | 498 | | return Response.FromValue(value, message.Response); |
| | 499 | | } |
| | 500 | | default: |
| 2 | 501 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 502 | | } |
| 16 | 503 | | } |
| | 504 | |
|
| | 505 | | /// <summary> Autocompletes incomplete query terms based on input text and matching terms in the index. </summar |
| | 506 | | /// <param name="autocompleteRequest"> The definition of the Autocomplete request. </param> |
| | 507 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 508 | | /// <exception cref="ArgumentNullException"> <paramref name="autocompleteRequest"/> is null. </exception> |
| | 509 | | public Response<AutocompleteResults> AutocompletePost(AutocompleteOptions autocompleteRequest, CancellationToken |
| | 510 | | { |
| 18 | 511 | | if (autocompleteRequest == null) |
| | 512 | | { |
| 0 | 513 | | throw new ArgumentNullException(nameof(autocompleteRequest)); |
| | 514 | | } |
| | 515 | |
|
| 18 | 516 | | using var message = CreateAutocompletePostRequest(autocompleteRequest); |
| 18 | 517 | | _pipeline.Send(message, cancellationToken); |
| 18 | 518 | | switch (message.Response.Status) |
| | 519 | | { |
| | 520 | | case 200: |
| | 521 | | { |
| | 522 | | AutocompleteResults value = default; |
| 16 | 523 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 16 | 524 | | value = AutocompleteResults.DeserializeAutocompleteResults(document.RootElement); |
| 16 | 525 | | return Response.FromValue(value, message.Response); |
| | 526 | | } |
| | 527 | | default: |
| 2 | 528 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 529 | | } |
| 16 | 530 | | } |
| | 531 | | } |
| | 532 | | } |