Leading wildcard in Examine
# help-with-umbraco
j
Hi. I have this code: var searchTerms = searchTerm?.Split(' ')?.Select(s => $"*{s}*").ToArray() ?? []; var resultsAsSearchItems = searcher.CreateQuery(IndexTypes.Content) .GroupedOr(["__NodeTypeAlias"], [MyPage.ModelTypeAlias]) .And() .GroupedOr(propertyNames, searchTerms) .Execute(); I would like to use leading wildcards as in searchTerms above. I tried to find a way to use AllowLeadingWildcard in LuceneSearchOptions but it seems like I can't add it to the CreateQuery. Is there a way to solve this with my approach above, or do I have to write a native Lucene query or something?
j
Examine only has trailing wildcards not leading, so probably need a native lucene query. https://shazwazza.github.io/Examine/docs-v1-v2/searching.html#wildcards
a
Got this from a coworker might give some pointers:
Copy code
((LuceneSearchQueryBase)rootquery).QueryParser.AllowLeadingWildcard = true;
  ((LuceneSearchQueryBase)rootquery).SearchOptions.AllowLeadingWildcard = true;
j
Thank you @User . I tried this but it didn't work 😦 Maybe you can spot som error in my code? var searchTerms = searchTerm?.Split(' ')?.Select(s => $"\*{s}\*").ToArray() ?? []; var rootQuery = (LuceneSearchQuery)searcher.CreateQuery(IndexTypes.Content, default) .GroupedOr(["__NodeTypeAlias"], [Recipe.ModelTypeAlias]) .And() .GroupedOr(propertyNames, searchTerms); rootQuery.QueryParser.AllowLeadingWildcard = true; rootQuery.SearchOptions.AllowLeadingWildcard = true; var resultsAsSearchItems = rootQuery.Execute();
10 Views