Gravatar for colema18@gmail.com

Question by colema18, Aug 10, 2015 2:30 PM

Apply AdvancedExpression on Page Load (JS Framework)

I have successfully altered a user's query to use a specific Collection and to boost the Title field. The problem however is that this alteration is only occurring when the user interacts with the Search box and I need it to occur on page load as well.

With the javascript below when the page is loaded with http://url/#q=test I get 4,103 results back but if I then click the Search button I get 3,805 back because the filter is now being applied. Thoughts on what I am supposed to do to make it so on page load the collection is taken into account?

  $(document).ready(function () {
    Coveo.$('#search').on("doneBuildingQuery", setupQreOnQueryBuilt);
});
function coveoInit(endPoint) {
    Coveo.SearchEndpoint.configureOnPremiseEndpoint(endPoint);
    Coveo.$('#search').coveo('init');
}

var createQre = function (expression, modifier) {
    var qre = "";
    if (expression && modifier) {
        qre = "$qre(expression:'" + expression + "', modifier:'" + modifier + "')";
    }
    return qre;
};

var setupQreOnQueryBuilt = function (e, args) {
    var queryBuilder = args.queryBuilder;
    var query = queryBuilder.build();

    var collectionFilter = "@syssource==\"Kb\"";
    queryBuilder.advancedExpression.add(collectionFilter);

    //Add a heavier weight to the Title field
    var qre = createQre("@systitle=\"" + query.q + "\"", "75");
    queryBuilder.advancedExpression.add(qre);
};
1 Reply
Gravatar for olamothe@coveo.com

Answer by olamothe, Aug 10, 2015 3:27 PM

It's probably because of the order with which your functions are executed. Put a breakpoint on the
Coveo.$('#search').coveo('init');
line, and then another one on the
Coveo.$('#search').on("doneBuildingQuery", setupQreOnQueryBuilt);

You should see which one is executed first (probably the init one).
And since the first query is automatically triggered by default on initialization (meaning, inside the code that is executed when you call init), then your code that adds qre is not executed on the first query.

A simple fix would be to change your init function like this :

function coveoInit(endPoint) {
    Coveo.SearchEndpoint.configureOnPremiseEndpoint(endPoint);
    Coveo.$('#search').on("doneBuildingQuery", setupQreOnQueryBuilt);
    Coveo.$('#search').coveo('init');
}
Gravatar for colema18@gmail.com

Comment by colema18, Aug 10, 2015 3:30 PM

Perfect, thanks Olamothe

Ask a question