6

I'm trying to do something and I'm not sure it is possible. We are using results suggestions as described in the link below. And I want to have it match partial words without requiring a wild card. So for example if I type "tick" it would be the same as if I typed "tick*" and it would suggest results with the word "ticket" in it. Is this possible? https://developers.coveo.com/display/public/SitecoreV4/Provide+Result+Suggestions

The example on that link suggests this would work but it doesn't seem to in Coveo 450 for Sitecore using Pro Cloud

1 Answer 1

7

From my testing, it does it by default. So I followed this page: developers.coveo.com/display/SitecoreV4/Provide+Result+Suggestions and I added the "Coveo Search Resources", "Coveo Search Box" and "Coveo Omnibox Result List" to my header. I now have suggestions on everything within my index.

Side Note : The suggestions, when clicked, will open the result itself, it will not redirect me to the search page.

I tested just typing "ar" and it suggested Article , Article Group, Glossary and more.

Is it what you are seeing?

* UPDATE *

I made a mistake in my original answer, I was using field-based query suggestions : https://developers.coveo.com/display/SitecoreV4/Provide+Query+Suggestions

To have the same query completion behavior with the OmniboxResultList (Results Suggestions) is not possible without adding a wildcard, since it does a full query against the index, while query suggestions does a field listing call: https://developers.coveo.com/display/SearchREST/Listing+Values+of+a+Field

So is there a workaround? Yes!

BE WARNED! This workaround will require you to map the title of your items as a facet. Facets require more computing than standard fields, so a facet with a high number of values is not recommended for large index. If you use this workaround for a few hundreds/thousands items, fine, but not for millions.

The way it work on our pages such as the online help is by doing a field-based query with a custom onSelect event, which looks like this:

$('#searchBox').coveo('initSearchbox', MySearchBoxComponent, {
    FieldSuggestions: {
        omniboxSuggestionOptions: {
            onSelect: function (valueSelected, populateOmniBoxEventArgs) {
                populateOmniBoxEventArgs.closeOmnibox();
                // When a value is selected, query the index for the result list
                Coveo.SearchEndpoint.endpoints["default"]
                    .search({
                    q: '@systitle=="' + valueSelected + '"',
                    aq: SuggestionScope
                    })
                    .done(function (results) {
                        var foundResult = Coveo._.find(results.results, function(result){
                                return valueSelected == result.raw.systitle;
                            });
                        if(foundResult) {
                            logCustomEvent('pageNav', 'omniboxTitleSuggestion', uaToken, foundResult.Title, foundResult.clickUri);                                  
                            window.location = foundResult.clickUri;
                        } else {
                            logger.warn("Selected suggested result," + valueSelected + " , not found.");
                        }
                    })
            },
            queryOverride: SuggestionScope
        }
    }
});

* UPDATE 2 * My original code was using the Coveo JavaScript framework standalone, here is the new code in Coveo for Sitecore using MVC

    Coveo.$(function () {
        var searchbox = Coveo.$('#@Model.SearchboxId');
            if (typeof(CoveoForSitecore) !== 'undefined') {
                CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()));

                //Here is the part where I access the suggestions of my search box
                CoveoForSitecore.componentsOptions.FieldsSuggestions = { 
                    omniboxSuggestionOptions: {
                    //This queryOverride is optional, you can use it to filter the suggestions
                        queryOverride: '@Model.ToCoveoFieldName("haslayout")=="1"',
                        //The onSelect function is the one we want to override in order to open the suggestion 
                        onSelect: function (valueSelected, populateOmniBoxEventArgs) {
                            populateOmniBoxEventArgs.closeOmnibox();
                            //This is the core of the code, you will launch a new query to find the value selected in the suggestion and open it directly
                            Coveo.SearchEndpoint.endpoints["default"]
                                .search({
                                    q: '@Model.ToCoveoFieldName("title")' + '=="' + valueSelected + '"'
                                })
                                .done(function (results) {
                                    var foundResult = Coveo._.find(results.results, function(result){
                                            return valueSelected == result.raw.systitle;
                                        });
                                    if(foundResult) {
                                    // This logCustomEvent is optional but should be used in order to track in the Usage Analytics
                                        logCustomEvent('enter the name of the event');                                  
                                        window.location = foundResult.clickUri;
                                    } else {
                                        logger.warn("Selected suggested result," + valueSelected + " , not found.");
                                    }
                                })
                        }
                    }   
                }          

                searchbox.coveoForSitecore('initSearchbox', CoveoForSitecore.componentsOptions);

            } else {
                searchbox.coveo('initSearchbox',
                                '@Model.GetSearchPageUrl()');
            }
            searchbox.find(".CoveoSearchbox input").attr("placeholder", '@Model.SearchboxPlaceholderText');

So what it does is offering query suggestions, but instead of redirecting to the result page, it queries the index and redirects to the most relevant result.

For more information on the PopulateOmnibox function: https://developers.coveo.com/display/JsSearchV1/Providing+Suggestions+for+the+Omnibox

5
  • So I see that on your site but I don't see that on our QA site we have to add the wild card to make it work. I see suggested results and they do click through right to the result and not the search page. Commented Nov 4, 2016 at 14:51
  • You are correct, I made a mistake, see my updated answer Commented Nov 4, 2016 at 18:37
  • Ok so trying to get this to work. Right now it just causes the page to error out. I feel like JavaScript is different from how things work in 450. Specifically this part: $('#searchBox').coveo( shouldn't it be something like Coveo.$('#@Model.Id') Commented Nov 4, 2016 at 19:00
  • Here is the error I'm seeing: error CS0103: The name 'systitle' does not exist in the current context Commented Nov 4, 2016 at 19:09
  • I updated my answer for a Coveo for Sitecore 4 with Sitecore MVC approach Commented Dec 8, 2016 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.