コード例 #1
0
        public void DeleteDocument()
        {
            const string testIndexName = "contents";
            var          searchService = new AzureSearchService(TestSettings.AzureSearchApiKey,
                                                                TestSettings.AzureSearchUrlPrefix, TestSettings.AzureSearchApiVersion);

            // Prepare test env: create a new index
            var result = searchService.CreateIndex(typeof(TestDocument), testIndexName);

            Assert.IsTrue(result.IsSuccessStatusCode, "Failed to create index. " + result.StatusCode);

            try
            {
                // Prepare test env (2): insert a document

                var itemsToAdd = GetData();
                searchService.AddContent(testIndexName, itemsToAdd);

                // Wait a bit, give AzureSearch some time to process
                Thread.Sleep(1000);

                // Test: Delete
                var count = searchService.GetCount(testIndexName);
                Assert.IsTrue(count >= 1, "Unable to get item count. ");
            }
            finally
            {
                // Cleanup: delete the newly created index
                searchService.DeleteIndex(testIndexName);
            }
        }
コード例 #2
0
        public void InsertDocumentInBulk()
        {
            const string testIndexName = "cx";
            var searchService = new AzureSearchService(TestSettings.AzureSearchApiKey,
                    TestSettings.AzureSearchUrlPrefix, TestSettings.AzureSearchApiVersion);

            // Prepare test env: create a new index
            var result = searchService.CreateIndex(typeof(TestDocument), testIndexName);
            Assert.IsTrue(result.IsSuccessStatusCode, "Failed to create index. " + result.StatusCode);

            try
            {
                var itemsToAdd = GetData(2);
                searchService.AddContent(testIndexName, itemsToAdd);

                // Wait a bit, give AzureSearch some time to process
                Thread.Sleep(1000);

                // Test: try to get document count
                var count = searchService.GetCount(testIndexName);
                Assert.IsTrue(count >= 1, "Unable to get item count. ");
            }
            finally
            {
                // Cleanup: delete the newly created index
                searchService.DeleteIndex(testIndexName);
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            const string testIndexName = "contents";

            // 1. Create an instance of the service to communicate with AzureSearch service
            var searchService = new AzureSearchService(TestSettings.AzureSearchApiKey, TestSettings.AzureSearchUrlPrefix, TestSettings.AzureSearchApiVersion);
            searchService.DeleteIndex(testIndexName);
            // 2. Create the index and check status
            var result = searchService.CreateIndex(typeof(Content), testIndexName);
            if (result.IsSuccessStatusCode)
            {
                Console.WriteLine("Index: {0} was created successfully.", testIndexName);
            }
            else
            {
                var task = result.Content.ReadAsStringAsync();
                task.Wait();

                var errorMessage = task.Result;
                Console.WriteLine();
            }

            //3. Prepare the list of documents/items to upload and upload in batch
            var jsonDocuments = GetData();
            var itemsToUpload = new List<object>();

            int count = 1;
            foreach (var doc in jsonDocuments)
            {
                count++;
                var item = new Content();

                (doc as JObject).FillObject(item);
                itemsToUpload.Add(item);

                if (count > TestSettings.AzureSearchBatchUpdateLimit)
                {
                    searchService.AddContent(testIndexName, itemsToUpload);

                    itemsToUpload.Clear();
                    count = 0;
                }
            }

            // Upload any remaining items
            if (itemsToUpload.Count > 0)
            {
                searchService.AddContent(testIndexName, itemsToUpload);
            }

            // 5. Check total document count
            //    Depending on service tier and server load, azure service may take some time
            //    to process the uploaded data. Let's wait for 1 sec before checking the count
            Thread.Sleep(1000);

            var totalDocuments = searchService.GetCount(testIndexName);
            Console.WriteLine("Total documents in index: " + totalDocuments);
            Console.WriteLine("");

            // 6. Run a search query
            var searchResult = searchService.Search<SearchResultItem>(testIndexName, "Windows 8");
            Console.WriteLine("Search result for term: Windows 8");
            foreach (SearchResultItem item in searchResult.value)
            {
                Console.WriteLine("Title: {0} \nScore: {1}\n", item.Title, item.SearchScore);
            }

            // Uncomment this line if you like to delete the index
            // searchService.DeleteIndex(testIndexName);

            Console.Read();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            const string testIndexName = "contents";

            // 1. Create an instance of the service to communicate with AzureSearch service
            var searchService = new AzureSearchService(TestSettings.AzureSearchApiKey, TestSettings.AzureSearchUrlPrefix, TestSettings.AzureSearchApiVersion);

            searchService.DeleteIndex(testIndexName);
            // 2. Create the index and check status
            var result = searchService.CreateIndex(typeof(Content), testIndexName);

            if (result.IsSuccessStatusCode)
            {
                Console.WriteLine("Index: {0} was created successfully.", testIndexName);
            }
            else
            {
                var task = result.Content.ReadAsStringAsync();
                task.Wait();

                var errorMessage = task.Result;
                Console.WriteLine();
            }

            //3. Prepare the list of documents/items to upload and upload in batch
            var jsonDocuments = GetData();
            var itemsToUpload = new List <object>();

            int count = 1;

            foreach (var doc in jsonDocuments)
            {
                count++;
                var item = new Content();

                (doc as JObject).FillObject(item);
                itemsToUpload.Add(item);

                if (count > TestSettings.AzureSearchBatchUpdateLimit)
                {
                    searchService.AddContent(testIndexName, itemsToUpload);

                    itemsToUpload.Clear();
                    count = 0;
                }
            }

            // Upload any remaining items
            if (itemsToUpload.Count > 0)
            {
                searchService.AddContent(testIndexName, itemsToUpload);
            }

            // 5. Check total document count
            //    Depending on service tier and server load, azure service may take some time
            //    to process the uploaded data. Let's wait for 1 sec before checking the count
            Thread.Sleep(1000);

            var totalDocuments = searchService.GetCount(testIndexName);

            Console.WriteLine("Total documents in index: " + totalDocuments);
            Console.WriteLine("");

            // 6. Run a search query
            var searchResult = searchService.Search <SearchResultItem>(testIndexName, "Windows 8");

            Console.WriteLine("Search result for term: Windows 8");
            foreach (SearchResultItem item in searchResult.value)
            {
                Console.WriteLine("Title: {0} \nScore: {1}\n", item.Title, item.SearchScore);
            }

            // Uncomment this line if you like to delete the index
            // searchService.DeleteIndex(testIndexName);


            Console.Read();
        }