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?

1 Reply

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;
}
});

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.

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?

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"?

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.