Gravatar for lautaro.bonetto@gmail.com

Question by Lautaro Bonetto, Aug 6, 2014 6:26 PM

Could I request JSON data on Front-end server from outside of JS Search interface?

Hi. The search page of my project is implemented using JS Framework of Coveo. We have a Front-End server that provide us the indexed data on JSON format.

When the search page renders the items on the HTML, other async JS functions are triggered to fill some pending data. We're taking an ID data on the rendered items and we're querying the JSON of the Front-End server again for other extra data.

To do this, we're using a regular AJAX qurey by JQuery. Like this one:

var exampleData = [];
function RetrieveSubBrands() {
    return $.ajax({
        type: 'GET',
        crossDomain: true,
        url: 'http://localhost:8080/Coveo/Rest/',
        data: { 'aq': '(@syscollection=="TestCollection")' },
        dataType: 'jsonp',
        success: function (jsonData) {
            $.each(jsonData.results, function (i, item) {
                exampleData[i] = {
                    Id: item.raw.id,
                    Image: item.raw.image,
                    Name: item.raw.name,
                    Link: item.raw.link
                };
            });
        },
        error: function () {
            alert('Error loading data from Coveo');
        }
    });
}

I'm pretty sure that there is a way to do this easily using the Coveo JS Framework, but I didn't find a place with some example about how to do this. I should retrieve more data from Coveo without change the main search that the user did.

Please, could you provide me an example about how to do this in that way?

Thanks

1 Reply
Gravatar for olamothe@coveo.com

Answer by olamothe, Aug 6, 2014 6:57 PM

Here is a general setup that should help you get going.

First, keep a reference to your Coveo Search Endpoint object that you created at the initialization of your page. To keep things simple, i'll put every variable in the global scope of the page. Obviously you could scope your stuff as you see fit :

var myEndpoint = new Coveo.Rest.SearchEndpoint({
    restUri : 'https://somewhere.com/rest/search',
}) 

Coveo.Rest.SearchEndpoint.endpoints["default"] = myEndpoint;
$('#search').coveo('init')

Now we could use the endpoint object to do something like this

var queryBuilder = new Coveo.Ui.QueryBuilder();

Check https://developers.coveo.com/display/JsSearch/QueryBuilder for all the info about this object Here we'll just add an advanced expression, but you could use it to build a very complicated query.

queryBuilder.advancedExpression.add('@syscollection=="TestCollection"');
var query = queryBuilder.build();
var deferred = myEndpoint.search(query); 

The search method returns a jquery deferred object http://api.jquery.com/category/deferred-object/

 deferred.done(function(response){
        //
          The parameters reponse and all it's properties are documented here 
          https://developers.coveo.com/display/SearchREST/Query+Results
          So you could loop on response.results, for example.
        //
    })
   deferred.fail(function(){
      //handle failures
   })
Gravatar for lautaro.bonetto@gmail.com

Comment by Lautaro Bonetto, Aug 6, 2014 7:02 PM

Thanks! This will help me alot.

Ask a question