Gravatar for nick.morgan@arm.com

Question by morgan8889, Oct 6, 2015 12:44 PM

Coveo Sitecore search results as you type

I'm using the omnibox result list view along with the search box view to enable search as you type functionality. However I'm unable to get this to work for anything other than full words. What would I need to do to be able to hit results on a character by character basis?

Thank you

2 Replies
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Oct 6, 2015 1:18 PM

Hi Nick,

The OmniBox Result List component is like a small search interface under a search box. It only executes the query the user enters as is.

To give you a suggestion, it would be good to know exactly your needs:

  • Do you want it to search in all the indexed documents body and fields or just in a specific field like the title of the documents?
  • Do you want to display only the title of the matching documents or also more fields on the documents?

If the user enters "ab" in the search box, what do you want to suggest:

  • All the documents containing a word that contains the string "ab" in any field or in the body?
  • All the documents whose title contains a word that contains the string "ab"?
  • All the documents whose title begins with the string "ab"?
  • All the documents whose title contains a word that begins with the string "ab"?

Thanks,

Jeff

Gravatar for nick.morgan@arm.com

Comment by morgan8889, Oct 6, 2015 2:27 PM

Hi Jeff,

Thanks for the reply.

Yes we would like to index the body and fields of all content on the site and displaying just the document title would be great for the omnibox.

If there is a way to cover all four of your suggestions above that would be perfect.

Thanks again.

Nick

Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Oct 7, 2015 12:18 PM

Hi Nick,

All the 4 suggestions is the same as only the first suggestion. So you want to search the characters in all the words of all the fields and body of the indexed documents.

The key feature you want to use here is the wildcards on the searched terms (see Using Wildcards in Queries).

The OmniBox Result List component is a bit difficult to customize to activate the wildcard feature and alter the user searched terms to add wildcards but I found a way to do what you are looking for. Unfortunately, the relevancy of the returned results is affected negatively. This is because a short series of characters can be found in a lot of words in a lot of indexed documents.

The way I did it was first to duplicate the original "Coveo Omnibox Result List" Sitecore sublayout as "Custom Omnibox Result List", move it in a separate sublayout folder named "Custom" and change the "Ascx file" field value of my duplicated sublayout to "/layouts/Custom/CustomOmniboxResultList.ascx". Then I created the "/layouts/Custom" folder on the file system, copied the "/layouts/Coveo/CoveoOmniboxResultList.ascx" in the "Custom" folder and renamed the copy "CustomOmniboxResultList.ascx". This is all to ensure an upgrade of Coveo for Sitecore would not override your customizations to the ascx file. This applies to all the components you would want to modify (see Duplicating the Coveo Search Component).

Then, in my "CustomOmniboxResultList.ascx" file, I replaced the default script block that configures the OmniBox with a custom script that adds wildcards to the queries.

Original script block:

<script type="text/javascript">
    Coveo.$(function() {
        CoveoForSitecore.plugin.configureOmnibox({
            headerTitle : '<%= Model.HeaderTitle %>',
            queryExpression : '<%= Model.AdditionalHiddenExpression %>',
            numberOfResults : <%= Model.NumberOfResults %>,
            uniqueId : '<%= Model.UniqueId %>',
            omniboxDataRowIndex : <%= Model.OmniboxDataRowOrderingIndex %>
        });
    });
</script>

Modified script block:

<script type="text/javascript">
    Coveo.$(function() {
        CoveoForSitecore.plugin.element.on(Coveo.Events.QueryEvents.buildingQuery,
            function (e, data) {
                // Enable the wildcards on the query builder when it is built
                data.queryBuilder.enableWildcards = true;
            }
        ).on(Coveo.Events.OmniBoxEvents.populateOmniBox,
            function (e, populateOmniBoxObject) {
                // Alter the searched expression to include a wildcard version
                var wordsToSearch = populateOmniBoxObject.completeQueryExpression.word;
                wordsToSearch = "(" + wordsToSearch + ") OR (" + wordsToSearch + "*)";
                populateOmniBoxObject.completeQueryExpression.word = wordsToSearch;

                // Call the Coveo for Sitecore jQuery plugin with the default options to populate the omnibox
                CoveoForSitecore.plugin.populateOmniboxResultList(e, populateOmniBoxObject, {
                    headerTitle : '<%= Model.HeaderTitle %>',
                    queryExpression : '<%= Model.AdditionalHiddenExpression %>',
                    numberOfResults : <%= Model.NumberOfResults %>,
                    uniqueId : '<%= Model.UniqueId %>',
                    omniboxDataRowIndex : <%= Model.OmniboxDataRowOrderingIndex %>
                });
            }
        );
    });
</script>

This code does 3 things:

  • Enable the wildcards on the query builder when the query is built.
  • Alter the searched expression to include a wildcard version. ("abc" becomes "(abc) OR (abc*)")
  • Call the Coveo for Sitecore jQuery plugin with the default options to populate the omnibox (replaces the original script).

There's a few improvements that needs to be done to that script:

  • Do not alter the query by adding a wildcard to the last searched term when the length of the term is less than 2 characters because CES refuses to run wildcard queries that have less than 2 leading characters.
  • Improve the generated query to improve the relevance of the search results.

I hope this example will be helpful for you to implement a working solution for your use case. Please share that solution too. It would be helpful.

Ask a question