博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Search Service
阅读量:4613 次
发布时间:2019-06-09

本文共 5671 字,大约阅读时间需要 18 分钟。

Windows Search Service是一个全方位的托管云服务,可以允许开发者通过.Net SDK或者REST API多种多样的搜索服务.

如果你想开发一个搜索服务,那么你的服务应该包含以下组件:

 

最简单的创建服务的步骤如下:

1.Provisioning a service

2.Define a shema for index

3.Load documents to index

4.Query the index

今天我们将通过REST API和.NET SDK两种方式来讲述如何创建index,load document,query index等.

方式1:REST API,将会借助PostMan这个工具。PostMan是Googel Chrome浏览器的一个组件,你可以通过访问去下载.

在使用PostMan访问Search Service服务时我们需要先进行配置,点击Hearder按钮,输入如下值:

  • api-key:  [Admin Key]
  • Content-Type: application/json; charset=utf-8

1.Create an azure search index

URL:https://[SEARCH SERVICE].search.windows.net/indexes/trails?api-version=2015-02-28

Request Type:Put

Raw body content:

{

"name": "trails",
"fields": [
{
"name": "id", "type": "Edm.String", "key": true, "searchable": false},
{
"name": "name", "type": "Edm.String"},
{
"name": "county", "type": "Edm.String"},
{
"name": "elevation", "type": "Edm.Int32"},
{
"name": "location", "type": "Edm.GeographyPoint"} ]
}

Click Send.

2.Post documents to an azure search index

URL:https://[SEARCH SERVICE].windows.net/indexes/trails/docs/index?api-version=2015-02-28

Request Type:Post

Raw body content:

{

  "value": [
    {"@search.action": "upload", "id": "233358", "name": "Pacific Crest National Scenic Trail", "county": "San Diego", "elevation":1294, "location": { "type": "Point", "coordinates": [-120.802102,49.00021] }}
]
}

Click Send.

3.Query documents from an azure search index

URL:https://[SEARCH SERVICE].search.windows.net/indexes/trails/docs?api-version=2015-02-28&search=trail

Request Type:Get

Click Send.

方式2:.NET SDK

下面的Demo中包含:创建Index,上传Documents,查询Docuemnts.

1 class Program  2     {  3         static void Main(string[] args)  4         {  5             string searchServiceName = "Search Service Name";  6             string apiKey = "API-Key";  7   8             SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName,new SearchCredentials(apiKey));  9             DeleteHotelsIndexIfExists(serviceClient); 10             Console.WriteLine("{0}", "Create index...\n"); 11             CreateHotelsIndex(serviceClient); 12  13             Console.WriteLine("{0}", "Upload Document...\n"); 14             SearchIndexClient indexClient = serviceClient.Indexes.GetClient("hotels"); 15             UploadDocuments(indexClient); 16  17             Console.WriteLine("Search Document from index"); 18             SearchDocuments(indexClient, "Fancy Stay"); 19  20             Console.ReadKey(); 21         } 22  23         private static void DeleteHotelsIndexIfExists(SearchServiceClient serviceClient) 24         { 25             if(serviceClient.Indexes.Exists("hotels")) 26             { 27                 serviceClient.Indexes.Delete("hotels"); 28             } 29         } 30  31         private static void CreateHotelsIndex(SearchServiceClient serviceClient) 32         { 33             var definition = new Index() 34             { 35                 Name = "hotels", 36                 Fields = new[] 37                 { 38                     new Field("hotelId",DataType.String) { IsKey=true}, 39                     new Field("hotelName",DataType.String) { IsSearchable=true,IsFilterable=true}, 40                     new Field("baseRate",DataType.Double) {IsFilterable=true,IsSortable=true }, 41                     new Field("category",DataType.String) { IsSearchable=true,IsFilterable=true} 42  43                 } 44             }; 45  46             serviceClient.Indexes.Create(definition); 47         } 48  49         private static void UploadDocuments(SearchIndexClient indexClient) 50         { 51             var documents = 52                 new Hotel[] 53                 { 54                     new Hotel() 55                     { 56                         HotelId="1058-441", 57                         HotelName="Fancy Stay", 58                         BaseRate=199.0, 59                         category="Luxury" 60                     }, 61                     new Hotel() 62                     { 63                         HotelId="666-437", 64                         HotelName="Roach Motel", 65                         BaseRate=79.99, 66                         category="Budget" 67                     }, 68                     new Hotel() 69                     { 70                         HotelId="970-501", 71                         HotelName="Econo-Stay", 72                         BaseRate=199.0, 73                         category="Luxury" 74                     } 75                 }; 76          77             try 78             { 79                 indexClient.Documents.Index(IndexBatch.Create(documents.Select(doc => IndexAction.Create(doc)))); 80  81             } 82             catch (IndexBatchException e) 83             { 84                 Console.WriteLine("Failed to index some of the documents:{0}", string.Join(",", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))); 85             } 86             Thread.Sleep(2000); 87  88         } 89  90         private static void SearchDocuments(SearchIndexClient indexClient,string searchText,string filter=null) 91         { 92             var sp = new SearchParameters(); 93  94             if(!string.IsNullOrEmpty(filter)) 95             { 96                 sp.Filter = filter; 97             } 98  99             DocumentSearchResponse
response = indexClient.Documents.Search
(searchText,sp);100 foreach(SearchResult
result in response)101 {102 Console.WriteLine(result.Document);103 }104 }105 106 107 }
View Code

 

转载于:https://www.cnblogs.com/jessicaxia/p/4961541.html

你可能感兴趣的文章
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>
Linux免密码登陆
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>
我的2015---找寻真实的自己
查看>>
android编译遇到问题修改
查看>>
解决Ubuntu18.04.2远程桌面Xrdp登录蓝屏问题
查看>>
python_封装redis_hash方法
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>
eclipse 找不到 tomcat 的解决方案
查看>>
HDU 1890--Robotic Sort(Splay Tree)
查看>>