Browse Source

提交代码。

Signed-off-by: zouap <zouap@pcl.ac.cn>
tags/v1.22.3.2^2
zouap 4 years ago
parent
commit
dd68548886
3 changed files with 113 additions and 12 deletions
  1. +26
    -7
      models/dbsql/repo_foreigntable_for_es.sql
  2. +1
    -0
      models/repo_list.go
  3. +86
    -5
      routers/search.go

+ 26
- 7
models/dbsql/repo_foreigntable_for_es.sql View File

@@ -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,


+ 1
- 0
models/repo_list.go View File

@@ -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


+ 86
- 5
routers/search.go View File

@@ -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 {


Loading…
Cancel
Save