package routers import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "github.com/olivere/elastic/v7" ) type Table struct { TableName string SpecifyField string SortBy string Where string } type SearchOptions struct { Page int64 PageSize int64 Key string SearchObj []Table } var client *elastic.Client var host = "http://192.168.207.94:9200" 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") //ESSearchUrl := setting.RecommentRepoAddr var err error //这个地方有个小坑 不加上elastic.SetSniff(false) 会连接不上 client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) if err != nil { panic(err) } if Key != "" { boolQ := elastic.NewBoolQuery() nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) boolQ.Should(nameQuery, descriptionQuery) res, err := client.Search(TableName+"-es-index").Query(boolQ).Sort("updated_unix", false).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } } else { log.Info("query all content.") res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } } }