# AliyunOpenSearch4Net **Repository Path**: kerryjiang/AliyunOpenSearch4Net ## Basic Information - **Project Name**: AliyunOpenSearch4Net - **Description**: No description available - **Primary Language**: C# - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2015-11-17 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## AliyunOpenSearch4Net [![Build Status](https://travis-ci.org/kerryjiang/AliyunOpenSearch4Net.svg?branch=master)](https://travis-ci.org/kerryjiang/AliyunOpenSearch4Net) [![NuGet Version](https://img.shields.io/nuget/v/AliyunOpenSearch4Net.svg?style=flat)](https://www.nuget.org/packages/AliyunOpenSearch4Net/) [![NuGet Downloads](https://img.shields.io/nuget/dt/AliyunOpenSearch4Net.svg?style=flat)](https://www.nuget.org/packages/AliyunOpenSearch4Net/) AliyunOpenSearch4Net aimed to provide a client tool for you to use Aliyun OpenSearch service in .NET environment. ### Runtime Requirements Any one in the below list: * .NET 2.0 * .NET 3.5 * .NET 4.0 * .NET 4.5 or higher * Portable Class Library (portable-net40+sl50+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10): supports Windows Phone, Xamarin.iOS and Xamarin.Android. ### Development Tool Any one in the below list: * Visual Studio 2010 * Visual Studio 2015 ### How to Build? There are many different solution files (*.sln) for different .NET framework versions and targets: * AliyunOpenSearch4Net.Net20.sln: for .NET 2.0 * AliyunOpenSearch4Net.Net35.sln: for .NET 3.5 * AliyunOpenSearch4Net.Net40.sln: for .NET 4.0 (with test project) * AliyunOpenSearch4Net.Net45.sln: for .NET 4.5 (with test project) * AliyunOpenSearch4Net.VS2010.sln : Visual Studio 2010, .NET 4.0 (with test project) * AliyunOpenSearch4Net.Portable.sln : Visual Studio 2015, Portable Class Library ### Third party dependencies (NuGet organized): This SDK references JSON.NET (the most famous .NET open source JSON library) and other libraries (portable project references more libraries than JSON.NET) using NuGet. So please restore the references using NuGet when you want to build the solution first time after you download the code. ### Usages #### Instantiate a client. After you instantiate your client, you can invoke the methods provided by the client to access the Aliyun OpenSearch service. // instantiate a client with access key ID and access key secret // the default connecting service zone is hangzhou var client = new AliyunClient("testkeyid", "testkeysecret"); // if you want to connect to another zone, you pass in your desired host var client = new AliyunClient("http://opensearch-cn-hangzhou.aliyuncs.com", "testkeyid", "testkeysecret"); #### List apps with page and page size var response = client.ListApp(1, 100); if (response.Status == ApiStatus.OK) { // print the apps' count Console.WriteLine(response.Result.Length); // print the first app's name Console.WriteLine(response.Result[0].Name); } #### Create a new app var createRespnse = client.CreateApp("TestApp", "builtin_bbs"); if (response.Status == ApiStatus.OK) { Console.WriteLine("Created successfully"); } #### Delete an existing app var createRespnse = client.DeleteApp("TestApp"); if (response.Status == ApiStatus.OK) { Console.WriteLine("Deleted successfully"); } #### Access errors var response = client.DeleteApp("NotFoundApp"); if (response.Status == ApiStatus.FAIL && response.Errors.Length > 0) { // print the first error var firstError = response.Errors[0]; Console.WriteLine("{0}: {1}", firstError.Code, firstError.Message); } #### Push data var items = new List(); var item = new DataItem(DataCommand.add); item.Fields .Add("id", Guid.NewGuid().ToString()) .Add("type_id", 1) .Add("cat_id", 1) .Add("title", "Aliyun OpenSearch SDK") .Add("body", "Aliyun OpenSearch SDK rocks"); items.Add(item); item = new DataItem(DataCommand.add); item.Fields .Add("id", Guid.NewGuid().ToString()) .Add("type_id", 1) .Add("cat_id", 1) .Add("title", "Aliyun OpenSearch SDK 2") .Add("body", "Aliyun OpenSearch SDK rocks 2"); items.Add(item); var response = client.UploadData("TestApp", "main", items); if (response.Status == ApiStatus.OK) { Console.WriteLine("Pushed successfully"); } #### Search records var response = client.Search("TestApp", "query=default:'Aliyun'"); if (response.Status == ApiStatus.OK) { // print found records count Console.WriteLine("{0} records are found", response.Result.Items.Count); } #### Asynchronous API In .NET 4.0, you can use Task to make your invoking asynchronously: var listTask = client.ListAppAsync(1, 100); listTask.ContinueWith(t => { if(t.Result.Result == ApiStatus.OK) { Console.WriteLine("Get app list successfully"); } }); In .NET 4.5 or above, you can use the keyword "await" to simplify your asynchronous operations: var response = await client.ListAppAsync(1, 100); if(response.Result == ApiStatus.OK) { Console.WriteLine("Get app list successfully"); }