Browse Source

提交代码。

Signed-off-by: zouap <zouap@pcl.ac.cn>
tags/v1.22.3.2^2
zouap 3 years ago
parent
commit
657383658a
2 changed files with 21 additions and 8 deletions
  1. +5
    -3
      public/home/search.js
  2. +16
    -5
      routers/search.go

+ 5
- 3
public/home/search.js View File

@@ -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 += " <div class=\"ui tags\">";
if(!isEmpty(recordMap["topics"]) && recordMap["topics"] !="null"){
for(var j = 0; j < recordMap["topics"].length;j++){
html +=" <a href=\"/explore/repos?q=" + recordMap["topics"][j] + "&amp;topic=\"><div class=\"ui small label topic\">"+ recordMap["topics"][j] + "</div></a>";
//function doSearch(tableName,keyword,page,pageSize=15,onlyReturnNum=true,sortBy){
html +=" <a href=\"javascript:doSearch('repository','" + recordMap["topics"][j] + "',1,15,false,'updated_unix.keyword',true);\"><div class=\"ui small label topic\">"+ recordMap["topics"][j] + "</div></a>";
}
}
html +=" </div>";


+ 16
- 5
routers/search.go View File

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


Loading…
Cancel
Save