Adding paging to elasticsearch API. (#9425)
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
bfec808e53
Коммит
37e00ef916
36
api4/post.go
36
api4/post.go
@@ -330,26 +330,42 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
includeDeletedChannels := r.URL.Query().Get("include_deleted_channels") == "true"
|
||||
params := model.SearchParameterFromJson(r.Body)
|
||||
|
||||
props := model.StringInterfaceFromJson(r.Body)
|
||||
|
||||
terms, ok := props["terms"].(string)
|
||||
if !ok || len(terms) == 0 {
|
||||
if params.Terms == nil || len(*params.Terms) == 0 {
|
||||
c.SetInvalidParam("terms")
|
||||
return
|
||||
}
|
||||
terms := *params.Terms
|
||||
|
||||
timeZoneOffset, ok := props["time_zone_offset"].(float64)
|
||||
if !ok {
|
||||
timeZoneOffset = 0
|
||||
timeZoneOffset := 0
|
||||
if params.TimeZoneOffset != nil {
|
||||
timeZoneOffset = *params.TimeZoneOffset
|
||||
}
|
||||
|
||||
isOrSearch, _ := props["is_or_search"].(bool)
|
||||
isOrSearch := false
|
||||
if params.IsOrSearch != nil {
|
||||
isOrSearch = *params.IsOrSearch
|
||||
}
|
||||
|
||||
page := 0
|
||||
if params.Page != nil {
|
||||
page = *params.Page
|
||||
}
|
||||
|
||||
perPage := 60
|
||||
if params.PerPage != nil {
|
||||
perPage = *params.PerPage
|
||||
}
|
||||
|
||||
includeDeletedChannels := false
|
||||
if params.IncludeDeletedChannels != nil {
|
||||
includeDeletedChannels = *params.IncludeDeletedChannels
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
results, err := c.App.SearchPostsInTeam(terms, c.Session.UserId, c.Params.TeamId, isOrSearch, includeDeletedChannels, int(timeZoneOffset))
|
||||
results, err := c.App.SearchPostsInTeam(terms, c.Session.UserId, c.Params.TeamId, isOrSearch, includeDeletedChannels, int(timeZoneOffset), page, perPage)
|
||||
|
||||
elapsedTime := float64(time.Since(startTime)) / float64(time.Second)
|
||||
metrics := c.App.Metrics
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mattermost/mattermost-server/app"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
@@ -1323,6 +1325,24 @@ func TestSearchPosts(t *testing.T) {
|
||||
t.Fatal("wrong search")
|
||||
}
|
||||
|
||||
terms = "search"
|
||||
page := 0
|
||||
perPage := 2
|
||||
searchParams = model.SearchParameter{
|
||||
Terms: &terms,
|
||||
IsOrSearch: &isOrSearch,
|
||||
TimeZoneOffset: &timezoneOffset,
|
||||
Page: &page,
|
||||
PerPage: &perPage,
|
||||
}
|
||||
posts2, resp := Client.SearchPostsWithParams(th.BasicTeam.Id, &searchParams)
|
||||
CheckNoError(t, resp)
|
||||
if len(posts2.Order) != 3 { // We don't support paging for DB search yet, modify this when we do.
|
||||
t.Fatal("Wrong number of posts", len(posts2.Order))
|
||||
}
|
||||
assert.Equal(t, posts.Order[0], posts2.Order[0])
|
||||
assert.Equal(t, posts.Order[1], posts2.Order[1])
|
||||
|
||||
posts, resp = Client.SearchPosts(th.BasicTeam.Id, "search", false)
|
||||
CheckNoError(t, resp)
|
||||
if len(posts.Order) != 3 {
|
||||
@@ -1341,7 +1361,15 @@ func TestSearchPosts(t *testing.T) {
|
||||
t.Fatal("wrong search")
|
||||
}
|
||||
|
||||
posts, resp = Client.SearchPostsIncludeDeletedChannels(th.BasicTeam.Id, "#hashtag", false)
|
||||
terms = "#hashtag"
|
||||
includeDeletedChannels := true
|
||||
searchParams = model.SearchParameter{
|
||||
Terms: &terms,
|
||||
IsOrSearch: &isOrSearch,
|
||||
TimeZoneOffset: &timezoneOffset,
|
||||
IncludeDeletedChannels: &includeDeletedChannels,
|
||||
}
|
||||
posts, resp = Client.SearchPostsWithParams(th.BasicTeam.Id, &searchParams)
|
||||
CheckNoError(t, resp)
|
||||
if len(posts.Order) != 2 {
|
||||
t.Fatal("wrong search")
|
||||
@@ -1351,7 +1379,7 @@ func TestSearchPosts(t *testing.T) {
|
||||
*cfg.TeamSettings.ExperimentalViewArchivedChannels = false
|
||||
})
|
||||
|
||||
posts, resp = Client.SearchPostsIncludeDeletedChannels(th.BasicTeam.Id, "#hashtag", false)
|
||||
posts, resp = Client.SearchPostsWithParams(th.BasicTeam.Id, &searchParams)
|
||||
CheckNoError(t, resp)
|
||||
if len(posts.Order) != 1 {
|
||||
t.Fatal("wrong search")
|
||||
@@ -1389,13 +1417,13 @@ func TestSearchHashtagPosts(t *testing.T) {
|
||||
Client := th.Client
|
||||
|
||||
message := "#sgtitlereview with space"
|
||||
_ = th.CreateMessagePost(message)
|
||||
assert.NotNil(t, th.CreateMessagePost(message))
|
||||
|
||||
message = "#sgtitlereview\n with return"
|
||||
_ = th.CreateMessagePost(message)
|
||||
assert.NotNil(t, th.CreateMessagePost(message))
|
||||
|
||||
message = "no hashtag"
|
||||
_ = th.CreateMessagePost(message)
|
||||
assert.NotNil(t, th.CreateMessagePost(message))
|
||||
|
||||
posts, resp := Client.SearchPosts(th.BasicTeam.Id, "#sgtitlereview", false)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
@@ -622,7 +622,7 @@ func (a *App) DeletePostFiles(post *model.Post) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int) (*model.PostSearchResults, *model.AppError) {
|
||||
func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) {
|
||||
paramsList := model.ParseSearchParams(terms, timeZoneOffset)
|
||||
includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels
|
||||
|
||||
@@ -668,7 +668,7 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postIds, matches, err := a.Elasticsearch.SearchPosts(userChannels, finalParamsList)
|
||||
postIds, matches, err := a.Elasticsearch.SearchPosts(userChannels, finalParamsList, page, perPage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ type ElasticsearchInterface interface {
|
||||
Start() *model.AppError
|
||||
Stop() *model.AppError
|
||||
IndexPost(post *model.Post, teamId string) *model.AppError
|
||||
SearchPosts(channels *model.ChannelList, searchParams []*model.SearchParams) ([]string, model.PostSearchMatches, *model.AppError)
|
||||
SearchPosts(channels *model.ChannelList, searchParams []*model.SearchParams, page, perPage int) ([]string, model.PostSearchMatches, *model.AppError)
|
||||
DeletePost(post *model.Post) *model.AppError
|
||||
TestConfig(cfg *model.Config) *model.AppError
|
||||
PurgeIndexes() *model.AppError
|
||||
|
||||
@@ -2168,17 +2168,6 @@ func (c *Client4) SearchPostsWithParams(teamId string, params *SearchParameter)
|
||||
}
|
||||
}
|
||||
|
||||
// SearchPosts returns any posts with matching terms string including deleted channels.
|
||||
func (c *Client4) SearchPostsIncludeDeletedChannels(teamId string, terms string, isOrSearch bool) (*PostList, *Response) {
|
||||
requestBody := map[string]interface{}{"terms": terms, "is_or_search": isOrSearch}
|
||||
if r, err := c.DoApiPost(c.GetTeamRoute(teamId)+"/posts/search?include_deleted_channels=true", StringInterfaceToJson(requestBody)); err != nil {
|
||||
return nil, BuildErrorResponse(r, err)
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return PostListFromJson(r.Body), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
// SearchPosts returns any posts with matching terms string, including .
|
||||
func (c *Client4) SearchPostsWithMatches(teamId string, terms string, isOrSearch bool) (*PostSearchResults, *Response) {
|
||||
requestBody := map[string]interface{}{"terms": terms, "is_or_search": isOrSearch}
|
||||
|
||||
@@ -97,9 +97,12 @@ type PostPatch struct {
|
||||
}
|
||||
|
||||
type SearchParameter struct {
|
||||
Terms *string `json:"terms"`
|
||||
IsOrSearch *bool `json:"is_or_search"`
|
||||
TimeZoneOffset *int `json:"time_zone_offset"`
|
||||
Terms *string `json:"terms"`
|
||||
IsOrSearch *bool `json:"is_or_search"`
|
||||
TimeZoneOffset *int `json:"time_zone_offset"`
|
||||
Page *int `json:"page"`
|
||||
PerPage *int `json:"per_page"`
|
||||
IncludeDeletedChannels *bool `json:"include_deleted_channels"`
|
||||
}
|
||||
|
||||
func (o *PostPatch) WithRewrittenImageURLs(f func(string) string) *PostPatch {
|
||||
|
||||
Ссылка в новой задаче
Block a user