public void CountingDocsOfNewIndexGivesZero() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); DocumentCountResponse countResponse = client.Documents.Count(); Assert.Equal(HttpStatusCode.OK, countResponse.StatusCode); Assert.Equal(0, countResponse.DocumentCount); }); }
public void RequestIdIsReturnedInResponse() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); // We need to use a constant GUID so that this test will still work in playback mode. Guid myRequestId = new Guid("c4cfce79-eb42-4e61-9909-84510c04706f"); client.SetClientRequestId(myRequestId); DocumentCountResponse countResponse = client.Documents.Count(); Assert.Equal(HttpStatusCode.OK, countResponse.StatusCode); Assert.Equal(myRequestId.ToString(), countResponse.RequestId); }); }
/// <summary> /// Queries the number of documents in the Azure Search index. (see /// https://msdn.microsoft.com/library/azure/dn798924.aspx for more /// information) /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Response containing the count of documents in the index. /// </returns> public async Task <DocumentCountResponse> CountAsync(CancellationToken cancellationToken) { // Validate // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); TracingAdapter.Enter(invocationId, this, "CountAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/docs/$count"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2015-02-28"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/json;odata.metadata=none"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result DocumentCountResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new DocumentCountResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { long documentCountInstance = ((long)responseDoc); result.DocumentCount = documentCountInstance; } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("request-id")) { result.RequestId = httpResponse.Headers.GetValues("request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
public void CanIndexDynamicDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var batch = new IndexBatch(new[] { new IndexAction( IndexActionType.Upload, new Document() { { "hotelId", "1" }, { "baseRate", 199.0 }, { "description", "Best hotel in town" }, { "descriptionFr", "Meilleur hôtel en ville" }, { "hotelName", "Fancy Stay" }, { "category", "Luxury" }, { "tags", new[] { "pool", "view", "wifi", "concierge" } }, { "parkingIncluded", false }, { "smokingAllowed", false }, { "lastRenovationDate", new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8)) }, { "rating", 5 }, { "location", GeographyPoint.Create(47.678581, -122.131577) } }), new IndexAction( IndexActionType.Upload, new Document() { { "hotelId", "2" }, { "baseRate", 79.99 }, { "description", "Cheapest hotel in town" }, { "descriptionFr", "Hôtel le moins cher en ville" }, { "hotelName", "Roach Motel" }, { "category", "Budget" }, { "tags", new[] { "motel", "budget" } }, { "parkingIncluded", true }, { "smokingAllowed", true }, { "lastRenovationDate", new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero) }, { "rating", 1 }, { "location", GeographyPoint.Create(49.678581, -122.131577) } }), new IndexAction( IndexActionType.Merge, new Document() { { "hotelId", "3" }, { "baseRate", 279.99 }, { "description", "Surprisingly expensive" }, { "lastRenovationDate", null } }), new IndexAction(IndexActionType.Delete, new Document() { { "hotelId", "4" } }), new IndexAction( IndexActionType.MergeOrUpload, new Document() { { "hotelId", "5" }, { "baseRate", Double.NaN }, { "hotelName", null }, { "tags", new string[0] } }) }); IndexBatchException e = Assert.Throws <IndexBatchException>(() => client.Documents.Index(batch)); AssertIsPartialFailure(e, batch, "3"); Assert.Equal(5, e.IndexResponse.Results.Count); AssertIndexActionSucceeded("1", e.IndexResponse.Results[0]); AssertIndexActionSucceeded("2", e.IndexResponse.Results[1]); AssertIndexActionFailed("3", e.IndexResponse.Results[2], "Document not found."); AssertIndexActionSucceeded("4", e.IndexResponse.Results[3]); AssertIndexActionSucceeded("5", e.IndexResponse.Results[4]); SearchTestUtilities.WaitForIndexing(); DocumentCountResponse countResponse = client.Documents.Count(); Assert.Equal(HttpStatusCode.OK, countResponse.StatusCode); Assert.Equal(3, countResponse.DocumentCount); }); }
public void CanIndexStaticallyTypedDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var batch = IndexBatch.Create(new[] { IndexAction.Create( IndexActionType.Upload, new Hotel() { HotelId = "1", BaseRate = 199.0, Description = "Best hotel in town", DescriptionFr = "Meilleur hôtel en ville", HotelName = "Fancy Stay", Category = "Luxury", Tags = new[] { "pool", "view", "wifi", "concierge" }, ParkingIncluded = false, SmokingAllowed = false, LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8)), Rating = 5, Location = GeographyPoint.Create(47.678581, -122.131577) }), IndexAction.Create( IndexActionType.Upload, new Hotel() { HotelId = "2", BaseRate = 79.99, Description = "Cheapest hotel in town", DescriptionFr = "Hôtel le moins cher en ville", HotelName = "Roach Motel", Category = "Budget", Tags = new[] { "motel", "budget" }, ParkingIncluded = true, SmokingAllowed = true, LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero), Rating = 1, Location = GeographyPoint.Create(49.678581, -122.131577) }), IndexAction.Create( IndexActionType.Merge, new Hotel() { HotelId = "3", BaseRate = 279.99, Description = "Surprisingly expensive", LastRenovationDate = null }), IndexAction.Create(IndexActionType.Delete, new Hotel() { HotelId = "4" }), IndexAction.Create( IndexActionType.MergeOrUpload, new Hotel() { HotelId = "5", BaseRate = Double.NaN, HotelName = null, Tags = new string[0] }) }); IndexBatchException e = Assert.Throws <IndexBatchException>(() => client.Documents.Index(batch)); AssertIsPartialFailure(e, batch, "3"); Assert.Equal(5, e.IndexResponse.Results.Count); AssertIndexActionSucceeded("1", e.IndexResponse.Results[0]); AssertIndexActionSucceeded("2", e.IndexResponse.Results[1]); AssertIndexActionFailed("3", e.IndexResponse.Results[2], "Document not found."); AssertIndexActionSucceeded("4", e.IndexResponse.Results[3]); AssertIndexActionSucceeded("5", e.IndexResponse.Results[4]); SearchTestUtilities.WaitForIndexing(); DocumentCountResponse countResponse = client.Documents.Count(); Assert.Equal(HttpStatusCode.OK, countResponse.StatusCode); Assert.Equal(3, countResponse.DocumentCount); }); }