[MM-15811] Adds fallback mechanism for elasticsearch queries (#11065)
* [MM-15811] Adds fallback mechanism for elasticsearch queries * Rename config helpers
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
4c075e45f4
Коммит
406dcb1311
237
app/user.go
237
app/user.go
@@ -280,8 +280,7 @@ func (a *App) createUserOrGuest(user *model.User, guest bool) (*model.User, *mod
|
||||
})
|
||||
}
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
|
||||
if a.IsESIndexingEnabled() {
|
||||
a.Srv.Go(func() {
|
||||
if err := a.indexUser(user); err != nil {
|
||||
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
|
||||
@@ -1160,8 +1159,7 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User,
|
||||
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
|
||||
if a.IsESIndexingEnabled() {
|
||||
a.Srv.Go(func() {
|
||||
if err := a.indexUser(user); err != nil {
|
||||
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
|
||||
@@ -1516,8 +1514,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
|
||||
mlog.Warn(fmt.Sprintf("Permanently deleted account %v id=%v", user.Email, user.Id), mlog.String("user_id", user.Id))
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
|
||||
if a.IsESIndexingEnabled() {
|
||||
a.Srv.Go(func() {
|
||||
if err := a.Elasticsearch.DeleteUser(user); err != nil {
|
||||
mlog.Error("Encountered error deleting user", mlog.String("user_id", user.Id), mlog.Err(err))
|
||||
@@ -1709,35 +1706,25 @@ func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term stri
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
var result store.StoreResult
|
||||
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
license := a.License()
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {
|
||||
listOfAllowedChannels, err := a.GetViewUsersRestrictionsForTeam(a.Session.UserId, teamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if listOfAllowedChannels != nil && len(listOfAllowedChannels) == 0 {
|
||||
return []*model.User{}, nil
|
||||
}
|
||||
|
||||
usersIds, err := a.Elasticsearch.SearchUsersInTeam(teamId, listOfAllowedChannels, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil)
|
||||
} else {
|
||||
result = <-a.Srv.Store.User().Search(teamId, term, options)
|
||||
func (a *App) esSearchUsersInTeam(teamId, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
listOfAllowedChannels, err := a.GetViewUsersRestrictionsForTeam(a.Session.UserId, teamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if listOfAllowedChannels != nil && len(listOfAllowedChannels) == 0 {
|
||||
return []*model.User{}, nil
|
||||
}
|
||||
|
||||
usersIds, err := a.Elasticsearch.SearchUsersInTeam(teamId, listOfAllowedChannels, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1747,6 +1734,34 @@ func (a *App) SearchUsersInTeam(teamId string, term string, options *model.UserS
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersInTeam(teamId, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
var users []*model.User
|
||||
var err *model.AppError
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
if a.IsESAutocompletionEnabled() {
|
||||
users, err = a.esSearchUsersInTeam(teamId, term, options)
|
||||
if err != nil {
|
||||
mlog.Error("Encountered error on SearchUsersInTeam through Elasticsearch. Falling back to default search.", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
if !a.IsESAutocompletionEnabled() || err != nil {
|
||||
result := <-a.Srv.Store.User().Search(teamId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
|
||||
users = result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
}
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
term = strings.TrimSpace(term)
|
||||
result := <-a.Srv.Store.User().SearchNotInTeam(notInTeamId, term, options)
|
||||
@@ -1777,38 +1792,26 @@ func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptio
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) {
|
||||
var uchan, nuchan store.StoreChannel
|
||||
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
license := a.License()
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {
|
||||
listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(listOfAllowedChannels) == 0 {
|
||||
return &model.UserAutocompleteInChannel{}, nil
|
||||
}
|
||||
uchanIds := []string{}
|
||||
nuchanIds := []string{}
|
||||
if !strings.Contains(strings.Join(listOfAllowedChannels, "."), channelId) {
|
||||
nuchanIds, err = a.Elasticsearch.SearchUsersInTeam(teamId, listOfAllowedChannels, term, options)
|
||||
} else {
|
||||
uchanIds, nuchanIds, err = a.Elasticsearch.SearchUsersInChannel(teamId, channelId, listOfAllowedChannels, term, options)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uchan = a.Srv.Store.User().GetProfileByIds(uchanIds, false, nil)
|
||||
nuchan = a.Srv.Store.User().GetProfileByIds(nuchanIds, false, nil)
|
||||
} else {
|
||||
uchan = a.Srv.Store.User().SearchInChannel(channelId, term, options)
|
||||
nuchan = a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options)
|
||||
func (a *App) esAutocompleteUsersInChannel(teamId, channelId, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) {
|
||||
listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(listOfAllowedChannels) == 0 {
|
||||
return &model.UserAutocompleteInChannel{}, nil
|
||||
}
|
||||
uchanIds := []string{}
|
||||
nuchanIds := []string{}
|
||||
if !strings.Contains(strings.Join(listOfAllowedChannels, "."), channelId) {
|
||||
nuchanIds, err = a.Elasticsearch.SearchUsersInTeam(teamId, listOfAllowedChannels, term, options)
|
||||
} else {
|
||||
uchanIds, nuchanIds, err = a.Elasticsearch.SearchUsersInChannel(teamId, channelId, listOfAllowedChannels, term, options)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uchan := a.Srv.Store.User().GetProfileByIds(uchanIds, false, nil)
|
||||
nuchan := a.Srv.Store.User().GetProfileByIds(nuchanIds, false, nil)
|
||||
autocomplete := &model.UserAutocompleteInChannel{}
|
||||
|
||||
result := <-uchan
|
||||
@@ -1838,47 +1841,112 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
|
||||
return autocomplete, nil
|
||||
}
|
||||
|
||||
func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
|
||||
autocomplete := &model.UserAutocompleteInTeam{}
|
||||
var result store.StoreResult
|
||||
|
||||
func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, *model.AppError) {
|
||||
var autocomplete *model.UserAutocompleteInChannel
|
||||
var err *model.AppError
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
license := a.License()
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableAutocomplete && license != nil && *license.Features.Elasticsearch {
|
||||
listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions)
|
||||
if a.IsESAutocompletionEnabled() {
|
||||
autocomplete, err = a.esAutocompleteUsersInChannel(teamId, channelId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
mlog.Error("Encountered error on AutocompleteUsersInChannel through Elasticsearch. Falling back to default autocompletion.", mlog.Err(err))
|
||||
}
|
||||
if len(listOfAllowedChannels) == 0 {
|
||||
return &model.UserAutocompleteInTeam{}, nil
|
||||
}
|
||||
|
||||
usersIds, err := a.Elasticsearch.SearchUsersInTeam(teamId, listOfAllowedChannels, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil)
|
||||
} else {
|
||||
result = <-a.Srv.Store.User().Search(teamId, term, options)
|
||||
}
|
||||
|
||||
if !a.IsESAutocompletionEnabled() || err != nil {
|
||||
autocomplete = &model.UserAutocompleteInChannel{}
|
||||
uchan := a.Srv.Store.User().SearchInChannel(channelId, term, options)
|
||||
nuchan := a.Srv.Store.User().SearchNotInChannel(teamId, channelId, term, options)
|
||||
|
||||
result := <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
}
|
||||
|
||||
autocomplete.InChannel = users
|
||||
|
||||
result = <-nuchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
users = result.Data.([]*model.User)
|
||||
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
}
|
||||
|
||||
autocomplete.OutOfChannel = users
|
||||
}
|
||||
|
||||
return autocomplete, nil
|
||||
}
|
||||
|
||||
func (a *App) esAutocompleteUsersInTeam(teamId, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
|
||||
listOfAllowedChannels, err := a.getListOfAllowedChannelsForTeam(teamId, options.ViewRestrictions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(listOfAllowedChannels) == 0 {
|
||||
return &model.UserAutocompleteInTeam{}, nil
|
||||
}
|
||||
|
||||
usersIds, err := a.Elasticsearch.SearchUsersInTeam(teamId, listOfAllowedChannels, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := <-a.Srv.Store.User().GetProfileByIds(usersIds, false, nil)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
users := result.Data.([]*model.User)
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
}
|
||||
|
||||
autocomplete := &model.UserAutocompleteInTeam{}
|
||||
autocomplete.InTeam = users
|
||||
|
||||
return autocomplete, nil
|
||||
}
|
||||
|
||||
func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
|
||||
var autocomplete *model.UserAutocompleteInTeam
|
||||
var err *model.AppError
|
||||
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
if a.IsESAutocompletionEnabled() {
|
||||
autocomplete, err = a.esAutocompleteUsersInTeam(teamId, term, options)
|
||||
if err != nil {
|
||||
mlog.Error("Encountered error on AutocompleteUsersInTeam through Elasticsearch. Falling back to default autocompletion.", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
if !a.IsESAutocompletionEnabled() || err != nil {
|
||||
autocomplete = &model.UserAutocompleteInTeam{}
|
||||
result := <-a.Srv.Store.User().Search(teamId, term, options)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
|
||||
users := result.Data.([]*model.User)
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
}
|
||||
|
||||
autocomplete.InTeam = users
|
||||
}
|
||||
|
||||
return autocomplete, nil
|
||||
}
|
||||
|
||||
func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provider einterfaces.OauthProvider, service string) *model.AppError {
|
||||
oauthUser := provider.GetUserFromJson(userData)
|
||||
if oauthUser == nil {
|
||||
@@ -1922,8 +1990,7 @@ func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provide
|
||||
user = users.New
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
esInterface := a.Elasticsearch
|
||||
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
|
||||
if a.IsESIndexingEnabled() {
|
||||
a.Srv.Go(func() {
|
||||
if err := a.indexUser(user); err != nil {
|
||||
mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.Err(err))
|
||||
|
||||
Ссылка в новой задаче
Block a user