| @@ -3,6 +3,7 @@ package routers | |||
| import ( | |||
| "encoding/json" | |||
| "fmt" | |||
| "strings" | |||
| "code.gitea.io/gitea/modules/context" | |||
| "code.gitea.io/gitea/modules/log" | |||
| @@ -120,7 +121,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa | |||
| 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 { | |||
| result := makeRepoResult(res) | |||
| result := makeRepoResult(res, Key) | |||
| ctx.JSON(200, result) | |||
| } else { | |||
| log.Info("query es error," + err.Error()) | |||
| @@ -131,7 +132,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa | |||
| //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} | |||
| res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) | |||
| if err == nil { | |||
| result := makeRepoResult(res) | |||
| result := makeRepoResult(res, "") | |||
| ctx.JSON(200, result) | |||
| } else { | |||
| log.Info("query es error," + err.Error()) | |||
| @@ -140,7 +141,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa | |||
| } | |||
| } | |||
| func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { | |||
| func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { | |||
| total := sRes.Hits.TotalHits.Value | |||
| result := make([]map[string]interface{}, 0) | |||
| @@ -148,13 +149,44 @@ func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { | |||
| log.Info("this is " + 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 { | |||
| record := make(map[string]interface{}) | |||
| record["name"] = recordSource["name"] | |||
| record["owner_name"] = recordSource["owner_name"] | |||
| record["description"] = recordSource["description"] | |||
| 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["num_watches"] = recordSource["num_watches"] | |||
| record["num_stars"] = recordSource["num_stars"] | |||
| record["num_forks"] = recordSource["num_forks"] | |||
| @@ -169,7 +201,6 @@ func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { | |||
| } else { | |||
| log.Info("deal source error," + err.Error()) | |||
| } | |||
| } | |||
| returnObj := &SearchRes{ | |||
| @@ -178,7 +209,6 @@ func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { | |||
| } | |||
| return returnObj | |||
| } | |||
| func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { | |||