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);
}
} }