diff --git a/public/home/search.js b/public/home/search.js index 2321803db..6a96512e0 100644 --- a/public/home/search.js +++ b/public/home/search.js @@ -72,7 +72,7 @@ function search(){ } } -function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy){ +function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy,OnlySearchLabel=false){ $.ajax({ type:"GET", url:"/all/dosearch/", @@ -87,7 +87,8 @@ function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy){ 'Page': page, 'PageSize': pageSize, 'OnlyReturnNum':onlyReturnNum, - 'SortBy':sortBy + 'SortBy':sortBy, + 'OnlySearchLabel':OnlySearchLabel }, async:true, success:function(json){ @@ -405,7 +406,8 @@ function displayRepoResult(page,jsonResult,onlyReturnNum,keyword){ html += "
"; if(!isEmpty(recordMap["topics"]) && recordMap["topics"] !="null"){ for(var j = 0; j < recordMap["topics"].length;j++){ - html +="
"+ recordMap["topics"][j] + "
"; + //function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy){ + html +="
"+ recordMap["topics"][j] + "
"; } } html +="
"; diff --git a/routers/search.go b/routers/search.go index 1d7a5b183..4a2e727df 100644 --- a/routers/search.go +++ b/routers/search.go @@ -44,6 +44,7 @@ func SearchApi(ctx *context.Context) { Page := ctx.QueryInt("Page") PageSize := ctx.QueryInt("PageSize") OnlyReturnNum := ctx.QueryBool("OnlyReturnNum") + OnlySearchLabel := ctx.QueryBool("OnlySearchLabel") if Page <= 0 { Page = 1 } @@ -52,7 +53,11 @@ func SearchApi(ctx *context.Context) { } if TableName == "repository" { - searchRepo(ctx, "repository-es-index", Key, Page, PageSize, OnlyReturnNum) + if OnlySearchLabel { + searchRepoByLabel(ctx, Key, Page, PageSize) + } else { + searchRepo(ctx, "repository-es-index", Key, Page, PageSize, OnlyReturnNum) + } return } else if TableName == "issue" { searchIssue(ctx, "issue-es-index", Key, Page, PageSize, OnlyReturnNum) @@ -72,7 +77,7 @@ func SearchApi(ctx *context.Context) { } } -func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page int, PageSize int, OnlyReturnNum bool) { +func searchRepoByLabel(ctx *context.Context, Key string, Page int, PageSize int) { /* 项目, ES名称: repository-es-index 搜索: @@ -95,13 +100,16 @@ func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page boolQ := elastic.NewBoolQuery() 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()) + + res, err := client.Search("repository-es-index").Query(boolQ).SortBy(elastic.NewScoreSort(), elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("topics")).Do(ctx.Req.Context()) if err == nil { - result := makeRepoResult(res, "", OnlyReturnNum) + searchJson, _ := json.Marshal(res) + log.Info("searchJson=" + string(searchJson)) + result := makeRepoResult(res, "", false) ctx.JSON(200, result) - return } else { log.Info("query es error," + err.Error()) + ctx.JSON(200, "") } } ctx.JSON(200, "") @@ -182,6 +190,9 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string, OnlyReturnNum bool) } else { record["description"] = "" } + if Key == "" { + record["hightTopics"] = getLabelValue("topics", recordSource, hit.Highlight) + } record["num_watches"] = recordSource["num_watches"] record["num_stars"] = recordSource["num_stars"]