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); } }
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); } }
public void GetIndex() { const string testIndexName = "con94"; 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); // Test: try to get the newly created index and verify structure var indexDefinitionInJson = searchService.GetIndex(testIndexName); var jsonObject = JsonConvert.DeserializeObject(indexDefinitionInJson, typeof(ExpandoObject)); var data = jsonObject as IDictionary <string, object>; Assert.IsNotNull(data, "Failed to retrieve index information"); // A valid response will contain the following six properties: // @odata.context, name, fields, scoringProfile, defaultScoringProfile, corsOptions Assert.IsTrue(data.Keys.Count == 6, "Failed to retrieve index information"); // Cleanup: delete the newly created index searchService.DeleteIndex(testIndexName); }
public void CreateIndex() { const string testIndexName = "con92"; var searchService = new AzureSearchService(TestSettings.AzureSearchApiKey, TestSettings.AzureSearchUrlPrefix, TestSettings.AzureSearchApiVersion); // Test: verify index creation process var result = searchService.CreateIndex(typeof(TestDocument), testIndexName); Assert.IsTrue(result.IsSuccessStatusCode, "Failed to create index. " + result.StatusCode); // Cleanup: delete the newly created index searchService.DeleteIndex(testIndexName); }
public void DeleteIndex() { const string testIndexName = "con94"; var searchService = new AzureSearchService(TestSettings.AzureSearchApiKey, TestSettings.AzureSearchUrlPrefix, TestSettings.AzureSearchApiVersion); // Prepare test env // 1. create a new index var result = searchService.CreateIndex(typeof(TestDocument), testIndexName); Assert.IsTrue(result.IsSuccessStatusCode, "Failed to create index. " + result.StatusCode); // Test: try to delete the newly created index var deleteResult = searchService.DeleteIndex(testIndexName); Assert.IsTrue(result.IsSuccessStatusCode, "Failed to delete index. " + result.StatusCode); }
public void GetIndex() { const string testIndexName = "con94"; 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); // Test: try to get the newly created index and verify structure var indexDefinitionInJson = searchService.GetIndex(testIndexName); var jsonObject = JsonConvert.DeserializeObject(indexDefinitionInJson, typeof(ExpandoObject)); var data = jsonObject as IDictionary<string, object>; Assert.IsNotNull(data, "Failed to retrieve index information"); // A valid response will contain the following six properties: // @odata.context, name, fields, scoringProfile, defaultScoringProfile, corsOptions Assert.IsTrue(data.Keys.Count == 6, "Failed to retrieve index information"); // Cleanup: delete the newly created index searchService.DeleteIndex(testIndexName); }
/// <summary> /// Clears the search all. /// </summary> private static void ClearSearchAll() { searchService.DeleteIndex(ApplicationConstants.SearchIndex); }
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(); }
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(); }