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浏览器的一个组件,你可以通过访问Googel Chrome Store去下载.

在使用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.

 class Program
{
static void Main(string[] args)
{
string searchServiceName = "Search Service Name";
string apiKey = "API-Key"; SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName,new SearchCredentials(apiKey));
DeleteHotelsIndexIfExists(serviceClient);
Console.WriteLine("{0}", "Create index...\n");
CreateHotelsIndex(serviceClient); Console.WriteLine("{0}", "Upload Document...\n");
SearchIndexClient indexClient = serviceClient.Indexes.GetClient("hotels");
UploadDocuments(indexClient); Console.WriteLine("Search Document from index");
SearchDocuments(indexClient, "Fancy Stay"); Console.ReadKey();
} private static void DeleteHotelsIndexIfExists(SearchServiceClient serviceClient)
{
if(serviceClient.Indexes.Exists("hotels"))
{
serviceClient.Indexes.Delete("hotels");
}
} private static void CreateHotelsIndex(SearchServiceClient serviceClient)
{
var definition = new Index()
{
Name = "hotels",
Fields = new[]
{
new Field("hotelId",DataType.String) { IsKey=true},
new Field("hotelName",DataType.String) { IsSearchable=true,IsFilterable=true},
new Field("baseRate",DataType.Double) {IsFilterable=true,IsSortable=true },
new Field("category",DataType.String) { IsSearchable=true,IsFilterable=true} }
}; serviceClient.Indexes.Create(definition);
} private static void UploadDocuments(SearchIndexClient indexClient)
{
var documents =
new Hotel[]
{
new Hotel()
{
HotelId="1058-441",
HotelName="Fancy Stay",
BaseRate=199.0,
category="Luxury"
},
new Hotel()
{
HotelId="666-437",
HotelName="Roach Motel",
BaseRate=79.99,
category="Budget"
},
new Hotel()
{
HotelId="970-501",
HotelName="Econo-Stay",
BaseRate=199.0,
category="Luxury"
}
}; try
{
indexClient.Documents.Index(IndexBatch.Create(documents.Select(doc => IndexAction.Create(doc)))); }
catch (IndexBatchException e)
{
Console.WriteLine("Failed to index some of the documents:{0}", string.Join(",", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key)));
}
Thread.Sleep(); } private static void SearchDocuments(SearchIndexClient indexClient,string searchText,string filter=null)
{
var sp = new SearchParameters(); if(!string.IsNullOrEmpty(filter))
{
sp.Filter = filter;
} DocumentSearchResponse<Hotel> response = indexClient.Documents.Search<Hotel>(searchText,sp);
foreach(SearchResult<Hotel> result in response)
{
Console.WriteLine(result.Document);
}
} }

Windows Search Service的更多相关文章

  1. Windows Azure Service Bus Notification Hub推送通知

    前言 随着Windows Azure 在中国的正式落地,相信越来越多的人会体验到Windows Azure带来的强大和便利.在上一篇文章中, 我们介绍了如何利用Windows Azure中的Servi ...

  2. Windows Azure Service Bus Topics实现系统松散耦合

    前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...

  3. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  4. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  5. Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别

    <Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...

  6. Windows Azure Service Bus (5) 主题(Topic) 使用VS2013开发Service Bus Topic

    <Windows Azure Platform 系列文章目录> 项目文件,请在这里下载 在笔者之前的文章中Windows Azure Service Bus (1) 基础 介绍了Servi ...

  7. 除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法启动网站。目前,这两项服务均处于停止状态。

    win7 IIS 所有网站都停止了,启动提示: 除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法启动网站.目前,这两项服务均 ...

  8. paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]

    paip . 解决spring No unique bean of type   [com.mijie.homi.search.service.index.MoodUserIndexService] ...

  9. 关于windows的service编程

    最近需要学习下windows的service编程框架,查了下msdn发现不知所云.于是谷歌之,发现了一个非常不错的文章,重点推荐讲的非常详细,深入,看完之后基本上就能很清楚windows的servic ...

随机推荐

  1. Socket通信之Java学习(一)

    最近从一篇博客中看到了Socket的介绍,是阿蜜果姐姐的博文:http://www.blogjava.net/amigoxie/archive/2007/02/11/99331.html,学习了下. ...

  2. 持续集成环境Gitlab-CI的官方安装过程解析

    持续集成环境是一个非常重要的工具,在分工合作的项目中有着举足轻重的作用.公司最近要用Gitlab,需要配套的持续集成环境.研究了官方的文档,感觉官方的文档不是很明了.各种修改过后终于成功了.为了大家安 ...

  3. WCF服务属性注入基础设施

    WCF服务属性注入基础设施 WCF的服务的创建行为:使用默认构造函数创建WCF服务对象.如果我们想要在WCF内使用外部对象,最简单的方式就是把外部对象做成全局对象.然而这样的话会增加全局对象的数量,让 ...

  4. Ubuntu 12.04(所有ubuntu发行版都适用)sudo免输入密码

    首先执行以下命令(该命令用来修改 /etc/sudoers 文件): $ sudo gedit /etc/sudoers 然后把  %sudo    ALL=(ALL:ALL) ALL  这行注释掉, ...

  5. Hibernate缓存配置

    一级缓存 Hibernate的一级缓存是由Session提供的,因此它只存在于Session的生命周期中,当程序调用save(),update(),saveorupdate()等方法 及调用查询接口l ...

  6. Js-Html 前端系列--Ajax

    最近在做Web项目,用到很多Ajax,也遇到很多问题.下面与大家分享. Ajax传参 Ajax同步与异步 Ajax与谷歌浏览器 Ajax传参 我们一般传参有两种形式: $.ajax({ ... dat ...

  7. 关于 百度 Ueditor 上传图片时 打开文件夹的延迟问题

    在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...

  8. 集中式(CVS、SVN)VS分布式(Git)

    集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器.中央服务器就好比是一个图 ...

  9. CodeForces 718C Sasha and Array

    线段树. 线段树维护区间矩阵和,操作都是最简单的线段树.$lazy$标记不要记录乘了几次,直接记录乘了几次之后的矩阵就可以了,不然每次下传的时候再算一遍时间复杂度会提高. #pragma commen ...

  10. centos6.4、6.5、7.0环境下载及安装

    centos6.4.6.5.7.0下载地址: http://www.cnblogs.com/zhwl/p/3657958.html或 http://www.linuxdown.net/CentOS/2 ...