|
|
|
@@ -3,8 +3,10 @@ package routers |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/models" |
|
|
|
"code.gitea.io/gitea/modules/context" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
@@ -35,9 +37,10 @@ func Search(ctx *context.Context) { |
|
|
|
if Page <= 0 { |
|
|
|
Page = 1 |
|
|
|
} |
|
|
|
if PageSize <= 0 { |
|
|
|
if PageSize <= 0 || PageSize > 200 { |
|
|
|
PageSize = setting.UI.IssuePagingNum |
|
|
|
} |
|
|
|
|
|
|
|
if TableName == "repository" { |
|
|
|
searchRepo(ctx, "repository-es-index", Key, Page, PageSize) |
|
|
|
return |
|
|
|
@@ -80,11 +83,12 @@ func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page |
|
|
|
log.Info("query searchRepoByLabel start") |
|
|
|
if Key != "" { |
|
|
|
boolQ := elastic.NewBoolQuery() |
|
|
|
topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1) |
|
|
|
topicsQuery := elastic.NewMatchQuery("topics", Key) |
|
|
|
boolQ.Should(topicsQuery) |
|
|
|
res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) |
|
|
|
if err == nil { |
|
|
|
ctx.JSON(200, res) |
|
|
|
result := makeRepoResult(res, "") |
|
|
|
ctx.JSON(200, result) |
|
|
|
return |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
@@ -115,9 +119,9 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa |
|
|
|
log.Info("query searchRepo start") |
|
|
|
if Key != "" { |
|
|
|
boolQ := elastic.NewBoolQuery() |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") |
|
|
|
descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("desc_second") |
|
|
|
topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("topics_third") |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") |
|
|
|
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).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) |
|
|
|
if err == nil { |
|
|
|
@@ -146,16 +150,10 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { |
|
|
|
result := make([]map[string]interface{}, 0) |
|
|
|
|
|
|
|
for i, hit := range sRes.Hits.Hits { |
|
|
|
log.Info("this is " + fmt.Sprint(i) + " result.") |
|
|
|
log.Info("this is repo query " + fmt.Sprint(i) + " result.") |
|
|
|
recordSource := make(map[string]interface{}) |
|
|
|
source, err := hit.Source.MarshalJSON() |
|
|
|
var isNeedToDealText bool |
|
|
|
isNeedToDealText = false |
|
|
|
if len(hit.MatchedQueries) > 0 && Key != "" { |
|
|
|
if hit.MatchedQueries[0] == "desc_second" || hit.MatchedQueries[0] == "topics_third" { |
|
|
|
isNeedToDealText = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
err = json.Unmarshal(source, &recordSource) |
|
|
|
if err == nil { |
|
|
|
@@ -163,35 +161,16 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { |
|
|
|
record["name"] = recordSource["name"] |
|
|
|
record["owner_name"] = recordSource["owner_name"] |
|
|
|
desc := recordSource["description"].(string) |
|
|
|
stringlen := len(desc) |
|
|
|
if isNeedToDealText && stringlen > 200 { |
|
|
|
index := strings.Index(desc, Key) |
|
|
|
if index > 0 { |
|
|
|
start := index - 50 |
|
|
|
if start < 0 { |
|
|
|
start = 0 |
|
|
|
} |
|
|
|
end := index + 150 |
|
|
|
if end >= stringlen { |
|
|
|
end = stringlen |
|
|
|
} |
|
|
|
record["description"] = desc[start:end] |
|
|
|
} else { |
|
|
|
record["description"] = desc[0:200] |
|
|
|
} |
|
|
|
} else { |
|
|
|
if stringlen > 200 { |
|
|
|
record["description"] = desc[0:200] |
|
|
|
} else { |
|
|
|
record["description"] = desc |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
record["description"] = dealLongText(desc, Key, hit.MatchedQueries) |
|
|
|
|
|
|
|
record["num_watches"] = recordSource["num_watches"] |
|
|
|
record["num_stars"] = recordSource["num_stars"] |
|
|
|
record["num_forks"] = recordSource["num_forks"] |
|
|
|
record["topics"] = recordSource["topics"] |
|
|
|
record["avatar"] = recordSource["avatar"] |
|
|
|
if recordSource["avatar"] != nil { |
|
|
|
record["avatar"] = setting.AppSubURL + "/repo-avatars/" + recordSource["avatar"].(string) |
|
|
|
} |
|
|
|
record["updated_unix"] = recordSource["updated_unix"] |
|
|
|
record["lang"] = recordSource["lang"] |
|
|
|
result = append(result, record) |
|
|
|
@@ -211,6 +190,40 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { |
|
|
|
return returnObj |
|
|
|
} |
|
|
|
|
|
|
|
func dealLongText(text string, Key string, MatchedQueries []string) string { |
|
|
|
var isNeedToDealText bool |
|
|
|
isNeedToDealText = false |
|
|
|
if len(MatchedQueries) > 0 && Key != "" { |
|
|
|
if MatchedQueries[0] == "f_second" || MatchedQueries[0] == "f_third" { |
|
|
|
isNeedToDealText = true |
|
|
|
} |
|
|
|
} |
|
|
|
stringlen := len(text) |
|
|
|
if isNeedToDealText && stringlen > 200 { |
|
|
|
index := strings.Index(text, Key) |
|
|
|
if index > 0 { |
|
|
|
start := index - 50 |
|
|
|
if start < 0 { |
|
|
|
start = 0 |
|
|
|
} |
|
|
|
end := index + 150 |
|
|
|
if end >= stringlen { |
|
|
|
end = stringlen |
|
|
|
} |
|
|
|
return text[start:end] |
|
|
|
} else { |
|
|
|
return text[0:200] |
|
|
|
} |
|
|
|
} else { |
|
|
|
if stringlen > 200 { |
|
|
|
return text[0:200] |
|
|
|
} else { |
|
|
|
return text |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { |
|
|
|
/* |
|
|
|
用户或者组织 ES名称: user-es-index |
|
|
|
@@ -229,9 +242,9 @@ func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page in |
|
|
|
ascending := ctx.QueryBool("Ascending") |
|
|
|
boolQ := elastic.NewBoolQuery() |
|
|
|
if Key != "" { |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") |
|
|
|
full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("fullname_second") |
|
|
|
descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("desc_third") |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") |
|
|
|
full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("f_second") |
|
|
|
descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("f_third") |
|
|
|
boolQ.Should(nameQuery, full_nameQuery, descriptionQuery) |
|
|
|
} |
|
|
|
typeValue := 1 |
|
|
|
@@ -243,13 +256,64 @@ func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page in |
|
|
|
|
|
|
|
res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) |
|
|
|
if err == nil { |
|
|
|
ctx.JSON(200, res) |
|
|
|
result := makeUserOrOrgResult(res, Key, ctx) |
|
|
|
ctx.JSON(200, result) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
ctx.JSON(200, "") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Context) *SearchRes { |
|
|
|
total := sRes.Hits.TotalHits.Value |
|
|
|
result := make([]map[string]interface{}, 0) |
|
|
|
|
|
|
|
for i, hit := range sRes.Hits.Hits { |
|
|
|
log.Info("this is user query " + fmt.Sprint(i) + " result.") |
|
|
|
recordSource := make(map[string]interface{}) |
|
|
|
source, err := hit.Source.MarshalJSON() |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
err = json.Unmarshal(source, &recordSource) |
|
|
|
if err == nil { |
|
|
|
record := make(map[string]interface{}) |
|
|
|
record["name"] = recordSource["name"] |
|
|
|
record["full_name"] = recordSource["full_name"] |
|
|
|
desc := recordSource["description"].(string) |
|
|
|
|
|
|
|
record["description"] = dealLongText(desc, Key, hit.MatchedQueries) |
|
|
|
if ctx.User != nil { |
|
|
|
record["email"] = recordSource["email"] |
|
|
|
} else { |
|
|
|
record["email"] = "" |
|
|
|
} |
|
|
|
|
|
|
|
record["location"] = recordSource["location"] |
|
|
|
record["website"] = recordSource["website"] |
|
|
|
record["num_repos"] = recordSource["num_repos"] |
|
|
|
record["num_teams"] = recordSource["num_teams"] |
|
|
|
record["num_members"] = recordSource["num_members"] |
|
|
|
|
|
|
|
record["avatar"] = strings.TrimRight(setting.AppSubURL, "/") + "/user/avatar/" + recordSource["name"].(string) + "/" + strconv.Itoa(-1) |
|
|
|
record["updated_unix"] = recordSource["updated_unix"] |
|
|
|
record["created_unix"] = recordSource["created_unix"] |
|
|
|
result = append(result, record) |
|
|
|
} else { |
|
|
|
log.Info("deal source error," + err.Error()) |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.Info("deal source error," + err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
returnObj := &SearchRes{ |
|
|
|
Total: total, |
|
|
|
Result: result, |
|
|
|
} |
|
|
|
|
|
|
|
return returnObj |
|
|
|
} |
|
|
|
|
|
|
|
func searchDataSet(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
/* |
|
|
|
数据集,ES名称:dataset-es-index |
|
|
|
@@ -278,22 +342,70 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P |
|
|
|
|
|
|
|
boolQ := elastic.NewBoolQuery() |
|
|
|
if Key != "" { |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") |
|
|
|
contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("content_second") |
|
|
|
commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("comment_third") |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") |
|
|
|
contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") |
|
|
|
commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") |
|
|
|
boolQ.Should(nameQuery, contentQuery, commentQuery) |
|
|
|
} |
|
|
|
isIssueQuery := elastic.NewTermQuery("is_pull", false) |
|
|
|
boolQ.Must(isIssueQuery) |
|
|
|
res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) |
|
|
|
if err == nil { |
|
|
|
ctx.JSON(200, res) |
|
|
|
result := makeIssueResult(res, Key) |
|
|
|
ctx.JSON(200, result) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { |
|
|
|
total := sRes.Hits.TotalHits.Value |
|
|
|
result := make([]map[string]interface{}, 0) |
|
|
|
|
|
|
|
for i, hit := range sRes.Hits.Hits { |
|
|
|
log.Info("this is issue query " + fmt.Sprint(i) + " result.") |
|
|
|
recordSource := make(map[string]interface{}) |
|
|
|
source, err := hit.Source.MarshalJSON() |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
err = json.Unmarshal(source, &recordSource) |
|
|
|
if err == nil { |
|
|
|
record := make(map[string]interface{}) |
|
|
|
record["id"] = recordSource["id"] |
|
|
|
record["repo_id"] = recordSource["repo_id"] |
|
|
|
repo, errRepo := models.GetRepositoryByID(recordSource["repo_id"].(int64)) |
|
|
|
if errRepo == nil { |
|
|
|
record["repoUrl"] = repo.FullName() |
|
|
|
} |
|
|
|
record["name"] = recordSource["name"] |
|
|
|
desc := recordSource["content"].(string) |
|
|
|
record["content"] = dealLongText(desc, Key, hit.MatchedQueries) |
|
|
|
if recordSource["pr_id"] != nil { |
|
|
|
record["pr_id"] = recordSource["pr_id"] |
|
|
|
} |
|
|
|
|
|
|
|
desc = recordSource["comment"].(string) |
|
|
|
record["comment"] = dealLongText(desc, Key, hit.MatchedQueries) |
|
|
|
record["num_comments"] = recordSource["num_comments"] |
|
|
|
record["updated_unix"] = recordSource["updated_unix"] |
|
|
|
result = append(result, record) |
|
|
|
} else { |
|
|
|
log.Info("deal source error," + err.Error()) |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.Info("deal source error," + err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
returnObj := &SearchRes{ |
|
|
|
Total: total, |
|
|
|
Result: result, |
|
|
|
} |
|
|
|
|
|
|
|
return returnObj |
|
|
|
} |
|
|
|
|
|
|
|
func searchPR(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
|
|
|
|
/* |
|
|
|
@@ -308,16 +420,17 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page |
|
|
|
|
|
|
|
boolQ := elastic.NewBoolQuery() |
|
|
|
if Key != "" { |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") |
|
|
|
contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("content_second") |
|
|
|
commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("comment_third") |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") |
|
|
|
contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") |
|
|
|
commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") |
|
|
|
boolQ.Should(nameQuery, contentQuery, commentQuery) |
|
|
|
} |
|
|
|
isIssueQuery := elastic.NewTermQuery("is_pull", true) |
|
|
|
boolQ.Must(isIssueQuery) |
|
|
|
res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) |
|
|
|
if err == nil { |
|
|
|
ctx.JSON(200, res) |
|
|
|
result := makeIssueResult(res, Key) |
|
|
|
ctx.JSON(200, result) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
} |
|
|
|
|