Query Function

This topic describes the members of the structure defining a single Query Function to perform during a request. A Query Function is a mathematical expression that you run during a request to calculate a value for each result at query time, and output its result in a dynamic field.

The Query Function syntax is:

function:'[your function to test]', fieldName:'[field storing your function result]'

The @syssize field contains the search results item file size in bytes. You want to create a field that shows the value in KB. You use the following Query Function:

function:'@syssize/1024', fieldName: 'filesizekb'

The field @filesizekb is created at query time and contains the calculated value. You could then use this value and show it in your search result template.

You can use Query Functions through the $qf query extension (see Standard Query Extensions) that you can quickly test in a Coveo search box.

$qf(function:'@syssize/1024', fieldName:'filesizekb')

You can also chain multiple query functions and use the result of one in another.

You want to get the distance between two coordinates in kilometers:

function: 'dist(@latitude, @longitude, 46.8167, -71.2167)', fieldName: 'distance'
function: '@distance / 1000', fieldName: 'distanceinkm'

You can use a query function generated field almost anywhere you would use a normal numerical field. One notable exception is automatic facet ranges in the Coveo JavaScript Search Framework (see Coveo FacetRange Component (CoveoFacetRange)). If you need to create facet ranges on a query function generated field, you must specify those ranges at query time, because the indexer cannot determine them automatically.


The following list contains all parameters available when executing a query function using the REST Search API.


The function to execute

The mathematical expression that calculates the value to inject in the field.


The Coveo query function supports the syntax of the C++ Mathematical Expression Library ExprTk (see ExprTk Library).

Be aware that the following statements have been disabled:

  • if / else
  • while
  • repeat until
  • switch

You have a @price field on your items. You need a @discountedprice field that contains the price with a 10% discount. Your function should look like this:

(function:"@price*0.9", fieldName:"discountPrice")


The dist function was added to the supported query functions, on top of the ExprTK library functions. The dist function calculates the distance as the crow flies between two sets of latitude and longitude coordinates (using the Levenshtein distance algorithm) useful in geolocalization applications (see Getting Geolocalized Results in a JavaScript Search Page.

The dist function syntax is:

dist(latitude1, longitude1, latitude2, longitude2)

The returned value is in meters.

Your index includes geographic latitude and longitude fields for all kinds of shops. You want to calculate a distance between the current user geographic location and a restaurant location. You use the dist query function to calculate and inject a distance field at query time.

/rest/search/?q=restaurants&queryFunctions=[{"function":"dist(@latitude, @longitude, 46.8167, -71.2167)","fieldName":"distance"}]

In the response, the distance field is added.

  "totalCount" : 2,
  "duration" : 15,
  "indexDuration" : 10,
  "exception" : {
    "code" : "1",
    "context" : ""
  "queryCorrections" : [ ],
  "results" : [ {
    "title" : "Family Deli",
    "raw" : [
      "sysuri" : "http://familydeli.com",
      "distance" : "300"


Name of the field that will hold the results

This specifies the name of the field to be generated at query time. This field can be used as a facet or as a computed field.

The field name can only contain alphanumerical characters, and must not already exist in your index.

Performance Issues

To prevent performance issues, every computed field referenced in the function must be stored in memory. Not doing so may lead to serious performance issues, with queries that might take over a second, depending on your number of items.

This is because query functions are executed on all accessible items. If the computed field is not stored in memory, it might have to be loaded from the disk, which can significantly slow the query.

Solve the Issue in Cloud V2

When using Coveo Cloud V2, for instance in a Coveo for Sitecore Cloud edition environment, follow these steps:

  1. Access the Coveo Cloud Platform administration console.
  2. Under Content, select Fields.
  3. Search for the field you wish to store in cache.
  4. Select your desired field, and click Edit.
  5. In the Edit a Field window, select Advanced Settings.
  6. Select Use cache for numeric queries.

    If your numerical field is stored as a string, you may need to change its Type, for instance to Long.

  7. Select Save Field to save your changes.

Solve the Issue in Cloud V1

In a Cloud V1 environment, for instance when using Coveo for Salesforce, you cannot manually change this setting. For security reasons, the ability to change the configuration file has been limited to selected Coveo employees. To change this setting, contact Coveo Support.


A few query exceptions can be returned when using Query Functions:

  • InvalidQueryFunction,          Value: 40, Using the '@' character without any field name.
  • InvalidQueryFunctionField,     Value: 41, Using a field that doesn't exist in the index, nor in any prior query function.
  • InvalidQueryFunctionFieldType: Value: 42, Using a field that is not a numerical field.
  • InvalidQueryFunctionSyntax:    Value: 43, Syntax error in the expression.