I am trying to create a component that provides typeahead autocomplete suggestions for users and groups. I'm using elasticsearch 6.5.3. I created an index that contains the fields I want searched and 3 additional fields to filter by (isGroup,isUser,organizationId). In some cases I want to use this component to search all users and group, sometimes just users or just groups or just users belonging to a specific organization. I was planning to supply a filter along with the search terms depending on what the specific use case was. I'm using nest to do the search, but I can't figure out how to do that. Is it possible to do this and if so how? Am I going down the wrong path for this? I mainly followed this guide to create analyzer and stuff. I can post my index if that will help but it's kind of long.
Here is a search with two of the items returned.
return client.Search<UserGroupDocument>(s => s
.Query(q=>q
.QueryString(qs=>qs.Query("adm"))
)
);
{
"_index": "users_and_groups_autocomplete_index",
"_type": "usergroupdocument",
"_id": "c54956ab-c50e-481c-b093-f9855cc74480",
"_score": 2.2962174,
"_source": {
"id": "c54956ab-c50e-481c-b093-f9855cc74480",
"isUser": true,
"isGroup": false,
"name": "admin",
"email": "[email protected]",
"organizationId": 2
}
},
{
"_index": "users_and_groups_autocomplete_index",
"_type": "usergroupdocument",
"_id": "80f98d24-39e3-475d-9cb6-8f16ca472525",
"_score": 0.8630463,
"_source": {
"id": "80f98d24-39e3-475d-9cb6-8f16ca472525",
"isUser": false,
"isGroup": true,
"name": "new Group",
"users": [
{
"name": "Test User 1",
"email": "[email protected]"
},
{
"name": "admin",
"email": "[email protected]"
}
],
"organizationId": 0
}
},
So depending on where I use this component I may want all of these return, just the user, just the group, or just the user in organization 2.
Here is my UserGroupDocument class
public class UserGroupDocument
{
public string Id { get; set; }
public bool IsUser { get; set; }
public bool IsGroup { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<User> Users { get; set; }
public long OrganizationId { get; set; }
}
And the User class
public class User
{
public string Name { get; set; }
public string Email { get; set; }
}
Based on Russ Cam's answer below, I altered the Must statement as shown below. This gives me the filtering I wanted, but not the typeahead functionality. I still have to type and entire word before I start getting matches.
.Must(mu => mu
.QueryString(mmp => mmp
.Query(searchTerms)
.Fields(f => f
.Field(ff => ff.Name)
.Field(ff => ff.Users.Suffix("name"))
)
)
)
Here is the index I'm using.
{
"users_and_groups_autocomplete_index": {
"aliases": {},
"mappings": {
"usergroupdocument": {
"properties": {
"email": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"isGroup": {
"type": "boolean"
},
"isUser": {
"type": "boolean"
},
"name": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"organizationId": {
"type": "long"
},
"users": {
"properties": {
"email": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"name": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
}
}
}
}
}
},
"settings": {
"index": {
"number_of_shards": "5",
"provided_name": "users_and_groups_autocomplete_index",
"creation_date": "1548363729311",
"analysis": {
"analyzer": {
"autocomplete": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "autocomplete"
}
},
"tokenizer": {
"autocomplete": {
"token_chars": [
"digit",
"letter"
],
"min_gram": "1",
"type": "edge_ngram",
"max_gram": "20"
}
}
},
"number_of_replicas": "1",
"uuid": "Vxv-y58qQTG8Uh76Doi_dA",
"version": {
"created": "6050399"
}
}
}
}
}