Gravatar for

Question by fcote, Dec 1, 2016 2:16 PM

Best practices for multiple calls to the Coveo Cloud API

This is more of an approach question than a problem I think. The main search page had the requirement of displaying 2 sub searches as well as an entire site search. To accomplish this I hooked 2 jQuery AJAX calls to the Coveo API in doneBuildingQueryEvent. What is happening is that the main query for the page, and one of the two sub queries is working fine, but the last one can take a noticeable amount of time to respond, which causes the area to randomly change results as they are looking at it long after it looks like the search is complete.

As a question, what is the best practice you recommend for this kind of situation where you need to make multiple calls? Is there a prefered wait time between calling the cloud API that I could implement?

A solution I can see is to hook into the queryStart, add an overlay to the page while the search is happening, and then remove it when the last query is done. Another option is to do the first query, then do the second when the other is done, then remove the overly.

What do you think?

1 Reply
Gravatar for

Answer by Simon, Dec 2, 2016 10:54 AM

Okay so a few things here. First of all, instead of coding your own AJAX, you could use our new Related query component (October 2016):

It makes it easier to play with the filters.

Now is there a best practices, not really. Actually the best option is to avoid multiple calls if possible. Returning everything at once and use tabs/facets and a custom result template to help the users navigate.

In general, you can optimized all your queries by reducing the size of your index. Make sure you only have relevant content by changing the crawler scope:

and by using inbound filtering when needed :

Also, if you use these multiple calls to recommend content and you use the Enterprise Cloud edition, then you can use the Coveo Machine Learning Recommendations instead, which is lighter than a full Coveo query :

Finally, one query being slow is strange. It could be due to the structure of the query itself. Wildcards is an example of operation which can be demanding.

Gravatar for

Comment by chrisarcand, Dec 6, 2016 9:10 AM

Using the related query worked really well to solve this problem. I missed that it was released.

Ask a question