Add query ranking expressions at query time
Add query ranking expressions at query time
A query ranking expression (QRE) is used to modify the ranking of search results.
Search results that match the QRE will see their relevance either increased or decreased, depending on the specified settings.
In other words, this allows you to modify the order of appearance of search results.
For example, you may want to see search results that contain the word sales in their title appear first, no matter what query is executed.
Using the JavaScript Search Framework, there are essentially two ways to attach a QRE to your queries. The following sections will demonstrate both of them.
|
|
Notes
|
Attaching a QRE using the init method
-
From the host where the JavaScript Search Framework package is deployed, using a text editor:
-
Open your search page in a text editor.
-
Under the node
<script type="text/javascript">, replace the line$('#search').coveo('init')with the following code:/* * Creates a QRE query using the $qre query extension * @expression {string} query expression to use for the QRE * @modifier {string} ranking modifier (-100 to 100) * @returns {string} the QRE query */ var createQre = function (expression, modifier) { var qre = ""; if (expression && modifier) { qre = "$qre(expression:'" + expression + "', modifier:'" + modifier + "')"; } return qre; }; // Create the QRE // returns "$qre(expression:'@title="sales report"', modifier:'75')" var qre = createQre("@title=\"sales report\"", "75"); // Initialize the search interface with a hidden expression that contains the QRE $('#search').coveo('init', { SearchInterface: { expression: qre } }); -
Adapt the pasted code to the desired behavior. You should only have to modify the QRE itself.
-
Save the file.
-
-
Load the modified search page in a web browser.
-
Validate that the ranking of search results was modified in such a way that those with the string
salesin their title appear first.
Attaching a QRE using an event handler
-
From the host where the JavaScript Search Framework package is deployed, using a text editor:
-
Open the search page HTML file.
-
Under the node
<script type="text/javascript">, replace the line$('#search').coveo('init')with the following code:/* * Creates a QRE query using the $qre query extension * @expression {string} query expression to use for the QRE * @modifier {string} ranking modifier (-100 to 100) * @returns {string} the QRE query */ var createQre = function (expression, modifier) { var qre = ""; if (expression && modifier) { qre = "$qre(expression:'" + expression + "', modifier:'" + modifier + "')"; } return qre; }; /* * Way to create advanced qre using contextual data like the user query in this example */ var setupQreOnQueryBuilt = function(e, args) { var queryBuilder = args.queryBuilder; var query = queryBuilder.build(); var qre = createQre("@title=sales", "75"); // Add the QRE query to the advanced expression queryBuilder.advancedExpression.add(qre); }; // Attach a handler function to the doneBuildingQuery event such that a QRE is run with the query. $('#search').on("doneBuildingQuery", setupQreOnQueryBuilt); // Initialize the search interface in a standard way. $('#search').coveo('init'); -
Adapt the pasted code to the desired behavior. You should only have to modify the QRE itself.
-
Save the file.
-
-
Load the modified search page in a web browser.
-
Validate that the ranking of search results was modified in such a way that those with the string
salesin their title appear first.