Just figured I'll share the solution that I came up with. As far as I understand the the abstractions used in Examine don't exponse the configuration in a obvious way (probably for some good reason), so we need to do some casting before we can set the AllowLeadingWildcard-setting.
We can do this in two ways,
Either by casting the ISearcher from the .GetSearcher()-method to BaseLuceneSearcher which exposes a .CreateQuery()-method that takes the LuceneSearchOptions():
var index = ExamineManager.Instance.Indexes.Where(f => f.Name == "ExternalIndex").FirstOrDefault();
var searcher = (BaseLuceneSearcher)index.GetSearcher();
var query = searcher.CreateQuery("content", BooleanOperation.And, searcher.LuceneAnalyzer, new LuceneSearchOptions() { AllowLeadingWildcard = true });
Or by casting the query:
var index = ExamineManager.Instance.Indexes.Where(f => f.Name == "ExternalIndex").FirstOrDefault();
var searcher = index.GetSearcher();
var query = (LuceneSearchQueryBase)searcher.CreateQuery("content");
query.QueryParser.AllowLeadingWildcard = true;
This will allow the leading wild card in the terms which is the first thing we need to do.
Next we can create a IExamineValue that contains the leading wildcard, one might think something like this would work:
query.And().Field("nodeName", "*term*");
But in the processing of the string the *-s will be stripped out in the generated lucene-query so we have two options here as well:
Use the .MultipleCharacterWildcard() string extension and making sure that the string we pass contains the leading *
var value = "*" + "term"; // leading wildcard and term
query.And().Field("nodeName", value.MultipleCharacterWildcard());
Or pass a IExamineValue for the fieldValue to create "your own" IExamineValue:
query.And().Field("nodeName", new ExamineValue(Examineness.ComplexWildcard, "*term*"));
Hope my little research on the topics helps to clearify =D
Examine Leading Wildcard
Hello everyone, Is there any way to enable leading wildcards in examine in umbraco 8?
Hey did you manage to find a solution?
No man. I didn't please let me know if you find one.
No documentation on this but the work around was putting the wildcards in the search term
*searchterm*
Hi Guys!
Just figured I'll share the solution that I came up with. As far as I understand the the abstractions used in Examine don't exponse the configuration in a obvious way (probably for some good reason), so we need to do some casting before we can set the AllowLeadingWildcard-setting.
We can do this in two ways,
Either by casting the ISearcher from the .GetSearcher()-method to BaseLuceneSearcher which exposes a .CreateQuery()-method that takes the LuceneSearchOptions():
Or by casting the query:
This will allow the leading wild card in the terms which is the first thing we need to do.
Next we can create a IExamineValue that contains the leading wildcard, one might think something like this would work:
But in the processing of the string the *-s will be stripped out in the generated lucene-query so we have two options here as well:
Use the .MultipleCharacterWildcard() string extension and making sure that the string we pass contains the leading *
Or pass a IExamineValue for the fieldValue to create "your own" IExamineValue:
Hope my little research on the topics helps to clearify =D
Thanks for the detailed response Markus i will try it and i ll let you know for the results.
is working on a reply...