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