Search This Blog

Tuesday, March 1, 2016

SharePoint 2013: How to search using REST services - part 1

Search in SharePoint is most important and mostly used feature. Creating custom search controls through c# is complex  and requires farm level deployment. In the world of REST services farm solution is not correct approach. SharePoint 2013 has  REST services which enables you to work with SharePoint search. And these services are true friends of developers like me who scares of Search. In coming posts we will learn how to use Search REST services and its options.

REST URL to make search on text is:
/_api/search/query?querytext='text' 

If you open this REST URL in browser you will get the results like below. The response is huge and little bit complicated but if you read it carefully then you will understand all objects returned in response. So in this post we will learn how to read the response returned by Search REST service.

Let’s see the each section closely:

1. PrimaryQueryResult

This is first child element of ‘dada.d.Query’ , another one is ‘Properties’ will see it later. PrimaryQueryResult is the main element of search REST response. It contains following important elements: RefinementResults, ReleventResults and other elements are QueryId, QueryRuleId. Following screenshot shows the element.



2. RelevantResults

This is child element of ‘PrimaryQueryResult’ and it contains the search results. It contains search result in the form of array. To iterate through the results array you need to get like:data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results. Following code snippet iterate through it:

$.each(data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results, function () {
 }); 

RelevantResults also contains following elements: Properties, RowCount, TotalRows, TotalRowsIncludingDuplicates.
RowCount – This is count search results returned in response.
TotalRows – this represents number of matches found for the search text.
TotalRowsIncludingDuplicates - this represents number of matches found for the search text including duplicates.



3. RefinementResults

This element contains the list of refiners found for the search text. You can get them in the ‘Refiners’ element.
To get the refiners you need to construct the REST url like below:
/_api/search/query?querytext='yammer'&refiners='Write,DisplayAuthor,ContentType,FileExtension,FileType'.  



4. Refiners 

This tag contains the list of refiners returned in the result with their refiner tokens as shown in below screenshot. We will see more in it in next post.



So this is how the search result gets constructed. I have explained above only those elements which are important while development. In next post we will see how to get search results and bind them on screen.

No comments:

Post a Comment