I am using Examine to Search a list of Microsoft Graph Users. I’m having trouble with returning the expected results for phrase searches. A search for “Project Controller” returns all records containing “Project” or “Controller”. When I want only records containing “Project Controller”.
My existing Query:
public async Task<List<User>> Search(string query = "")
{
string[] terms = query.Split(' ');
_allUsers = await GetAllUsers();
List<User> matchingUsers = new List<User>();
if (_examineManager.TryGetIndex("ColleaguesIndex", out _colleaguesIndex))
{
var results = _colleaguesIndex.Searcher.CreateQuery(defaultOperation: BooleanOperation.Or)
.GroupedOr(searchFields, query.MultipleCharacterWildcard()).Or()
.GroupedOr(searchFields, query.Escape()).Or()
.GroupedOr(searchFields, terms)
.Execute();
var resultsID = results.Select(r => r.Id);
matchingUsers = _allUsers.Where(u => resultsID.Contains(u.OnPremisesSamAccountName)).ToList();
}
return matchingUsers;
}
When searching for “Project Controller” I get 68 results. There are 14 Users with the Job Title = “Project Controllers”.
I tried changing .GroupedOr(searchFields, terms)
to .GroupedAnd(searchFields, terms)
.
I tried changing .GroupedOr(searchFields, query.Escape())
to .GroupedAnd(searchFields, query.Escape())
In both cases I end up with 0 results. The problem with changing these to GroupedAnd is I think it’s checking to see if ALL fields in my searchFields = “Project Controller”.
I simplified my query for testing purposes and I ran this:
var results = _colleaguesIndex.Searcher.CreateQuery().GroupedOr(searchFields, query.Escape()).Execute();
…and got 0 results. That I can’t explain. I would have expected 14 results there. Needless to say I’m befuddled by how to achieve a phrase search on a group of fields.