Gravatar for chris.ruderschmidt@americaneagle.com

Question by Chris Ruderschmidt, Sep 1, 2015 12:35 PM

How do I enable redirects from a coveo search?

I am currently doing the following:

  1. On $('.CoveoSearchButton').click() or $('.CoveoQueryBox').keydown() (enter), calling a function Pass
  2. From that function, calling a webservice via AJAX Pass
  3. Redirects to a page Fail

The redirect does not occur, but instead continues on to the search page. Looking on fiddler, the webservice call does succeed, but the redirect itself does not.

For reference here is my code:

...
     $('.CoveoSearchButton').click(function (e) {
                checkForRedirect($(this).val());
            })
    ...

function checkForRedirect(searchText) {

    var options = {
        type: "POST",
        url: "/Web Services/AjaxService.asmx/GetCoveoKeywordRedirectURL",
        data: "{'searchTerm' : '"+searchText+"'}", //input keyword
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.d.status == "success") {
                window.location.href = data.d.data;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    };
    $.ajax(options);
}

Is there a way to (a) force the redirect or (b) cancel the coveo request?

Thanks

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Sep 1, 2015 1:17 PM

The Coveo JavaScript Search Framework is built to automatically call a REST endpoint to get the search results when a user clicks the search button or hit the Enter key in the search box. Then, it refreshes only the sections of the page that needs to be updated.

Also, on pages that are not search pages but contains a search box, you can initialize that search box to redirects to a search page with the user's searched terms automatically by initializing the search box like this:

$('#searchBox').coveo('initSearchBox', '/url/of/search/page.html');

You need to know the URL of the search page beforehand or at rendering-time. The search page URL must not contain the search terms, they will be added automatically to the URL by the Coveo JavaScript Search Framework.

More information about the initSearchBox call: https://search.coveo.com/#q=initsearchbox&sort=relevancy&f:spaceFacet=[JavaScript%20Search%20Framework%20V0.9]

Gravatar for chris.ruderschmidt@americaneagle.com

Comment by Chris Ruderschmidt, Sep 1, 2015 1:44 PM

Right, but I think that may not quite cover what I'm trying to do. For example: if I typed in a stand-alone search box "initSearchBox", id like it to go to the URL "https://developers.coveo.com/display/public/JsSearch/jQuery+Extension"

Is that possible?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Sep 1, 2015 2:04 PM

You can enable results suggestions like it is implemented on http://search.coveo.com or http://developers.coveo.com with the help of the OmniBoxResultList component (see OmniBoxResultList Component).

This is a good user experience because the user choose himself to open a document instead of executing a search.

Ask a question