diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 686109b06..5fb91cd23 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -266,6 +266,8 @@ CREATE TRIGGER es_insert_repository ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_insert_repository; +DROP TRIGGER IF EXISTS es_update_repository on public.repository; + CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS $def$ BEGIN @@ -454,6 +456,7 @@ $def$ end if; if not NEW.is_private then + raise notice 'update repo,the updated_unix is %',NEW.updated_unix; update public.repository_es SET description=NEW.description, name=NEW.name, lower_name=NEW.lower_name, @@ -467,14 +470,12 @@ $def$ alias = NEW.alias, lower_alias = NEW.lower_alias where id=NEW.id; - end if; return new; END $def$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS es_update_repository on public.repository; CREATE TRIGGER es_update_repository AFTER UPDATE ON public.repository FOR EACH ROW EXECUTE PROCEDURE update_repository(); @@ -500,20 +501,27 @@ CREATE TRIGGER es_delete_repository ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_delete_repository; + + +DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat; + CREATE OR REPLACE FUNCTION public.udpate_repository_lang() RETURNS trigger AS $def$ BEGIN - if (TG_OP = 'UPDATE') then + if (TG_OP = 'UPDATE') then update public.repository_es SET lang=(select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id; elsif (TG_OP = 'INSERT') then update public.repository_es SET lang=(select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id; - end if; + elsif (TG_OP = 'DELETE') then + if exists(select 1 from public.repository where id=OLD.repo_id) + update public.repository_es SET lang=(select array_to_string(array_agg(language order by percentage desc),',') from public.language_stat where repo_id=OLD.repo_id) where id=OLD.repo_id; + end if; + end if; return null; END; $def$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat; CREATE TRIGGER es_udpate_repository_lang AFTER INSERT OR UPDATE OR DELETE ON public.language_stat FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang(); diff --git a/models/repo_list.go b/models/repo_list.go index de4938604..5bf0ecf03 100755 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -190,7 +190,8 @@ type SearchRepoOptions struct { // None -> include all repos // True -> include just courses // False -> include just no courses - Course util.OptionalBool + Course util.OptionalBool + OnlySearchPrivate bool } //SearchOrderBy is used to sort the result @@ -225,7 +226,9 @@ const ( // SearchRepositoryCondition creates a query condition according search repository options func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond { var cond = builder.NewCond() - + if opts.OnlySearchPrivate { + cond = cond.And(builder.Eq{"is_private": true}) + } if opts.Private { if opts.Actor != nil && !opts.Actor.IsAdmin && opts.Actor.ID != opts.OwnerID { // OK we're in the context of a User diff --git a/routers/search.go b/routers/search.go index 3a8c323ca..ce745c077 100644 --- a/routers/search.go +++ b/routers/search.go @@ -250,6 +250,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa OnlyPrivate: true, Keyword: Key, IncludeDescription: setting.UI.SearchRepoDescription, + OnlySearchPrivate: true, }) if err != nil { ctx.JSON(200, "") @@ -297,7 +298,14 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa log.Info("searchJson=" + string(searchJson)) esresult := makeRepoResult(res, Key, OnlyReturnNum) resultObj.Total = resultObj.PrivateTotal + esresult.Total + isNeedSort := false + if len(resultObj.Result) > 0 { + isNeedSort = true + } resultObj.Result = append(resultObj.Result, esresult.Result...) + if isNeedSort { + + } ctx.JSON(200, resultObj) } else { log.Info("query es error," + err.Error()) @@ -321,6 +329,20 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } } +// func sortRepo(Result []map[string]interface{}, SortBy string, ascending bool){ +// orderBy :="" +// switch SortBy { +// case "updated_unix.keyword": +// orderBy = "updated_unix" +// case "num_stars": +// orderBy = "num_stars" +// case "num_forks": +// orderBy = models.SearchOrderByForksReverse +// case "num_watches": +// orderBy = models.SearchOrderByWatches +// } +// } + func makePrivateRepo(repos models.RepositoryList, res *SearchRes, keyword string) { for _, repo := range repos {