Gravatar for ahmedokour86@gmail.com

Question by aokour, Aug 21, 2015 8:00 AM

How to automatically add wildcard to search keyword

Hi, We are using Coveo for sitecore module, and i have enabled wild card by adding (data-enable-wildcards="true" ) to search box element, However, looks like the user needs to manually add "*" to the end of the search keyword for it to work, is it possible to add the "*" before the running the query automatically?

Thanks

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Aug 21, 2015 10:10 AM

The definition of the enableWildcards option on the JavaScript Search Framework is:

If true, Coveo Platform will expand keywords containing wildcard characters to the possible matching keywords to broaden the query. The default value is false.

Note that wildcard queries are a lot heavier to handle than regular queries for the CES index. If you still want to automatically add wildcard characters, this code snippet should be a good starting point:

// Execute the code in a doneBuildingQuery event handler to ensure the basic search expression is filled
Coveo.$('#search').on('doneBuildingQuery', function(e, args) {
    // Get the basic expression set by the components on the page
    var expression = args.queryBuilder.expression.build().trim();

    // You need to write your own logic to determine if you want to add wildcards or not.
    // This should check at least that:
    // - The expression contains only free-text. (No fields, no operators, no aliases, no query functions, no exact match...)
    // - The expression length is equal or higher than the minimum wildcard number of leading characters (default: 3)
    // - Add any other condition you want
    var shouldAddWildcards = false;

    if (shouldAddWildcards) {
        // Add the wildcard characters.
        // You may add one to each term in the expression.
        expression = expression + '*';

        // Replace the ExpressionBuilder with a new one
        var expressionBuilder = new Coveo.Ui.ExpressionBuilder();
        expressionBuilder.add(expression);
        args.queryBuilder.expression = expressionBuilder;
    }
});
Gravatar for slangevin@coveo.com

Comment by Simon, Aug 21, 2015 11:11 AM

To add to JF comment, be very careful with this since it can not only make queries heavier to process, it can also break relevance. If you are doing this to "type less, find more", I would instead aim for query suggestion and search as you type. All back up with the Coveo Usage Analytics, you will have way more success in the long run.

Gravatar for ahmedokour86@gmail.com

Comment by aokour, Aug 21, 2015 12:05 PM

Thanks! I have applied your example and seems to be the answer to my problem, however, I noticed that event "doneBuildingQuery" only gets triggered if i hit search from inside the search page, But if i was redirected to the search page from other page (Using standalone search box) the event is not triggered, any thoughts why?

Thanks

Gravatar for ahmedokour86@gmail.com

Comment by aokour, Aug 21, 2015 12:11 PM

Also, Is there a way we can have wildcard as preceding wildcard, example if user search for "ball" , i need to get the results containing words like "football" ? is it possible to send the keyword as "*ball"?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 21, 2015 12:42 PM

Here is the documentation about the wildcard support of the CES index: https://onlinehelp.coveo.com/en/ces/7.0/user/usingwildcardsin_queries.htm

I don't think it supports leading wildcards as in "*ball".

For the doneBuildingQuery event, I'm pretty sure it is triggered when landing on the page with search terms in the URL. Your problem may be the moment you register your event handler on the page. You need to register your event handler before the .coveoForSitecore('init', ...); call to ensure your event handler will be used for the query done at the initialization of the search page.

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 21, 2015 12:43 PM

Code:

<script type="text/javascript">
    Coveo.$(function() {
        Coveo.$('#search').on('doneBuildingQuery', function(e, args) {
          // Your event handler code here
        }.coveoForSitecore('init', CoveoForSitecore.componentsOptions);
    });
</script>
Gravatar for ahmedokour86@gmail.com

Comment by aokour, Aug 22, 2015 5:42 PM

Ok,that might be the problem then, However, i'm having an issue with wildcard search, as sometimes i get the message: "You query has an error, wildcard too general" The search keyword was "321", any thoughts what thats means? Thanks

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 24, 2015 9:51 AM

When doing wildcard searches, it's like the index is finding indexed terms that begins with the characters you typed and replace the expression with a big OR expression. (e.g.: univers* gives (univers OR universe OR university …)

The "wildcard too general" message appears when the index found too many terms (default setting is: more than 32 terms) beginning by the characters you typed. We have this message because doing a query with a lot of terms in a big OR expression is very heavy to handle for the index and degrades the user experience.

Gravatar for ahmedokour86@gmail.com

Comment by aokour, Aug 24, 2015 5:44 PM

Actually, Thats not the case for me, When i search for "0452" without wildcard, i don't get any results back, see here:

http://i.imgur.com/WTgAdy8.jpg

http://i.imgur.com/fZwcytg.jpg

Also, Assuming that was the case, how do you suggest we can work around it? Maybe we can trigger a second query search without wildcard based on the error we receive from the first query?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 25, 2015 2:54 PM

You don't find any document when searching for "0452" because no documents contains exactly that term. They probably contains 04520, 04521, 04522, 04523, 0452, …, or any other alphanumeric word that begins with 0452.

The conversion of the wildcard to a big OR expression is not done by the UI but by the search index itself. This is why you see 0452* for the q parameter of your query.

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 25, 2015 3:09 PM

  1. Listening to this error and triggering a new query without wildcards can be a good idea.
  2. Increasing the maximum number of wildcard candidates is another idea but it will increase the time it takes to get the search results from the index.
  3. Not using wildcards at all is probably a better idea depending on what you are trying to achieve.

Learn more about wildcards: https://developers.coveo.com/display/SupportKB/What's+Happening+Behind+the+Scenes+When+You+Use+a+Wildcard

Ask a question