From df272e87188f32acfc22712e34ea17ac7b148eac Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 8 Mar 2022 17:07:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- public/home/search.js | 6 ++- routers/search.go | 107 +++++++++++++++++++++++++----------------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/public/home/search.js b/public/home/search.js index c5e6b382b..83298c446 100644 --- a/public/home/search.js +++ b/public/home/search.js @@ -71,6 +71,7 @@ var startIndex =1; var endIndex = 5; var totalPage = 1; var totalNum = 0; +var privateTotal = 0; function initPageInfo(){ currentPage = 1; @@ -168,7 +169,9 @@ function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy=" 'OnlyReturnNum':onlyReturnNum, 'SortBy':sortBy, 'OnlySearchLabel':OnlySearchLabel, - 'Ascending':currentSearchAscending + 'Ascending':currentSearchAscending, + 'WebTotal':totalNum, + 'PrivateTotal':privateTotal }, async:true, success:function(json){ @@ -200,6 +203,7 @@ function displayResult(tableName,page,jsonResult,onlyReturnNum,keyword){ console.log("set total num." + tableName); totalPage =Math.ceil(jsonResult.Total/pageSize); totalNum = jsonResult.Total; + privateTotal = jsonResult.PrivateTotal; setPage(page); } diff --git a/routers/search.go b/routers/search.go index 1aa2060fc..6565f763b 100644 --- a/routers/search.go +++ b/routers/search.go @@ -18,9 +18,6 @@ type SearchRes struct { Total int64 Result []map[string]interface{} PrivateTotal int64 - PrivatePage int - PublicPage int - PublicTotal int64 } var client *elastic.Client @@ -141,44 +138,62 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa if SortBy == "" { SortBy = "updated_unix.keyword" } + PrivateTotal := ctx.QueryInt("PrivateTotal") + WebTotal := ctx.QueryInt("WebTotal") ascending := ctx.QueryBool("Ascending") - orderBy := models.SearchOrderByRecentUpdated - switch SortBy { - case "updated_unix.keyword": - orderBy = models.SearchOrderByRecentUpdated - case "num_stars": - orderBy = models.SearchOrderByStarsReverse - case "num_forks": - orderBy = models.SearchOrderByForksReverse - case "num_watches": - orderBy = models.SearchOrderByWatches - } - log.Info("actor is null?:" + fmt.Sprint(ctx.User == nil)) - repos, count, err := models.SearchRepository(&models.SearchRepoOptions{ - ListOptions: models.ListOptions{ - Page: Page, - PageSize: PageSize, - }, - Actor: ctx.User, - OrderBy: orderBy, - Private: true, - OnlyPrivate: true, - Keyword: Key, - IncludeDescription: setting.UI.SearchRepoDescription, - }) - if err != nil { - ctx.ServerError("SearchRepository", err) - return + from := (Page - 1) * PageSize + resultObj := &SearchRes{} + + resultObj.Result = make([]map[string]interface{}, 0) + + if from < PrivateTotal || from == 0 { + orderBy := models.SearchOrderByRecentUpdated + switch SortBy { + case "updated_unix.keyword": + orderBy = models.SearchOrderByRecentUpdated + case "num_stars": + orderBy = models.SearchOrderByStarsReverse + case "num_forks": + orderBy = models.SearchOrderByForksReverse + case "num_watches": + orderBy = models.SearchOrderByWatches + } + log.Info("actor is null?:" + fmt.Sprint(ctx.User == nil)) + repos, count, err := models.SearchRepository(&models.SearchRepoOptions{ + ListOptions: models.ListOptions{ + Page: Page, + PageSize: PageSize, + }, + Actor: ctx.User, + OrderBy: orderBy, + Private: true, + OnlyPrivate: true, + Keyword: Key, + IncludeDescription: setting.UI.SearchRepoDescription, + }) + if err != nil { + ctx.JSON(200, "") + return + } + resultObj.PrivateTotal = count + if repos.Len() > 0 { + log.Info("Query private repo number is:" + fmt.Sprint(repos.Len())) + makePrivateRepo(repos, resultObj, Key) + } else { + log.Info("not found private repo,keyword=" + Key) + } + if repos.Len() >= PageSize { + resultObj.Total = int64(WebTotal) + ctx.JSON(200, resultObj) + return + } } - privateRe := &SearchRes{} - privateRe.PrivateTotal = count - privateRe.Result = make([]map[string]interface{}, 0) - if count > 0 { - log.Info("Query private repo number is:" + fmt.Sprint(count)) - makePrivateRepo(repos, privateRe, Key) - } else { - log.Info("not found private repo,keyword=" + Key) + + from = from - PrivateTotal + if from < 0 { + from = 0 } + Size := PageSize - len(resultObj.Result) log.Info("query searchRepo start") if Key != "" { @@ -187,12 +202,14 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("f_second") topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, descriptionQuery, topicsQuery) - res, err := client.Search(TableName).Query(boolQ).SortBy(elastic.NewScoreSort(), elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("alias", "description", "topics")).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).SortBy(elastic.NewScoreSort(), elastic.NewFieldSort(SortBy).Order(ascending)).From(from).Size(Size).Highlight(queryHighlight("alias", "description", "topics")).Do(ctx.Req.Context()) if err == nil { searchJson, _ := json.Marshal(res) log.Info("searchJson=" + string(searchJson)) - result := makeRepoResult(res, Key, OnlyReturnNum) - ctx.JSON(200, result) + esresult := makeRepoResult(res, Key, OnlyReturnNum) + resultObj.Total = resultObj.PrivateTotal + esresult.Total + resultObj.Result = append(resultObj.Result, esresult.Result...) + ctx.JSON(200, resultObj) } else { log.Info("query es error," + err.Error()) ctx.JSON(200, "") @@ -200,12 +217,14 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } else { log.Info("query all content.") //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} - res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From(from).Size(Size).Do(ctx.Req.Context()) if err == nil { searchJson, _ := json.Marshal(res) log.Info("searchJson=" + string(searchJson)) - result := makeRepoResult(res, "", OnlyReturnNum) - ctx.JSON(200, result) + esresult := makeRepoResult(res, "", OnlyReturnNum) + resultObj.Total = resultObj.PrivateTotal + esresult.Total + resultObj.Result = append(resultObj.Result, esresult.Result...) + ctx.JSON(200, resultObj) } else { log.Info("query es error," + err.Error()) ctx.JSON(200, "")