22
#spsmad May 7 th , 2016 SharePoint Saturday Madrid Using Azure Search to build Office 365 search driven solutions José Carlos Rodríguez Avilés

Using Azure Search to build Office 365 search driven solutions

Embed Size (px)

Citation preview

#spsmad

May 7th, 2016SharePoint Saturday Madrid

Using Azure Search to build Office 365 search driven solutionsJosé Carlos Rodríguez Avilés

#spsmad

José Carlos Rodríguez AvilésSharePoint Software DeveloperUCI

http://elblogdelprogramador.wordpress.com@jcroavSoftware developer working mainly on SharePoint and very interested in Azure and how to use services available in this platform in my projects.

Gold sponsors

Silver sponsors

Bronze sponsors

Collaborate

RafflePlease, fill your SPS Madrid passport if you want to participate.You must get signature from sponsors and complete the poll.Give us the passport at 6:00 PM in the Auditorium room.You can win one Sphero BB-8 or a mini drone:

Using Azure Search to build Office 365 search driven solutions

What are we going to see in this session?

1. What about Azure Search Service?2. How to configure Azure Search in our subscription3. What about indexes and how to create them?4. How to index Office 365 content in Azure Search5. Using all of this in a SPA

What about Azure Search Service? This service recently left preview version Based on Elastic Search This service allow us create simple and complete search experiences

WE DON´T NEED TO KNOW ABOUT SEARCH INFRAESTRUCTURE

Available SDK.NET or API REST to work with it

How does Azure Search work?

Schema

Documents

1.- Creating indexes

2.- Population3.- Consume via API REST or SDK.NET

Demo 1:Provisioning Azure Search in our subscription

What about Indexes? Indexes are the main way to order and search content in Azure Search

It is similar to how to order registers in tables of databases

Each index has an schema (fields, field types, properties)

Furthermore, there are other elements inside an index (such as suggestion providers, scoring profiles, and so on…)

How can we create an index in Azure Search? There are three options:

Using SDK.NET for Azure Search Using API REST Using Azure portal

Making a Console Application to create indexes:1. Connect to Azure Search2. Test if index exists and delete it (best practice)3. Create index schema4. Populate the index

Creating an index with SDK.NETSearchServiceClient serviceClient = new SearchServiceClient(ServiceName, new SearchCredentials(ApiKey));

Index indexToAdd = new Index{Name = indexName,

Fields = new[]{ new Field("ContactId", DataType.String) {IsKey = true, IsRetrievable = true, IsFilterable = true}, new Field("Name",DataType.String) { IsRetrievable = true, IsSearchable=true, IsSortable=true, IsFilterable = true}, new Field("Job", DataType.String) { IsRetrievable = true, IsSearchable = true, IsSortable=true, IsFilterable = true}, new Field("Department", DataType.String) { IsRetrievable = true, IsSearchable = true, IsSortable=true, IsFilterable = true}, new Field("Email", DataType.String) { IsRetrievable = true, IsSearchable = true, IsSortable=true, IsFilterable = true} } }; if (serviceClient.Indexes.Exists(indexName)) serviceClient.Indexes.Delete(indexName);

serviceClient.Indexes.Create(indexToAdd);

Demo 2:Creating and index in Azure Search

Note: Azure SDK must be installed

Install-Package Microsoft.Azure.Search -Pre

Click to insert photo.

How to index Office 365 content in Azure Search? There are three important elements in Office 365 content population New Microsoft Graph API Azure WebJob App-Only authentication for Office 365

Configuring Azure Subscription

1. Creating certificate2. Create and configuring AAD permissions3. Load certificate in AAD4. Create webjob and configuring5. Load certificate in webjob

Demo 3:Configuring Azure and WebJob

Populating content in Azure WebJob Console Application to populate content iin Azure

Search1. Get authentication token 2. Call Microsoft Graph3. Create array with content to index4. Add information to Azure Search

Populating content from Office 365var client = new RestClient(GraphUrl);var request = new RestRequest("/v1.0/users/-/contacts", Method.GET);request.AddHeader("Authorization", "Bearer" + token.Result);request.AddHeader("Content-Type", "application/json");request.AddHeader("Accept", "application/json");

var response = client.Execute(request);var content = response.Content;

dynamic contacts = JObject.Parse(content);List<Contacts> contactsToPopulate = new List<Contacts>();int count = 1;

foreach (var contact in contacts.value){

//Create array}SearchIndexClient indexClient = GetSearchIndexClient();var batch = IndexBatch.Upload(contactsToPopulate.ToArray());indexClient.Documents.Index(batch);

Demo 4:Populating content with WebJob

Note: Azure SDK must be installed

Via Manage Nuget Package (ADAL)

Install-Package Microsoft.Azure.Search -PreInstall-Package RestSharp

Then what?

Example of using Azure Search in a SPA through API REST

Searching content with SDK.NETvar parameters = new SearchParameters();

List<string> orderby = new List<string>();List<string> highlight = new List<string>(); parameters.OrderBy = orderby.AsReadOnly();parameters.HighlightFields = highlight.AsReadOnly();parameters.HighlightPreTag = "<strong>";parameters.HighlightPostTag = "</strong>";

SearchServiceClient serviceClient = new SearchServiceClient(ServiceName, new SearchCredentials(ApiKey));SearchIndexClient indexClient = serviceClient.Indexes.GetClient(IndexName);DocumentSearchResult<T> response = indexClient.Documents.Search<T>(value, parameters);

foreach (SearchResult<T> result in response.Results){

//To do something}

References https://elblogdelprogramador.wordpress.com/2016/01/18/primeros-pasos-

con-azure-search-los-indices/ https://elblogdelprogramador.wordpress.com/2016/01/24/autenticacion-ap

p-only-para-usar-la-api-microsoft-graph-en-webjobs-de-azure/ https://elblogdelprogramador.wordpress.com/2016/02/02/indexando-conte

nido-en-el-servicio-azure-search/ https://azure.microsoft.com/en-us/documentation/articles/search-what-is-a

zure-search/ http://www.eliostruyf.com/building-daemon-or-service-app-with-the-micros

oft-graph-api/ https://azure.microsoft.com/en-us/documentation/articles/search-howto-do

tnet-sdk/ http://graph.microsoft.io/en-us/docs