Gravatar for wvuong@captechconsulting.com

Question by wvuong, Dec 29, 2015 11:16 AM

Modify Results Based on Input via Query Extension

I have created my own pipeline and implemented my own query extension. Based on parameters I pass in to the query extension I would like to modify a field of my results.

i.e.

  • Implemented query extension $foobar(inputA: number)
  • Store the number as a global var e.g. _inputA = number;
  • In Coveo.onPostProcessResults() I use the _inputA to do some calculation and store it in a field.

Is this the appropriate way to achieve this functionality? My worry is that since this one pipeline handles all incoming requests data will be out of sync. Consider the following example.

  1. Request A hits pipeline, calling $foobar(inputA: 1), _inputA is set to 1.
  2. Request B hits pipeline, calling $foobar(inputA: 2), _inputA is set to 2.
  3. Request A continues execution calling Coveo.OnPostProcessResults().
  4. Now I am calculating the result for request A using the wrong _inputA value, because Request B has set it.

Is this the correct understanding of pipelines? Or does each request finish before a new one begins?

Gravatar for glaporte@coveo.com

Comment by Gregory Laporte, Dec 29, 2015 2:41 PM

Hi,

It is indeed the way it works. Requests are processed in parallel.

We advice against using the global scope, because all the query pipelines are designed to be stateless. Using the global scope to store some state can prevent you from load balancing correctly and could cause issues.

Starting on November, there is a way to pass contextual information as part of your query. See: https://developers.coveo.com/display/public/SearchREST/Query+Parameters#QueryParameters-context

Then, you can leverage this context in your javascript code using the Context global object. https://developers.coveo.com/display/public/SearchREST/Context

That way, your code would remain stateless.

Gravatar for wvuong@captechconsulting.com

Comment by wvuong, Dec 29, 2015 3:21 PM

Alright, I'll try this and let you know how it goes. Is this the correct way to mimic the functionality of the $qf standard extension in terms of creating a query extension that puts a value dynamically into a field that exists just for the instance of the query?

0 Reply
Ask a question