예제 #1
0
        public void CanIndexAndRetrieveWithCamelCaseContractResolver()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index = Book.DefineIndex(useCamelCase: true);
                serviceClient.Indexes.Create(index);

                SearchIndexClient client = Data.GetSearchIndexClient(index.Name);
                client.SerializationSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

                var expectedBook = new Book()
                {
                    ISBN = "123", Title = "The Hobbit", Author = "J.R.R. Tolkien"
                };
                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());

                Book actualBook = client.Documents.Get <Book>(expectedBook.ISBN);

                Assert.Equal(expectedBook, actualBook);
            });
        }
예제 #2
0
        protected void TestCanSearchWithDateTimeInStaticModel()
        {
            SearchServiceClient serviceClient = Data.GetSearchServiceClient();

            Index index = Book.DefineIndex();

            serviceClient.Indexes.Create(index);
            SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

            var tolkien = new Author()
            {
                FirstName = "J.R.R.", LastName = "Tolkien"
            };
            var doc1 = new Book()
            {
                ISBN = "123", Title = "Lord of the Rings", Author = tolkien
            };
            var doc2 = new Book()
            {
                ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18)
            };
            var batch = IndexBatch.Upload(new[] { doc1, doc2 });

            indexClient.Documents.Index(batch);
            SearchTestUtilities.WaitForIndexing();

            DocumentSearchResult <Book> response = indexClient.Documents.Search <Book>("War and Peace");

            Assert.Equal(1, response.Results.Count);
            Assert.Equal(doc2, response.Results[0].Document);
        }
예제 #3
0
        public void CanIndexWithPascalCaseFields()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index = Book.DefineIndex();
                serviceClient.Indexes.Create(index);
                SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

                var batch =
                    IndexBatch.Upload(new[]
                {
                    new Book()
                    {
                        ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien"
                    }
                });

                DocumentIndexResult indexResponse = indexClient.Documents.Index(batch);

                Assert.Equal(1, indexResponse.Results.Count);
                AssertIndexActionSucceeded("123", indexResponse.Results[0], 201);
            });
        }
예제 #4
0
        private void TestCanSearchWithCustomConverter <T>(Action <SearchIndexClient> customizeSettings = null)
            where T : CustomBook, 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 T()
            {
                InternationalStandardBookNumber = "123",
                Name            = "Lord of the Rings",
                AuthorName      = "J.R.R. Tolkien",
                PublishDateTime = new DateTime(1954, 7, 29)
            };

            var batch = IndexBatch.Upload(new[] { doc });

            indexClient.Documents.Index(batch);
            SearchTestUtilities.WaitForIndexing();

            DocumentSearchResult <T> response = indexClient.Documents.Search <T>("*");

            Assert.Equal(1, response.Results.Count);
            Assert.Equal(doc, response.Results[0].Document);
        }
예제 #5
0
        public void CanGetStaticallyTypedDocumentWithPascalCaseFields()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index = Book.DefineIndex();
                serviceClient.Indexes.Create(index);
                SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

                var expectedDoc =
                    new Book()
                {
                    ISBN   = "123",
                    Title  = "Lord of the Rings",
                    Author = new Author()
                    {
                        FirstName = "J.R.R.", LastName = "Tolkien"
                    }
                };

                var batch = IndexBatch.Upload(new[] { expectedDoc });
                indexClient.Documents.Index(batch);

                Book actualDoc = indexClient.Documents.Get <Book>("123");
                Assert.Equal(expectedDoc, actualDoc);
            });
        }
예제 #6
0
        protected void TestCanSuggestWithDateTimeInStaticModel()
        {
            SearchServiceClient serviceClient = Data.GetSearchServiceClient();

            Index index = Book.DefineIndex();

            serviceClient.Indexes.Create(index);
            SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

            var doc1 = new Book()
            {
                ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien"
            };
            var doc2 = new Book()
            {
                ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18)
            };
            var batch = IndexBatch.Upload(new[] { doc1, doc2 });

            indexClient.Documents.Index(batch);
            SearchTestUtilities.WaitForIndexing();

            var parameters = new SuggestParameters()
            {
                Select = new[] { "ISBN", "Title", "PublishDate" }
            };
            DocumentSuggestResult <Book> response = indexClient.Documents.Suggest <Book>("War", "sg", parameters);

            Assert.Equal(1, response.Results.Count);
            Assert.Equal(doc2, response.Results[0].Document);
        }
예제 #7
0
        private void TestCanIndexAndRetrieveWithCustomConverter <T>(Action <SearchIndexClient> customizeSettings = null)
            where T : CustomBook, new()
        {
            customizeSettings = customizeSettings ?? (client => { });
            SearchServiceClient serviceClient = Data.GetSearchServiceClient();

            Index index = Book.DefineIndex();

            serviceClient.Indexes.Create(index);

            SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

            customizeSettings(indexClient);

            // Pre-index the document so we can test that Merge works with the custom converter.
            var firstBook = new Book()
            {
                ISBN        = "123",
                Title       = "The Hobbit",
                Author      = "J.R.R. Tolkeen",          // Misspelled on purpose.
                PublishDate = new DateTime(1945, 09, 21) // Incorrect date on purpose (should be 1937).
            };

            DocumentIndexResult result = indexClient.Documents.Index(IndexBatch.Upload(new[] { firstBook }));

            Assert.Equal(1, result.Results.Count);
            AssertIndexActionSucceeded("123", result.Results[0], 201);

            SearchTestUtilities.WaitForIndexing();

            var expectedBook = new T()
            {
                InternationalStandardBookNumber = "123",
                AuthorName      = "J.R.R. Tolkien",
                PublishDateTime = new DateTime(1937, 09, 21)
            };

            result = indexClient.Documents.Index(IndexBatch.Merge(new[] { expectedBook }));

            Assert.Equal(1, result.Results.Count);
            AssertIndexActionSucceeded("123", result.Results[0], 200);

            SearchTestUtilities.WaitForIndexing();

            Assert.Equal(1, indexClient.Documents.Count());

            T actualBook = indexClient.Documents.Get <T>(expectedBook.InternationalStandardBookNumber);

            Assert.Equal(expectedBook, actualBook);
        }
        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);
        }
예제 #9
0
        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"]);
            });
        }
예제 #10
0
        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);
            });
        }
예제 #11
0
        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);
            });
        }