public void CanGetDocumentWithBase64EncodedKey() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); string complexKey = HttpServerUtility.UrlTokenEncode(new byte[] { 1, 2, 3, 4, 5 }); var expectedDoc = new Document() { { "hotelId", complexKey } }; var batch = new IndexBatch(new[] { new IndexAction(expectedDoc) }); client.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); DocumentGetResponse getResponse = client.Documents.Get(complexKey, expectedDoc.Keys); Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode); SearchAssert.DocumentsEqual(expectedDoc, getResponse.Document); }); }
public void CanGetStaticallyTypedDocumentWithNullOrEmptyValues() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var expectedDoc = new Hotel() { HotelId = "1", HotelName = null, Tags = new string[0], ParkingIncluded = null, LastRenovationDate = null, Rating = null, Location = null, Address = new HotelAddress(), Rooms = new[] { new HotelRoom(), new HotelRoom() { BaseRate = null, BedOptions = null, SleepsCount = null, SmokingAllowed = null, Tags = new string[0] } } }; var batch = IndexBatch.Upload(new[] { expectedDoc }); client.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); Hotel actualDoc = client.Documents.Get <Hotel>("1"); Assert.Equal(expectedDoc, actualDoc); }); }
private void TestCanSuggestWithCustomConverter <TBook, TAuthor>(Action <SearchIndexClient> customizeSettings = null) where TBook : CustomBookBase <TAuthor>, new() where TAuthor : CustomAuthor, new() { customizeSettings = customizeSettings ?? (client => { }); SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = Book.DefineIndex(); serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); customizeSettings(indexClient); var doc = new TBook() { InternationalStandardBookNumber = "123", Name = "Lord of the Rings", AuthorName = new TAuthor() { FullName = "J.R.R. Tolkien" }, PublishDateTime = new DateTime(1954, 7, 29) }; var batch = IndexBatch.Upload(new[] { doc }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); var parameters = new SuggestParameters() { Select = new[] { "*" } }; DocumentSuggestResult <TBook> response = indexClient.Documents.Suggest <TBook>("Lord", "sg", parameters); Assert.Equal(1, response.Results.Count); Assert.Equal(doc, response.Results[0].Document); }
public void DynamicDocumentDateTimesRoundTripAsUtc() { Run(() => { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = Book.DefineIndex(); serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); // Can't test local date time since we might be testing against a pre-recorded mock response. DateTime utcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime unspecifiedDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); var batch = IndexBatch.Upload( new[] { new Document() { { "ISBN", "1" }, { "PublishDate", utcDateTime } }, new Document() { { "ISBN", "2" }, { "PublishDate", unspecifiedDateTime } } }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); Document book = indexClient.Documents.Get("1"); Assert.Equal(new DateTimeOffset(utcDateTime), book["PublishDate"]); book = indexClient.Documents.Get("2"); Assert.Equal(new DateTimeOffset(utcDateTime), book["PublishDate"]); }); }
public void StaticallyTypedDateTimesRoundTripAsUtc() { Run(() => { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = Book.DefineIndex(); serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); // Can't test local date time since we might be testing against a pre-recorded mock response. DateTime utcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime unspecifiedDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); var batch = IndexBatch.Upload( new[] { new Book() { ISBN = "1", PublishDate = utcDateTime }, new Book() { ISBN = "2", PublishDate = unspecifiedDateTime } }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); Book book = indexClient.Documents.Get <Book>("1"); Assert.Equal(utcDateTime, book.PublishDate); book = indexClient.Documents.Get <Book>("2"); Assert.Equal(utcDateTime, book.PublishDate); }); }
public void GetDynamicDocumentWithEmptyObjectsReturnsObjectsFullOfNulls() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var originalDoc = new Document() { ["hotelId"] = "1", ["address"] = new Document() }; var expectedDoc = new Document() { ["hotelId"] = "1", ["address"] = new Document() { ["streetAddress"] = null, ["city"] = null, ["stateProvince"] = null, ["country"] = null, ["postalCode"] = null } }; var batch = IndexBatch.Upload(new[] { originalDoc }); client.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); // Select only the fields set in the test case so we don't get superfluous data back. IEnumerable <string> selectedFields = SelectPopulatedFields(originalDoc); Document actualDoc = client.Documents.Get("1", selectedFields); Assert.Equal(expectedDoc, actualDoc); }); }
public void CanDeleteBatchByKeys() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var uploadBatch = IndexBatch.Upload( new[] { new Hotel() { HotelId = "1" }, new Hotel() { HotelId = "2" } }); client.Documents.Index(uploadBatch); SearchTestUtilities.WaitForIndexing(); Assert.Equal(2, client.Documents.Count()); var deleteBatch = IndexBatch.Delete("hotelId", new[] { "1", "2" }); DocumentIndexResult documentIndexResult = client.Documents.Index(deleteBatch); SearchTestUtilities.WaitForIndexing(); Assert.Equal(2, documentIndexResult.Results.Count); AssertIndexActionSucceeded("1", documentIndexResult.Results[0], 200); AssertIndexActionSucceeded("2", documentIndexResult.Results[1], 200); Assert.Equal(0, client.Documents.Count()); }); }
public void CanIndexAndRetrieveModelWithExtraProperties() { Run(() => { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = Book.DefineIndex(); serviceClient.Indexes.Create(index); SearchIndexClient client = Data.GetSearchIndexClient(index.Name); var resolver = new MyCustomContractResolver(); client.SerializationSettings.ContractResolver = resolver; client.DeserializationSettings.ContractResolver = resolver; string bookJson = @"{ ""ISBN"": ""123"", ""Title"": ""The Hobbit"", ""Author"": ""J.R.R.Tolkien"", ""Rating"": 5 }"; // Real customers would just use JsonConvert, but that would break the test. var expectedBook = SafeJsonConvert.DeserializeObject <ReviewedBook>(bookJson); DocumentIndexResult result = client.Documents.Index(IndexBatch.Upload(new[] { expectedBook })); Assert.Equal(1, result.Results.Count); AssertIndexActionSucceeded("123", result.Results[0], 201); SearchTestUtilities.WaitForIndexing(); Assert.Equal(1, client.Documents.Count()); ReviewedBook actualBook = client.Documents.Get <ReviewedBook>(expectedBook.ISBN); Assert.Equal(0, actualBook.Rating); actualBook.Rating = 5; Assert.Equal(expectedBook, actualBook); }); }
public void CanSetExplicitNullsInStaticallyTypedDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); // This is just so we can use the LoudHotel class instead of Hotel since it has per-property // NullValueHandling set. var resolver = new MyCustomContractResolver(); client.SerializationSettings.ContractResolver = resolver; client.DeserializationSettings.ContractResolver = resolver; var originalDoc = new LoudHotel() { HOTELID = "1", BASERATE = 199.0, DESCRIPTION = "Best hotel in town", DESCRIPTIONFRENCH = "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) }; var updatedDoc = new LoudHotel() { HOTELID = "1", BASERATE = 99.0, DESCRIPTION = null, CATEGORY = null, // This property doesn't have NullValueHandling.Include, so this should have no effect. TAGS = new[] { "pool", "view", "wifi" }, PARKINGINCLUDED = true, LASTRENOVATIONDATE = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8)), RATING = 4, LOCATION = null }; var expectedDoc = new LoudHotel() { HOTELID = "1", BASERATE = 99.0, DESCRIPTION = null, DESCRIPTIONFRENCH = "Meilleur hôtel en ville", HOTELNAME = "Fancy Stay", CATEGORY = "Luxury", TAGS = new[] { "pool", "view", "wifi" }, PARKINGINCLUDED = true, SMOKINGALLOWED = false, LASTRENOVATIONDATE = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8)), RATING = 4, LOCATION = null }; client.Documents.Index(IndexBatch.Upload(new[] { originalDoc })); SearchTestUtilities.WaitForIndexing(); client.Documents.Index(IndexBatch.Merge(new[] { updatedDoc })); SearchTestUtilities.WaitForIndexing(); LoudHotel actualDoc = client.Documents.Get <LoudHotel>("1"); Assert.Equal(expectedDoc, actualDoc); client.Documents.Index(IndexBatch.Upload(new[] { originalDoc })); SearchTestUtilities.WaitForIndexing(); actualDoc = client.Documents.Get <LoudHotel>("1"); Assert.Equal(originalDoc, actualDoc); }); }
public void CanMergeStaticallyTypedDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var originalDoc = 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) }; var updatedDoc = new Hotel() { HotelId = "1", BaseRate = 99.0, Description = null, Category = "Business", Tags = new[] { "pool", "view", "wifi" }, ParkingIncluded = true, LastRenovationDate = null, Rating = 4, Location = null }; var expectedDoc = new Hotel() { HotelId = "1", BaseRate = 99.0, Description = "Best hotel in town", DescriptionFr = "Meilleur hôtel en ville", HotelName = "Fancy Stay", Category = "Business", Tags = new[] { "pool", "view", "wifi" }, ParkingIncluded = true, SmokingAllowed = false, LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8)), Rating = 4, Location = GeographyPoint.Create(47.678581, -122.131577) }; client.Documents.Index(IndexBatch.MergeOrUpload(new[] { originalDoc })); SearchTestUtilities.WaitForIndexing(); client.Documents.Index(IndexBatch.Merge(new[] { updatedDoc })); SearchTestUtilities.WaitForIndexing(); Hotel actualDoc = client.Documents.Get <Hotel>("1"); Assert.Equal(expectedDoc, actualDoc); client.Documents.Index(IndexBatch.MergeOrUpload(new[] { originalDoc })); SearchTestUtilities.WaitForIndexing(); actualDoc = client.Documents.Get <Hotel>("1"); Assert.Equal(originalDoc, actualDoc); }); }
public void CanMergeDynamicDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var originalDoc = 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", 5L }, { "location", GeographyPoint.Create(47.678581, -122.131577) } }; var updatedDoc = new Document() { { "hotelId", "1" }, { "baseRate", 99.0 }, { "description", null }, { "category", "Business" }, { "tags", new[] { "pool", "view", "wifi" } }, { "parkingIncluded", true }, { "lastRenovationDate", null }, { "rating", 4L }, { "location", null } }; var expectedDoc = new Document() { { "hotelId", "1" }, { "baseRate", 99.0 }, { "description", null }, { "descriptionFr", "Meilleur hôtel en ville" }, { "hotelName", "Fancy Stay" }, { "category", "Business" }, { "tags", new[] { "pool", "view", "wifi" } }, { "parkingIncluded", true }, { "smokingAllowed", false }, { "lastRenovationDate", null }, { "rating", 4L }, { "location", null } }; client.Documents.Index(IndexBatch.MergeOrUpload(new[] { originalDoc })); SearchTestUtilities.WaitForIndexing(); client.Documents.Index(IndexBatch.Merge(new[] { updatedDoc })); SearchTestUtilities.WaitForIndexing(); Document actualDoc = client.Documents.Get("1"); Assert.Equal(expectedDoc, actualDoc); client.Documents.Index(IndexBatch.MergeOrUpload(new[] { originalDoc })); SearchTestUtilities.WaitForIndexing(); actualDoc = client.Documents.Get("1"); Assert.Equal(originalDoc, actualDoc); }); }
public void CanRoundtripBoundaryValues() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var expectedDocs = new[] { // Minimum values new Hotel() { HotelId = "1", BaseRate = Double.MinValue, Category = String.Empty, LastRenovationDate = DateTimeOffset.MinValue, Location = GeographyPoint.Create(-90, -180), // South pole, date line from the west ParkingIncluded = false, Rating = Int32.MinValue, Tags = new string[0] }, // Maximimum values new Hotel() { HotelId = "2", BaseRate = Double.MaxValue, Category = "test", // No meaningful string max since there is no length limit (other than payload size or term length). LastRenovationDate = DateTimeOffset.MaxValue, Location = GeographyPoint.Create(90, 180), // North pole, date line from the east ParkingIncluded = true, Rating = Int32.MaxValue, Tags = new string[] { "test" } // No meaningful string max; see above. }, // Other boundary values #1 new Hotel() { HotelId = "3", BaseRate = Double.NegativeInfinity, Category = null, LastRenovationDate = null, Location = GeographyPoint.Create(0, 0), // Equator, meridian ParkingIncluded = null, Rating = null, Tags = new string[0] }, // Other boundary values #2 new Hotel() { HotelId = "4", BaseRate = Double.PositiveInfinity, Location = null, Tags = new string[0] }, // Other boundary values #3 new Hotel() { HotelId = "5", BaseRate = Double.NaN, Tags = new string[0] }, // Other boundary values #4 new Hotel() { HotelId = "6", BaseRate = null, Tags = new string[0] } }; var batch = IndexBatch.Upload(expectedDocs); client.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); Hotel[] actualDocs = expectedDocs.Select(d => client.Documents.Get <Hotel>(d.HotelId)).ToArray(); for (int i = 0; i < actualDocs.Length; i++) { Assert.Equal(expectedDocs[i], actualDocs[i]); } }); }
public void CanIndexStaticallyTypedDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var batch = IndexBatch.New(new[] { IndexAction.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.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), //aka.ms/sre-codescan/disable Rating = 1, Location = GeographyPoint.Create(49.678581, -122.131577) }), IndexAction.Merge( new Hotel() { HotelId = "3", BaseRate = 279.99, Description = "Surprisingly expensive", LastRenovationDate = null }), IndexAction.Delete(new Hotel() { HotelId = "4" }), IndexAction.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, "3"); Assert.Equal(5, e.IndexingResults.Count); AssertIndexActionSucceeded("1", e.IndexingResults[0], 201); AssertIndexActionSucceeded("2", e.IndexingResults[1], 201); AssertIndexActionFailed("3", e.IndexingResults[2], "Document not found.", 404); AssertIndexActionSucceeded("4", e.IndexingResults[3], 200); AssertIndexActionSucceeded("5", e.IndexingResults[4], 201); SearchTestUtilities.WaitForIndexing(); Assert.Equal(3, client.Documents.Count()); }); }
protected void TestCanRoundTripNonNullableValueTypes() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = new Index() { Name = SearchTestUtilities.GenerateName(), Fields = new[] { new Field("Key", DataType.String) { IsKey = true }, new Field("Rating", DataType.Int32), new Field("Count", DataType.Int64), new Field("IsEnabled", DataType.Boolean), new Field("Ratio", DataType.Double), new Field("StartDate", DataType.DateTimeOffset), new Field("EndDate", DataType.DateTimeOffset) } }; serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); DateTimeOffset startDate = new DateTimeOffset(2015, 11, 24, 14, 01, 00, TimeSpan.FromHours(-8)); DateTime endDate = startDate.UtcDateTime + TimeSpan.FromDays(15); var doc1 = new NonNullableModel() { Key = "123", Count = 3, EndDate = endDate, IsEnabled = true, Rating = 5, Ratio = 3.14, StartDate = startDate }; var doc2 = new NonNullableModel() { Key = "456", Count = default(long), EndDate = default(DateTime), IsEnabled = default(bool), Rating = default(int), Ratio = default(double), StartDate = default(DateTimeOffset) }; var batch = IndexBatch.Upload(new[] { doc1, doc2 }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); DocumentSearchResult <NonNullableModel> response = indexClient.Documents.Search <NonNullableModel>("*"); Assert.Equal(2, response.Results.Count); Assert.Equal(doc1, response.Results[0].Document); Assert.Equal(doc2, response.Results[1].Document); }
public void EmptyDynamicObjectsInCollectionExpandedOnGetWhenCollectionFieldSelected() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var originalDoc = new Document() { ["hotelId"] = "1", ["rooms"] = new[] { new Document(), new Document() { ["baseRate"] = null, ["bedOptions"] = null, ["sleepsCount"] = null, ["smokingAllowed"] = null, ["tags"] = new object[0] } } }; var expectedDoc = new Document() { ["hotelId"] = "1", ["rooms"] = new[] { new Document() { ["description"] = null, ["descriptionFr"] = null, ["type"] = null, ["baseRate"] = null, ["bedOptions"] = null, ["sleepsCount"] = null, ["smokingAllowed"] = null, ["tags"] = new object[0] }, new Document() { ["description"] = null, ["descriptionFr"] = null, ["type"] = null, ["baseRate"] = null, ["bedOptions"] = null, ["sleepsCount"] = null, ["smokingAllowed"] = null, ["tags"] = new object[0] }, } }; var batch = IndexBatch.Upload(new[] { originalDoc }); client.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); Document actualDoc = client.Documents.Get("1", selectedFields: new[] { "hotelId", "rooms" }); Assert.Equal(expectedDoc, actualDoc); }); }
public void CanGetDynamicDocument() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var expectedDoc = new Document() { ["hotelId"] = "1", ["hotelName"] = "Secret Point Motel", ["description"] = "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.", ["descriptionFr"] = "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.", ["category"] = "Boutique", ["tags"] = new[] { "pool", "air conditioning", "concierge" }, ["parkingIncluded"] = false, ["smokingAllowed"] = true, ["lastRenovationDate"] = new DateTimeOffset(1970, 1, 18, 0, 0, 0, TimeSpan.FromHours(-5)), ["rating"] = 4L, ["location"] = GeographyPoint.Create(40.760586, -73.975403), ["address"] = new Document() { ["streetAddress"] = "677 5th Ave", ["city"] = "New York", ["stateProvince"] = "NY", ["country"] = "USA", ["postalCode"] = "10022" }, ["rooms"] = new[] { new Document() { ["description"] = "Budget Room, 1 Queen Bed (Cityside)", ["descriptionFr"] = "Chambre Économique, 1 grand lit (côté ville)", ["type"] = "Budget Room", ["baseRate"] = 9.69, ["bedOptions"] = "1 Queen Bed", ["sleepsCount"] = 2L, ["smokingAllowed"] = true, ["tags"] = new[] { "vcr/dvd" } }, new Document() { ["description"] = "Budget Room, 1 King Bed (Mountain View)", ["descriptionFr"] = "Chambre Économique, 1 très grand lit (Mountain View)", ["type"] = "Budget Room", ["baseRate"] = 8.09, ["bedOptions"] = "1 King Bed", ["sleepsCount"] = 2L, ["smokingAllowed"] = true, ["tags"] = new[] { "vcr/dvd", "jacuzzi tub" } } } }; var batch = IndexBatch.Upload(new[] { expectedDoc }); client.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); Document actualDoc = client.Documents.Get("1"); Assert.Equal(expectedDoc, actualDoc); }); }
protected void TestCanRoundTripNonNullableValueTypes() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); var index = new Index() { Name = SearchTestUtilities.GenerateName(), Fields = FieldBuilder.BuildForType <NonNullableModel>() }; serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); var startDate = new DateTimeOffset(2015, 11, 24, 14, 01, 00, TimeSpan.FromHours(-8)); DateTime endDate = startDate.UtcDateTime + TimeSpan.FromDays(15); var doc1 = new NonNullableModel() { Key = "123", Count = 3, EndDate = endDate, IsEnabled = true, Rating = 5, Ratio = 3.14, StartDate = startDate, TopLevelBucket = new Bucket() { BucketName = "A", Count = 12 }, Buckets = new[] { new Bucket() { BucketName = "B", Count = 20 }, new Bucket() { BucketName = "C", Count = 7 } } }; var doc2 = new NonNullableModel() { Key = "456", Count = default(long), EndDate = default(DateTime), IsEnabled = default(bool), Rating = default(int), Ratio = default(double), StartDate = default(DateTimeOffset), TopLevelBucket = default(Bucket), Buckets = new[] { default(Bucket) } }; var batch = IndexBatch.Upload(new[] { doc1, doc2 }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); DocumentSearchResult <NonNullableModel> response = indexClient.Documents.Search <NonNullableModel>("*"); Assert.Equal(2, response.Results.Count); Assert.Equal(doc1, response.Results[0].Document); Assert.Equal(doc2, response.Results[1].Document); }
public void CanIndexDynamicDocuments() { Run(() => { SearchIndexClient client = Data.GetSearchIndexClient(); var batch = IndexBatch.New(new[] { IndexAction.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) } }), IndexAction.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) }, //aka.ms/sre-codescan/disable { "rating", 1 }, { "location", GeographyPoint.Create(49.678581, -122.131577) } }), IndexAction.Merge( new Document() { { "hotelId", "3" }, { "baseRate", 279.99 }, { "description", "Surprisingly expensive" }, { "lastRenovationDate", null } }), IndexAction.Delete(keyName: "hotelId", keyValue: "4"), IndexAction.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, "3"); Assert.Equal(5, e.IndexingResults.Count); AssertIndexActionSucceeded("1", e.IndexingResults[0], 201); AssertIndexActionSucceeded("2", e.IndexingResults[1], 201); AssertIndexActionFailed("3", e.IndexingResults[2], "Document not found.", 404); AssertIndexActionSucceeded("4", e.IndexingResults[3], 200); AssertIndexActionSucceeded("5", e.IndexingResults[4], 201); SearchTestUtilities.WaitForIndexing(); Assert.Equal(3L, client.Documents.Count()); }); }