|
|
|
@@ -3,6 +3,7 @@ package routers |
|
|
|
import ( |
|
|
|
"code.gitea.io/gitea/modules/context" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
"github.com/olivere/elastic/v7" |
|
|
|
) |
|
|
|
|
|
|
|
@@ -22,44 +23,261 @@ type SearchOptions struct { |
|
|
|
|
|
|
|
var client *elastic.Client |
|
|
|
|
|
|
|
var host = "http://192.168.207.94:9200" |
|
|
|
func InitESClient() { |
|
|
|
ESSearchUrl := setting.ESSearchURL |
|
|
|
var err error |
|
|
|
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(ESSearchUrl)) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func Search(ctx *context.Context) { |
|
|
|
TableName := ctx.Query("TableName") |
|
|
|
Key := ctx.Query("Key") |
|
|
|
//SortBy := ctx.Query("SortBy") |
|
|
|
//Page := ctx.QueryInt64("Page") |
|
|
|
//PageSize := ctx.QueryInt64("PageSize") |
|
|
|
Page := ctx.QueryInt("Page") |
|
|
|
PageSize := ctx.QueryInt("PageSize") |
|
|
|
if Page <= 0 { |
|
|
|
Page = 1 |
|
|
|
} |
|
|
|
if PageSize <= 0 { |
|
|
|
PageSize = setting.UI.IssuePagingNum |
|
|
|
} |
|
|
|
if TableName == "repository" { |
|
|
|
searchRepo(ctx, "repository-es-index", Key, Page, PageSize) |
|
|
|
return |
|
|
|
} else if TableName == "issue" { |
|
|
|
searchIssue(ctx, "issue-es-index", Key, Page, PageSize) |
|
|
|
return |
|
|
|
} else if TableName == "user" { |
|
|
|
searchUserOrOrg(ctx, "user-es-index", Key, Page, PageSize, true) |
|
|
|
return |
|
|
|
} else if TableName == "org" { |
|
|
|
searchUserOrOrg(ctx, "user-es-index", Key, Page, PageSize, false) |
|
|
|
return |
|
|
|
} else if TableName == "dataset" { |
|
|
|
searchDataSet(ctx, "dataset-es-index", Key, Page, PageSize) |
|
|
|
return |
|
|
|
} else if TableName == "pr" { |
|
|
|
searchPR(ctx, "issue-es-index", Key, Page, PageSize) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//ESSearchUrl := setting.RecommentRepoAddr |
|
|
|
// if Key != "" { |
|
|
|
// boolQ := elastic.NewBoolQuery() |
|
|
|
// nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) |
|
|
|
// descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) |
|
|
|
// //owner_idQuery := elastic.NewTermQuery("owner_id", 3) |
|
|
|
// boolQ.Should(nameQuery, descriptionQuery) |
|
|
|
// //boolQ.Must(owner_idQuery) |
|
|
|
// res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) |
|
|
|
// if err == nil { |
|
|
|
// ctx.JSON(200, res) |
|
|
|
// } else { |
|
|
|
// log.Info("query es error," + err.Error()) |
|
|
|
// } |
|
|
|
// } else { |
|
|
|
// log.Info("query all content.") |
|
|
|
// //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} |
|
|
|
// res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) |
|
|
|
// if err == nil { |
|
|
|
// ctx.JSON(200, res) |
|
|
|
// } else { |
|
|
|
// log.Info("query es error," + err.Error()) |
|
|
|
// } |
|
|
|
// } |
|
|
|
} |
|
|
|
|
|
|
|
var err error |
|
|
|
//这个地方有个小坑 不加上elastic.SetSniff(false) 会连接不上 |
|
|
|
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
|
|
|
|
/* |
|
|
|
项目, ES名称: repository-es-index |
|
|
|
搜索: |
|
|
|
name character varying(255) , 项目名称 |
|
|
|
description text, 项目描述 |
|
|
|
topics json, 标签 |
|
|
|
排序: |
|
|
|
updated_unix |
|
|
|
num_watches, |
|
|
|
num_stars, |
|
|
|
num_forks, |
|
|
|
*/ |
|
|
|
|
|
|
|
SortBy := ctx.Query("SortBy") |
|
|
|
if SortBy == "" { |
|
|
|
SortBy = "updated_unix.keyword" |
|
|
|
} |
|
|
|
ascending := ctx.QueryBool("Ascending") |
|
|
|
if Key != "" { |
|
|
|
boolQ := elastic.NewBoolQuery() |
|
|
|
nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) |
|
|
|
descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) |
|
|
|
//owner_idQuery := elastic.NewTermQuery("owner_id", 3) |
|
|
|
boolQ.Should(nameQuery, descriptionQuery) |
|
|
|
//boolQ.Must(owner_idQuery) |
|
|
|
res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) |
|
|
|
topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1) |
|
|
|
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) |
|
|
|
return |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
ctx.JSON(200, "") |
|
|
|
} |
|
|
|
|
|
|
|
func searchRepo(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
/* |
|
|
|
项目, ES名称: repository-es-index |
|
|
|
搜索: |
|
|
|
name character varying(255) , 项目名称 |
|
|
|
description text, 项目描述 |
|
|
|
topics json, 标签 |
|
|
|
排序: |
|
|
|
updated_unix |
|
|
|
num_watches, |
|
|
|
num_stars, |
|
|
|
num_forks, |
|
|
|
*/ |
|
|
|
|
|
|
|
SortBy := ctx.Query("SortBy") |
|
|
|
if SortBy == "" { |
|
|
|
SortBy = "updated_unix.keyword" |
|
|
|
} |
|
|
|
ascending := ctx.QueryBool("Ascending") |
|
|
|
|
|
|
|
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") |
|
|
|
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 { |
|
|
|
ctx.JSON(200, res) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
ctx.JSON(200, "") |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.Info("query all content.") |
|
|
|
//搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} |
|
|
|
res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) |
|
|
|
res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) |
|
|
|
if err == nil { |
|
|
|
ctx.JSON(200, res) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
ctx.JSON(200, "") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { |
|
|
|
/* |
|
|
|
用户或者组织 ES名称: user-es-index |
|
|
|
搜索: |
|
|
|
name , 名称 |
|
|
|
full_name 全名 |
|
|
|
description 描述或者简介 |
|
|
|
排序: |
|
|
|
created_unix |
|
|
|
名称字母序 |
|
|
|
*/ |
|
|
|
SortBy := ctx.Query("SortBy") |
|
|
|
if SortBy == "" { |
|
|
|
SortBy = "updated_unix.keyword" |
|
|
|
} |
|
|
|
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") |
|
|
|
boolQ.Should(nameQuery, full_nameQuery, descriptionQuery) |
|
|
|
} |
|
|
|
typeValue := 1 |
|
|
|
if IsQueryUser { |
|
|
|
typeValue = 0 |
|
|
|
} |
|
|
|
UserOrOrgQuery := elastic.NewTermQuery("type", typeValue) |
|
|
|
boolQ.Must(UserOrOrgQuery) |
|
|
|
|
|
|
|
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) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
ctx.JSON(200, "") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func searchDataSet(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
/* |
|
|
|
数据集,ES名称:dataset-es-index |
|
|
|
搜索: |
|
|
|
title , 名称 |
|
|
|
description 描述 |
|
|
|
category 标签 |
|
|
|
file_name 数据集文件名称 |
|
|
|
排序: |
|
|
|
download_times |
|
|
|
|
|
|
|
*/ |
|
|
|
} |
|
|
|
|
|
|
|
func searchIssue(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
|
|
|
|
/* |
|
|
|
任务,合并请求 ES名称:issue-es-index |
|
|
|
搜索: |
|
|
|
name character varying(255) , 标题 |
|
|
|
content text, 内容 |
|
|
|
comment text, 评论 |
|
|
|
排序: |
|
|
|
updated_unix |
|
|
|
*/ |
|
|
|
|
|
|
|
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") |
|
|
|
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) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func searchPR(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { |
|
|
|
|
|
|
|
/* |
|
|
|
任务,合并请求 ES名称:issue-es-index |
|
|
|
搜索: |
|
|
|
name character varying(255) , 标题 |
|
|
|
content text, 内容 |
|
|
|
comment text, 评论 |
|
|
|
排序: |
|
|
|
updated_unix |
|
|
|
*/ |
|
|
|
|
|
|
|
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") |
|
|
|
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) |
|
|
|
} else { |
|
|
|
log.Info("query es error," + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
} |