Coveo REST Search API Using Post Method

Coveo REST Search API Using Post Method

Coveo Rest API is a simple REST interface for invoking, exposing and manipulating the search based on the different filtering and search criteria.

Coveo provide both cloud and on-permises indexing Where Coveo cluod customer can accessed the REST API throught Coveo Cloud Plateform. But for on-permise indexing, we need to deploy the Search API locally. We are using the Coveo Cloud Platform and this blog is based on it. And when we are taking about the Coveo Cloud Platform then REST API is available to all users through a single URL – https://cloudplatform.coveo.com/rest/search/v2

Coveo provide many means of client authentication, but we will use the Authorization Key for the authenticating the client. We can perform both GET and POST method for the search. We can pass multiple supporting parameter either in the query string or in the body of the POST method as JSON format.

A typical POST method example in post man for the coveo cloud plateform is as follow –

postman

Where POST URL is https://cloudplatform.coveo.com/rest/search/v2, and authorization is done using Authentication key. Different parameters for the search such as organizationid, query, advance query etc. are passed in the body as content type – application/json.

A C# static class for making query to the coveo cloud plate form is as follow –

using System;

using System.IO;

using System.Net;

using System.Text;

using Sitecore.Diagnostics;

using MyStringExtentions

 

namespace MyNameSpace

{

Public static class MakeCoveoRequest

{

public static string MakeRequest(string query)

{

var result = string.Empty;

var CoveoRestUrl = “https://cloudplatform.coveo.com/rest/search/v2”;

 

var coveoUri = new Uri(CoveoRestUrl);

 

var httpWebRequest = WebRequest.Create(coveoUri) as HttpWebRequest;

if (httpWebRequest == null) return result;

 

try

{

var postData = Encoding.UTF8.GetBytes(query);

httpWebRequest.Method = “POST”;

httpWebRequest.PreAuthenticate = true;

httpWebRequest.Headers.Add(“Authorization”, “Bearer MyAuthorizationKey”);

httpWebRequest.ContentType = “application/json”;

httpWebRequest.ContentLength = postData.Length;

using (var dataStream = httpWebRequest.GetRequestStream())

{

dataStream.Write(postData, 0, postData.Length);

}

var myWebResponse = httpWebRequest.GetResponse();

var responseStream = myWebResponse.GetResponseStream();

if (responseStream == null) return null;

var myStreamReader = new StreamReader(responseStream, Encoding.Default);

result = myStreamReader.ReadToEnd();

responseStream.Close();

myWebResponse.Close();

}

catch (WebException ex)

{

Log.Error(“Message” + ex.Message, ex);

}

 

return result;

}

}

}

 

Thanks

 

Advertisements
Coveo for Sitecore : Intalligent Search for your website

Coveo for Sitecore : Intalligent Search for your website

Coveo is a provider of intelligent and predictive search technologies and as name says Coveo for sitecore is same for the Sitecore plateform. So what it offers? Well it makes your website more responsive, proactive and relevant for every visitor. it accesses site content and information, recommends the most relevant information and perform intelligent operation to improves results for future visits and visitors.

Let first understand the Architecture of Sitecore and Coveo with Sitecore. I have borrow following images from coveo’s developer site, where in first image you can see the basic architecture of sitecore

basic Sitecore architecture

And in second image – improve architecture of sitecore with coveo where each level have coveo block.

coveo improved sitecore architecture

Coveo installation – installation of coveo is easy and can be done by performing setups from below link –

https://developers.coveo.com/display/public/SitecoreV4/Setting+Up+Coveo+for+Sitecore

Coveo indexing – coveo provide both Cloud or On-Premises Indexes, on-permises is free edition where cloud provide one month trail. There are lots of advantage of using cloud indexing such as auto scaling, machine learning, security handling etc.

Coveo provides lots of out of box features which makes sitecore super powerful. A big rule engine provide more flexibility and power, supports MVC, provide REST API to make it plateform independent for the search. It also supports Linq API which makes backend user’s life easy.

There are lots of blog already on the Coveo about its introduction, setup and features, reason behind writing this blog is that, it is prologue for my next series of blogs.