summaryrefslogtreecommitdiffstats
path: root/store/sqlstore/channel_store.go
diff options
context:
space:
mode:
authorJesús Espino <jespinog@gmail.com>2018-09-11 22:45:31 +0200
committerGitHub <noreply@github.com>2018-09-11 22:45:31 +0200
commite32581aef3253bb0691d6fd678fbf01e86f2c10c (patch)
tree895aff7c6cc2e43a5209bb3c7b19384f13c5b105 /store/sqlstore/channel_store.go
parentd585f9d9a3bd7c272a02e1d3d52990bc6ecb37a3 (diff)
downloadchat-e32581aef3253bb0691d6fd678fbf01e86f2c10c.tar.gz
chat-e32581aef3253bb0691d6fd678fbf01e86f2c10c.tar.bz2
chat-e32581aef3253bb0691d6fd678fbf01e86f2c10c.zip
MM-11725: Add specific autocomplete endpoint for search autocomplete (#9337)
Diffstat (limited to 'store/sqlstore/channel_store.go')
-rw-r--r--store/sqlstore/channel_store.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go
index 1b5ae7ff7..820fe1e9f 100644
--- a/store/sqlstore/channel_store.go
+++ b/store/sqlstore/channel_store.go
@@ -1589,6 +1589,53 @@ func (s SqlChannelStore) AutocompleteInTeam(teamId string, term string, includeD
})
}
+func (s SqlChannelStore) AutocompleteInTeamForSearch(teamId string, userId string, term string, includeDeleted bool) store.StoreChannel {
+ return store.Do(func(result *store.StoreResult) {
+ deleteFilter := "AND DeleteAt = 0"
+ if includeDeleted {
+ deleteFilter = ""
+ }
+
+ queryFormat := `
+ SELECT
+ C.*
+ FROM
+ Channels AS C
+ JOIN
+ ChannelMembers AS CM ON CM.ChannelId = C.Id
+ WHERE
+ C.TeamId = :TeamId
+ AND CM.UserId = :UserId
+ ` + deleteFilter + `
+ %v
+ LIMIT 50`
+
+ var channels model.ChannelList
+
+ if likeClause, likeTerm := s.buildLIKEClause(term); likeClause == "" {
+ if _, err := s.GetReplica().Select(&channels, fmt.Sprintf(queryFormat, ""), map[string]interface{}{"TeamId": teamId, "UserId": userId}); err != nil {
+ result.Err = model.NewAppError("SqlChannelStore.AutocompleteInTeamForSearch", "store.sql_channel.search.app_error", nil, "term="+term+", "+", "+err.Error(), http.StatusInternalServerError)
+ }
+ } else {
+ // Using a UNION results in index_merge and fulltext queries and is much faster than the ref
+ // query you would get using an OR of the LIKE and full-text clauses.
+ fulltextClause, fulltextTerm := s.buildFulltextClause(term)
+ likeQuery := fmt.Sprintf(queryFormat, "AND "+likeClause)
+ fulltextQuery := fmt.Sprintf(queryFormat, "AND "+fulltextClause)
+ query := fmt.Sprintf("(%v) UNION (%v) LIMIT 50", likeQuery, fulltextQuery)
+
+ if _, err := s.GetReplica().Select(&channels, query, map[string]interface{}{"TeamId": teamId, "UserId": userId, "LikeTerm": likeTerm, "FulltextTerm": fulltextTerm}); err != nil {
+ result.Err = model.NewAppError("SqlChannelStore.AutocompleteInTeamForSearch", "store.sql_channel.search.app_error", nil, "term="+term+", "+", "+err.Error(), http.StatusInternalServerError)
+ }
+ }
+
+ sort.Slice(channels, func(a, b int) bool {
+ return strings.ToLower(channels[a].DisplayName) < strings.ToLower(channels[b].DisplayName)
+ })
+ result.Data = &channels
+ })
+}
+
func (s SqlChannelStore) SearchInTeam(teamId string, term string, includeDeleted bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
deleteFilter := "AND DeleteAt = 0"