From dd6854888630bd2ee6893f34fdf5ea119f0c9e22 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 7 Mar 2022 17:10:29 +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 --- models/dbsql/repo_foreigntable_for_es.sql | 33 ++++++-- models/repo_list.go | 1 + routers/search.go | 91 +++++++++++++++++++++-- 3 files changed, 113 insertions(+), 12 deletions(-) diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 713aa2112..06eeb3ed1 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -44,7 +44,9 @@ CREATE FOREIGN TABLE public.repository_es ( download_cnt bigint DEFAULT 0 NOT NULL, num_commit bigint DEFAULT 0 NOT NULL, git_clone_cnt bigint DEFAULT 0 NOT NULL, - lang character varying(2048) + lang character varying(2048), + alias character varying(255), + lower_alias character varying(255) ) SERVER multicorn_es OPTIONS ( @@ -96,7 +98,11 @@ delete from public.repository_es; balance, clone_cnt, num_commit, - git_clone_cnt,lang) + git_clone_cnt, + lang, + alias, + lower_alias + ) SELECT id, owner_id, @@ -138,7 +144,10 @@ delete from public.repository_es; balance, clone_cnt, num_commit, - git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) + git_clone_cnt, + (select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id), + alias, + lower_alias FROM public.repository b where b.is_private=false; @@ -186,7 +195,9 @@ $def$ balance, clone_cnt, num_commit, - git_clone_cnt) VALUES + git_clone_cnt, + alias, + lower_alias) VALUES (NEW.id, NEW.owner_id, NEW.owner_name, @@ -227,7 +238,9 @@ $def$ NEW.balance, NEW.clone_cnt, NEW.num_commit, - NEW.git_clone_cnt); + NEW.git_clone_cnt, + NEW.alias, + NEW.lower_alias); end if; RETURN NEW; END; @@ -289,7 +302,10 @@ $def$ balance, clone_cnt, num_commit, - git_clone_cnt,lang) + git_clone_cnt, + lang, + alias, + lower_alias) SELECT id, owner_id, @@ -331,7 +347,10 @@ $def$ balance, clone_cnt, num_commit, - git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) + git_clone_cnt, + (select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id), + alias, + lower_alias FROM public.repository b where b.id=NEW.id; INSERT INTO public.dataset_es( id, diff --git a/models/repo_list.go b/models/repo_list.go index 6fb9380de..7053ad5be 100755 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -219,6 +219,7 @@ const ( SearchOrderByDownloadTimes SearchOrderBy = "download_times DESC" SearchOrderByHot SearchOrderBy = "(num_watches + num_stars + num_forks + clone_cnt) DESC" SearchOrderByActive SearchOrderBy = "(num_issues + num_pulls + num_commit) DESC" + SearchOrderByWatches SearchOrderBy = "num_watches DESC" ) // SearchRepositoryCondition creates a query condition according search repository options diff --git a/routers/search.go b/routers/search.go index 6a97ac667..797786c15 100644 --- a/routers/search.go +++ b/routers/search.go @@ -15,8 +15,12 @@ import ( ) type SearchRes struct { - Total int64 - Result []map[string]interface{} + Total int64 + Result []map[string]interface{} + PrivateTotal int64 + PrivatePage int + PublicPage int + PublicTotal int64 } var client *elastic.Client @@ -138,6 +142,40 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa SortBy = "updated_unix.keyword" } 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 + } + + repos, count, err := models.SearchRepository(&models.SearchRepoOptions{ + ListOptions: models.ListOptions{ + Page: Page, + PageSize: PageSize, + }, + Actor: ctx.User, + OrderBy: orderBy, + OnlyPrivate: true, + Keyword: Key, + IncludeDescription: setting.UI.SearchRepoDescription, + }) + if err != nil { + ctx.ServerError("SearchRepository", err) + return + } + privateRe := &SearchRes{} + privateRe.PrivateTotal = count + privateRe.Result = make([]map[string]interface{}, 0) + if count > 0 { + makePrivateRepo(repos, privateRe, Key) + } + log.Info("query searchRepo start") if Key != "" { boolQ := elastic.NewBoolQuery() @@ -171,6 +209,46 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } } +func makePrivateRepo(repos models.RepositoryList, res *SearchRes, keyword string) { + + for _, repo := range repos { + record := make(map[string]interface{}) + record["id"] = repo.ID + record["name"] = makeHighLight(keyword, repo.Name) + record["real_name"] = repo.Name + record["owner_name"] = repo.OwnerName + record["description"] = truncLongText(makeHighLight(keyword, repo.Name), true, keyword) + + hightTopics := make([]string, 0) + if len(repo.Topics) > 0 { + for _, t := range repo.Topics { + hightTopics = append(hightTopics, makeHighLight(keyword, t)) + } + } + record["hightTopics"] = hightTopics + + record["num_watches"] = repo.NumWatches + record["num_stars"] = repo.NumStars + record["num_forks"] = repo.NumForks + record["alias"] = repo.Alias + record["lower_alias"] = repo.LowerAlias + record["topics"] = repo.Topics + record["avatar"] = repo.RelAvatarLink() + record["updated_unix"] = repo.UpdatedUnix + record["lang"] = "" + res.Result = append(res.Result, record) + } +} + +func makeHighLight(keyword string, dest string) string { + textRune := []rune(dest) + index := chineseIndex(textRune, []rune(keyword)) + if index > 0 { + dest = strings.ReplaceAll(dest, keyword, "\u003cfont color='red'\u003e"+keyword+"\u003c/font\u003e") + } + return dest +} + func makeRepoResult(sRes *elastic.SearchResult, Key string, OnlyReturnNum bool) *SearchRes { total := sRes.Hits.TotalHits.Value result := make([]map[string]interface{}, 0) @@ -199,7 +277,8 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string, OnlyReturnNum bool) record["num_watches"] = recordSource["num_watches"] record["num_stars"] = recordSource["num_stars"] record["num_forks"] = recordSource["num_forks"] - + record["alias"] = recordSource["alias"] + record["lower_alias"] = recordSource["lower_alias"] if recordSource["topics"] != nil { topicsStr := recordSource["topics"].(string) log.Info("topicsStr=" + topicsStr) @@ -251,11 +330,14 @@ func dealLongText(text string, Key string, MatchedQueries []string) string { isNeedToDealText = true } } + return truncLongText(text, isNeedToDealText, "color=") +} +func truncLongText(text string, isNeedToDealText bool, keyword string) string { textRune := []rune(text) stringlen := len(textRune) if isNeedToDealText && stringlen > 200 { - index := chineseIndex(textRune, []rune("color=")) + index := chineseIndex(textRune, []rune(keyword)) if index > 0 { start := index - 50 if start < 0 { @@ -276,7 +358,6 @@ func dealLongText(text string, Key string, MatchedQueries []string) string { return text } } - } func chineseIndex(text []rune, childText []rune) int {