Gravatar for

Question by cmgutmanis, Apr 27, 2015 2:51 PM

StartsWith LINQ returns no results when spaces are passed in


I am trying to do a very simple LINQ-based search in Sitecore perform a typeahead-style search on item names for real-time searching. It works perfectly when I input alpha characters, but the second I type in a space character, I get no results.

Basically if I have 3 items:

  • Test Item 1
  • Test Item 2
  • Test Page

"Test" would get translated into "Test*" and return all three. "Test " would become "Test *" and return nothing when I would think it should return all three items. Similarly, "Test I" would return nothing as "Test I*" despite the fact that it should match the first two items.

In this case, I am using the Sitecore wrapper to call Coveo Search as follows:

var results = context.GetQueryable<SearchResultItem>().Where(x => x.Name.StartsWith(searchQuery.ToLower(), StringComparison.OrdinalIgnoreCase)).OrderBy(c => c["name"]).GetResults();

Thanks in advance for all the help, this board has been outstanding for resolving issues!

1 Reply
Gravatar for

Answer by Jean-François L'Heureux, Apr 27, 2015 3:10 PM


The reason your queries aren't returning any results is because of your CES index "Number of Leading Chars" wildcard setting. By default, CES is configured to execute wildcard searches if there is at least 2 characters before a wildcard character in a term. It is not recommended to use a value below two as this would result in computationally expensive wildcard queries.

Your queries:

  • "Test" has only 1 term. It does the "Test" wildcard search and return results since there is 4 leading characters to the "" character.
  • "Test " has also only 1 term. It does the "Test " wildcard search. Since the "" character has no leading alphanumeric characters, the query is ignored and returns 0 results.
  • "Test I" has 2 terms ("Test" and "I"). It does the "Test I" wildcard search. Since the "" character has only one leading alphanumeric character, the query is ignored and returns 0 results.

Here is documentation on wildcard queries and their relevant settings

Ask a question