Gravatar for spatten@getfishtank.ca

Question by sebastianpatten, Oct 13, 2015 6:12 PM

How can I consume the REST service via JavaScript on a page with no Coveo Controls

I'd like to know my options for consuming the REST service via pure JavaScript on a page with no Coveo Controls.

From reading the documentation and observing the XHR requests in the browser it looks like I need the following things for the request to work:

  • In the POST body an aq key and @syssource value to identify the index to use
  • A way of constructing the Coveo field name
  • Possibly a way of constructing my query, however I believe I might be able to hand roll it myself

How do I get at these values via JavaScript?

Would it be creating a CoveoSearchInterface in memory and somehow inspecting it to find the @syssource? Is there a client side method for generating the Coveo field name? Are the above concerns taken care for me in some JavaScript class that I haven't stumbed upon yet? QueryBuilder? QueryController?

Many thanks

2 Replies
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Oct 14, 2015 8:39 AM

Hi Sebastian,

I guess that you are using Coveo for Sitecore since you talk about field name translation and finding the syssource field value to use to target a specific Sitecore index.

The translation of Sitecore field names to Coveo field names is done on the server side while the search page is rendered. The BaseModel class contains two ToCoveoFieldName methods that calls similar methods on an instance of the IFieldHelper interface (FieldHelper class). This class can help you to implement field translation in your custom solution.

To identify the right syssource field value to use, Coveo for Sitecore uses the Sitecore context database, finds the first Sitecore index (managed by Coveo) that indexes this database and get its SourceName. All that is done by the SitecoreHelper.GetSearchIndex(IIndexable) method on the server side again. The IIndexable object to pass to the method must be the Sitecore context current item.

The queries are part built on the server side and part on the client side with the Coveo JavaScript Search Framework classes like the [ExpressionBuilder][1].

I hope this helps you to bake your own search UI framework

Gravatar for mlaporte@coveo.com

Answer by Martin Laporte, Oct 14, 2015 3:17 AM

The REST API used by the JS UI is documented here: https://developers.coveo.com/display/SearchREST/Invoking+the+REST+Search+API

You do not need to specify a source, by default the whole index is queried (with proper security applied, of course). The most often use arguments are q for passing the user entered query, aq for the "advanced" part (typically filters and so on), etc.

Ask a question