I want to find users based on a list of usernames. the list of the username might contain partial usernames and will come from a web application.
var userNames = new List<string> (...); // not sure how many!
LINQ:
var userEntity = allUsers.Where(p=> userNames.Any(x=> p.UserName.Contains(x)))
NEST: ???
must.Terms(t => t.Field(f => f.UserName).Terms<string>(usernames))
but this only returns the exact matches and not the partials.
How would you translate the above LINQ query into NEST (ElasticSearch)?
The inefficient way would be to set up a
bool
query withshould
clauses (if relevancy scores are needed) or afilter
(if relevancy scores are not needed) clause withbool
queryshould
clauses, with a collection ofprefix
,wildcard
orregexp
queries, one for each unique username you are looking for. Depending on the amount of data that you are looking at and the number of usernames you are searching for, the performance of this query could be really bad.The much more efficient way is to index usernames with an analyzer that tokenizes usernames according to the tokens that you'd expect to search with e.g. if you're looking to match on username
Russ
by searching forRu
, then you'd want to build an analyzer that includes edgengram tokenizer or token filter, then use amatch
query, or any query that undergoes analysis at query time.