From 332f67d1c1932c8f8f769e5c9f6e02feaafb1f17 Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 22 Dec 2021 14:11:14 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E7=BD=91?= =?UTF-8?q?=E7=AB=99=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/website/api/es.go | 40 ++++++ app/api/website/api/technology.go | 62 +++++++++ app/api/website/controller/manage/search.go | 121 ++++++++++++++++++ .../website/controller/technology/demand.go | 100 +++++++++++++++ .../website/controller/technology/patent.go | 78 +++++++++++ app/api/website/model/sys_patent.go | 34 +++++ app/common/model/common.go | 2 +- app/common/model/sys_patent.go | 4 +- app/logic/ies.go | 7 + app/service/es.go | 88 +++++++++++++ router/address.go | 16 +++ serve/es/es.go | 41 +++--- serve/es/es_test.go | 69 ++++++++-- serve/es/serve.go | 3 + 14 files changed, 637 insertions(+), 28 deletions(-) create mode 100644 app/api/website/api/es.go create mode 100644 app/api/website/api/technology.go create mode 100644 app/api/website/controller/manage/search.go create mode 100644 app/api/website/controller/technology/demand.go create mode 100644 app/api/website/controller/technology/patent.go create mode 100644 app/logic/ies.go create mode 100644 app/service/es.go diff --git a/app/api/website/api/es.go b/app/api/website/api/es.go new file mode 100644 index 0000000..dda1c9e --- /dev/null +++ b/app/api/website/api/es.go @@ -0,0 +1,40 @@ +package api + +import ( + "SciencesServer/app/basic/api" + "SciencesServer/app/service" + "github.com/gin-gonic/gin" +) + +type ES struct{} + +func (*ES) Create(c *gin.Context) { + form := &struct { + ID uint64 `json:"id" form:"id"` + Title string `json:"title" form:"title"` + Keyword string `json:"keyword" form:"keyword"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + manage := service.NewManage( + service.WithManageID(form.ID), + service.WithManageTitle(form.Title), + service.WithManageKeyword(form.Keyword), + ) + api.APIResponse(manage.Create())(c) +} + +func (*ES) Search(c *gin.Context) { + form := &struct { + Params map[string]interface{} `json:"params" form:"params"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + manage := service.NewManage() + data, err := manage.Search(form.Params) + api.APIResponse(err, data)(c) +} diff --git a/app/api/website/api/technology.go b/app/api/website/api/technology.go new file mode 100644 index 0000000..04a6c43 --- /dev/null +++ b/app/api/website/api/technology.go @@ -0,0 +1,62 @@ +package api + +import ( + "SciencesServer/app/api/website/controller/technology" + "SciencesServer/app/basic/api" + "github.com/gin-gonic/gin" +) + +type Technology struct{} + +func (*Technology) Patent(c *gin.Context) { + form := &struct { + Title string `json:"title" form:"title"` + Industry string `json:"industry" form:"industry"` + api.PageForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := technology.NewPatent()().Instance(form.Title, form.Industry, form.Page, form.PageSize) + api.APIResponse(err, data)(c) +} + +func (*Technology) PatentDetail(c *gin.Context) { + form := &struct { + api.IDStringForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := technology.NewPatent()().Detail(form.Convert()) + api.APIResponse(err, data)(c) +} + +func (*Technology) Demand(c *gin.Context) { + form := &struct { + Title string `json:"title" form:"title"` + Industry string `json:"industry" form:"industry"` + Kind string `json:"kind" form:"kind"` + api.PageForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := technology.NewDemand()(nil).Instance(form.Title, form.Industry, form.Kind, form.Page, form.PageSize) + api.APIResponse(err, data)(c) +} + +func (*Technology) DemandDetail(c *gin.Context) { + form := &struct { + api.IDStringForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := technology.NewDemand()(nil).Detail(form.Convert()) + api.APIResponse(err, data)(c) +} diff --git a/app/api/website/controller/manage/search.go b/app/api/website/controller/manage/search.go new file mode 100644 index 0000000..90fc02a --- /dev/null +++ b/app/api/website/controller/manage/search.go @@ -0,0 +1,121 @@ +package manage + +import ( + "SciencesServer/app/api/website/model" + "SciencesServer/app/basic/config" + model2 "SciencesServer/app/common/model" + "SciencesServer/app/service" + "errors" +) + +type Search struct{} + +type SearchHandle func() *Search + +type ( + // SearchParams 搜索参数 + SearchParams struct { + } +) + +// searchIdentityHandle 搜索信息处理 +var searchIdentityHandle = map[int]func(ids []uint64) (interface{}, error){ + config.TenantUserIdentityForCompany: company, + config.TenantUserIdentityForExpert: company, + config.TenantUserIdentityForResearch: company, + config.TenantUserIdentityForLaboratory: company, + config.TenantUserIdentityForAgent: company, +} + +func company(ids []uint64) (interface{}, error) { + mManageCompany := model.NewManageCompany() + out := make([]*model2.ManageCompany, 0) + + if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{ + Where: model2.NewWhereIn("id", ids), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }, &model2.ModelWhereOrder{ + Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree), + }); err != nil { + return nil, err + } + return out, nil +} + +func expert(ids []uint64) (interface{}, error) { + mManageCompany := model.NewManageCompany() + out := make([]*model2.ManageCompany, 0) + + if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{ + Where: model2.NewWhereIn("id", ids), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }, &model2.ModelWhereOrder{ + Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree), + }); err != nil { + return nil, err + } + return out, nil +} + +func research(ids []uint64) (interface{}, error) { + mManageCompany := model.NewManageCompany() + out := make([]*model2.ManageCompany, 0) + + if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{ + Where: model2.NewWhereIn("id", ids), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }, &model2.ModelWhereOrder{ + Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree), + }); err != nil { + return nil, err + } + return out, nil +} + +func laboratory(ids []uint64) (interface{}, error) { + mManageCompany := model.NewManageCompany() + out := make([]*model2.ManageCompany, 0) + + if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{ + Where: model2.NewWhereIn("id", ids), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }, &model2.ModelWhereOrder{ + Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree), + }); err != nil { + return nil, err + } + return out, nil +} + +func (c *Search) Launch(identity int, params string) (interface{}, error) { + manage := service.NewManage() + + _params := map[string]interface{}{ + "title": params, "keyword": params, "research": params, + } + data, err := manage.Search(_params) + + if err != nil { + return nil, err + } + ids := make([]uint64, 0) + + for _, v := range data.([]*service.Manage) { + if v.Identity != identity { + continue + } + ids = append(ids, v.ID) + } + handle, has := searchIdentityHandle[identity] + + if !has { + return nil, errors.New("操作错误,未知的身份信息") + } + return handle(ids) +} + +func NewSearch() SearchHandle { + return func() *Search { + return &Search{} + } +} diff --git a/app/api/website/controller/technology/demand.go b/app/api/website/controller/technology/demand.go new file mode 100644 index 0000000..bad316b --- /dev/null +++ b/app/api/website/controller/technology/demand.go @@ -0,0 +1,100 @@ +package technology + +import ( + "SciencesServer/app/api/website/model" + "SciencesServer/app/basic/controller" + model2 "SciencesServer/app/common/model" + "SciencesServer/app/session" + "errors" + "fmt" + "time" +) + +type Demand struct { + *session.Enterprise +} + +type DemandHandle func(session *session.Enterprise) *Demand + +type ( + // DemandInfo 需求基本信息 + DemandInfo struct { + ID string `json:"id"` + Kinds []string `json:"kinds"` + Industrys []string `json:"industrys"` + Deadline time.Time `json:"deadline"` + } + // DemandDetailInfo 需求详细信息 + DemandDetailInfo struct { + ID string `json:"id"` + *model2.TechnologyDemand + Kinds []string `json:"kinds"` + Industrys []string `json:"industrys"` + } +) + +// Instance 需求信息 +func (c *Demand) Instance(title, industry, kind string, page, pageSize int) (*controller.ReturnPages, error) { + mTechnologyDemand := model.NewTechnologyDemand() + + out := make([]*model2.TechnologyDemand, 0) + + var count int64 + + where := []*model2.ModelWhereOrder{ + &model2.ModelWhereOrder{ + Where: model2.NewWhere("status", model2.TechnologyDemandStatusForAgree), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }, + } + if title != "" { + where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhere("title", title)}) + } + if industry != "" { + where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhereCondition("industry", "LIKE", + "%"+fmt.Sprintf(`"%v`, industry)+"%")}) + } + if kind != "" { + where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhereLike("kind", kind)}) + } + err := model2.PagesFields(mTechnologyDemand.TechnologyDemand, &out, []string{}, page, pageSize, &count, where...) + + if err != nil { + return nil, err + } + list := make([]*DemandInfo, 0) + + for _, v := range out { + list = append(list, &DemandInfo{ + ID: v.GetEncodeID(), Kinds: v.GetKindAttribute(), + Industrys: v.GetIndustryAttribute(), Deadline: v.Deadline, + }) + } + return &controller.ReturnPages{Data: list, Count: count}, nil +} + +// Detail 详细信息 +func (c *Demand) Detail(id uint64) (*DemandDetailInfo, error) { + mTechnologyDemand := model.NewTechnologyDemand() + mTechnologyDemand.ID = id + + isExist, err := model2.First(mTechnologyDemand.TechnologyDemand) + + if err != nil { + return nil, err + } else if !isExist { + return nil, errors.New("操作错误,需求信息不存在或已被删除") + } + return &DemandDetailInfo{ + ID: mTechnologyDemand.GetEncodeID(), + TechnologyDemand: mTechnologyDemand.TechnologyDemand, + Kinds: mTechnologyDemand.GetKindAttribute(), + Industrys: mTechnologyDemand.GetIndustryAttribute(), + }, nil +} + +func NewDemand() DemandHandle { + return func(session *session.Enterprise) *Demand { + return &Demand{Enterprise: session} + } +} diff --git a/app/api/website/controller/technology/patent.go b/app/api/website/controller/technology/patent.go new file mode 100644 index 0000000..f173622 --- /dev/null +++ b/app/api/website/controller/technology/patent.go @@ -0,0 +1,78 @@ +package technology + +import ( + "SciencesServer/app/api/website/model" + "SciencesServer/app/basic/controller" + model2 "SciencesServer/app/common/model" + "errors" + "fmt" +) + +type Patent struct{} + +type PatentHandle func() *Patent + +type ( + // PatentInfo 专利信息 + PatentInfo struct { + ID string `json:"id"` + *model.SysPatentInfo + } + // PatentDetailInfo 专利详细信息 + PatentDetailInfo struct { + ID string `json:"id"` + *model2.SysPatent + } +) + +// Instance 查询信息 +func (c *Patent) Instance(title, industry string, page, pageSize int) (*controller.ReturnPages, error) { + mSysPatent := model.NewSysPatent() + + where := make([]*model2.ModelWhere, 0) + + if title != "" { + where = append(where, model2.NewWhereLike("p.title", title)) + } + if industry != "" { + where = append(where, model2.NewWhereCondition("c.industry_detail", "LIKE", + "%"+fmt.Sprintf(`"%v`, industry)+"%")) + } + var count int64 + + out, err := mSysPatent.Patent(page, pageSize, &count, where...) + + if err != nil { + return nil, err + } + list := make([]*PatentInfo, 0) + + for _, v := range out { + list = append(list, &PatentInfo{ + ID: v.GetEncodeID(), + SysPatentInfo: v, + }) + } + return &controller.ReturnPages{Data: list, Count: count}, nil +} + +// Detail 详细信息 +func (c *Patent) Detail(id uint64) (*PatentDetailInfo, error) { + mSysPatent := model.NewSysPatent() + mSysPatent.ID = id + + isExist, err := model2.First(mSysPatent.SysPatent) + + if err != nil { + return nil, err + } else if !isExist { + return nil, errors.New("操作错误,专利信息不存在或已被删除") + } + return &PatentDetailInfo{ID: mSysPatent.GetEncodeID(), SysPatent: mSysPatent.SysPatent}, nil +} + +func NewPatent() PatentHandle { + return func() *Patent { + return &Patent{} + } +} diff --git a/app/api/website/model/sys_patent.go b/app/api/website/model/sys_patent.go index f8df747..e051d5a 100644 --- a/app/api/website/model/sys_patent.go +++ b/app/api/website/model/sys_patent.go @@ -10,6 +10,40 @@ type SysPatent struct { *model.SysPatent } +type SysPatentInfo struct { + model.Model + Kind model.SysParentKind `json:"kind"` + Title string `json:"title"` + Description string `json:"description"` + ApplyAt string `json:"apply_at"` +} + +// Patent 专利信息 +func (m *SysPatent) Patent(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*SysPatentInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS p"). + Select("p.id", "p.title", "LEFT(p.description, 80) AS description", "p.apply_at"). + Joins(fmt.Sprintf("LEFT JOIN %s AS c ON p.ipc_code = c.ipc AND c.is_deleted = %d", + model.NewSysPatentClassify().TableName(), model.DeleteStatusForNot)). + Where("p.shelf_status = ?", model.ShelfStatusForUp). + Where("p.is_deleted = ?", model.DeleteStatusForNot) + + if len(where) > 0 { + for _, v := range where { + db = db.Where(v.Condition, v.Value) + } + } + out := make([]*SysPatentInfo, 0) + + if err := db.Count(count).Error; err != nil { + return nil, err + } + + if err := db.Order("p.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { + return nil, err + } + return out, nil +} + // Distribution 分布信息 func (m *SysPatent) Distribution() ([]*DataAreaDistributionInfo, error) { out := make([]*DataAreaDistributionInfo, 0) diff --git a/app/common/model/common.go b/app/common/model/common.go index 983294d..75b4e17 100644 --- a/app/common/model/common.go +++ b/app/common/model/common.go @@ -83,7 +83,7 @@ type ShelfStatusKind int const ( // ShelfStatusForUp 上架 - ShelfStatusForUp ShelfStatusKind = iota + ShelfStatusForUp ShelfStatusKind = iota + 1 // ShelfStatusForDown 下架 ShelfStatusForDown ) diff --git a/app/common/model/sys_patent.go b/app/common/model/sys_patent.go index 5534bcf..f9eb1fd 100644 --- a/app/common/model/sys_patent.go +++ b/app/common/model/sys_patent.go @@ -3,7 +3,7 @@ package model // SysPatent 专利信息数据模型 type SysPatent struct { Model - Kind SysParentKind `gorm:"column:kind;type:tinyint(1);default:0;comment:专利类型" json:"kind"` + Kind SysParentKind `gorm:"column:kind;index:idx_sys_patent_kind;type:tinyint(1);default:0;comment:专利类型" json:"kind"` Title string `gorm:"column:title;type:varchar(100);default:'';comment:名称标题" json:"title"` FileUrl string `gorm:"column:file_url;type:varchar(255);default:'';comment:文件地址" json:"file_url"` ApplyCode string `gorm:"column:apply_code;type:varchar(30);default:'';comment:申请号" json:"apply_code"` @@ -15,7 +15,7 @@ type SysPatent struct { Inventor string `gorm:"column:inventor;type:varchar(255);default:'';comment:发明人" json:"inventor"` Description string `gorm:"column:description;type:text;comment:摘要" json:"description"` PrincipalClaim string `gorm:"column:principal_claim;type:text;comment:主权项" json:"principal_claim"` - IPCCode string `gorm:"column:ipc_code;type:varchar(30);default:'';comment:IPC主分类号" json:"ipc_code"` + IPCCode string `gorm:"column:ipc_code;index:idx_sys_patent_ipc_code;type:varchar(30);default:'';comment:IPC主分类号" json:"ipc_code"` Shelf Status SysParentStatus `gorm:"column:status;type:tinyint(1);default:1;comment:专利状态(1:授权,2:实审,3:公开)" json:"-"` ModelDeleted diff --git a/app/logic/ies.go b/app/logic/ies.go new file mode 100644 index 0000000..b98fed9 --- /dev/null +++ b/app/logic/ies.go @@ -0,0 +1,7 @@ +package logic + +type IES interface { + Index() string + Create() error + Search(condition map[string]interface{}) (interface{}, error) +} diff --git a/app/service/es.go b/app/service/es.go new file mode 100644 index 0000000..d6885ba --- /dev/null +++ b/app/service/es.go @@ -0,0 +1,88 @@ +package service + +import ( + "SciencesServer/serve/es" + "context" + "encoding/json" + "fmt" + "github.com/olivere/elastic/v7" +) + +type Manage struct { + ID uint64 `json:"id"` // ID + Identity int `json:"identity"` // 身份信息 + Title string `json:"title"` // 名称 + Keyword string `json:"keyword"` // 关键词 + Research string `json:"research"` // 研究方向 +} + +type ManageOption func(manage *Manage) + +func (c *Manage) Index() string { + return "es_manage_index" +} + +func (this *Manage) Create() error { + _bytes, _ := json.Marshal(this) + _, err := es.GetInstance().Index().Index(this.Index()).BodyJson(string(_bytes)).Do(context.Background()) + return err +} + +func (this *Manage) Search(condition map[string]interface{}) (interface{}, error) { + search := elastic.NewSearchSource() + + for k, v := range condition { + search.Query(elastic.NewMatchQuery(k, fmt.Sprintf("%v", v))) + } + service := es.GetInstance().Search().Index(this.Index()).SearchSource(search) + + result, err := service.Do(context.Background()) + + if err != nil { + return nil, err + } + out := make([]*Manage, 0) + + for _, hit := range result.Hits.Hits { + data := new(Manage) + + if err = json.Unmarshal(hit.Source, data); err != nil { + return nil, err + } + out = append(out, data) + } + return out, nil +} + +func WithManageID(id uint64) ManageOption { + return func(manage *Manage) { + manage.ID = id + } +} + +func WithManageTitle(title string) ManageOption { + return func(manage *Manage) { + manage.Title = title + } +} + +func WithManageKeyword(keyword string) ManageOption { + return func(manage *Manage) { + manage.Keyword = keyword + } +} + +func WithManageResearch(research string) ManageOption { + return func(manage *Manage) { + manage.Research = research + } +} + +func NewManage(options ...ManageOption) *Manage { + out := new(Manage) + + for _, option := range options { + option(out) + } + return out +} diff --git a/router/address.go b/router/address.go index 2ddfbf8..2c9fc7a 100644 --- a/router/address.go +++ b/router/address.go @@ -15,6 +15,13 @@ func registerAPI(app *gin.Engine) { g := app.Group(apiPrefix) v1 := g.Group("/v1") + esV1 := v1.Group("/es") + { + _api := new(api2.ES) + esV1.POST("/create", _api.Create) + esV1.POST("/search", _api.Search) + } + // Index 首页信息管理 indexV1 := v1.Group("/index") { @@ -59,6 +66,15 @@ func registerAPI(app *gin.Engine) { serviceV1.GET("/innovate/kind", _api.InnovateKind) serviceV1.POST("/innovate/detail", _api.InnovateDetail) } + //Technology 技术信息管理 + technologyV1 := v1.Group("/technology") + { + _api := new(api2.Technology) + technologyV1.POST("/patent", _api.Patent) + technologyV1.POST("/patent/detail", _api.PatentDetail) + technologyV1.POST("/demand", _api.Demand) + technologyV1.POST("/demand/detail", _api.DemandDetail) + } // About 关于我们管理 aboutV1 := v1.Group("/about") { diff --git a/serve/es/es.go b/serve/es/es.go index 984847c..dc7f67f 100644 --- a/serve/es/es.go +++ b/serve/es/es.go @@ -1,20 +1,22 @@ package es import ( - "github.com/elastic/go-elasticsearch/v7" + "context" + "github.com/olivere/elastic/v7" ) type Instance struct { - client *elasticsearch.Client + client *elastic.Client address []string username, password string + logger elastic.Logger } type Option func(*Instance) var ( - client *elasticsearch.Client + client *elastic.Client ) func WithEsAddress(address []string) Option { @@ -35,24 +37,29 @@ func WithEsPassword(password string) Option { } } +func WithEsLog(logger elastic.Logger) Option { + return func(instance *Instance) { + instance.logger = logger + } +} + func (this *Instance) Init() *Instance { - obj := elasticsearch.Config{ - Addresses: this.address, - Username: this.username, - Password: this.password, - } - client, err := elasticsearch.NewClient(obj) - + client, err := elastic.NewClient( + elastic.SetSniff(false), + elastic.SetURL(this.address...), + //elastic.SetInfoLog(this.logger), + ) if err != nil { - panic("Elasticsearch New Error " + err.Error()) + panic(err) } + ctx := context.Background() - if _, err = client.Ping(); err != nil { - panic("Elasticsearch Ping Error " + err.Error()) - + for _, address := range this.address { + if _, _, err = client.Ping(address).Do(ctx); err != nil { + panic(err) + } } this.client = client - return this } @@ -69,3 +76,7 @@ func NewInstance(option ...Option) *Instance { } return instance } + +func GetInstance() *elastic.Client { + return client +} diff --git a/serve/es/es_test.go b/serve/es/es_test.go index d4a3fe3..32fe6f0 100644 --- a/serve/es/es_test.go +++ b/serve/es/es_test.go @@ -2,7 +2,8 @@ package es import ( "context" - "github.com/elastic/go-elasticsearch/v7/esapi" + "encoding/json" + "fmt" "github.com/olivere/elastic/v7" "strings" "testing" @@ -14,19 +15,67 @@ var ( host = "http://192.168.0.188:9200" ) +type Student struct { + Name string `json:"name"` + Age int64 `json:"age"` + AverageScore float64 `json:"average_score"` +} + func TestNewInstance(t *testing.T) { client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) if err != nil { panic(err) } + if _, _, err = client.Ping(host).Do(context.Background()); err != nil { t.Log(err) return } - //client.CreateIndex(dataIndex).BodyJson().Do(context.Background()) - esversion, _ := client.ElasticsearchVersion(host) - t.Log(esversion) + + //newStudent := Student{ + // Name: "熊,桥,顺", + // Age: 10, + // AverageScore: 99.9, + //} + //dataJSON, _ := json.Marshal(newStudent) + // + //_, err = client.Index(). + // Index("students"). + // BodyJson(string(dataJSON)). + // Do(context.Background()) + // + //if err != nil { + // panic(err) + //} + var students []Student + + searchSource := elastic.NewSearchSource() + searchSource.Query(elastic.NewMatchQuery("name", "顺")) + + queryStr, _ := searchSource.Source() + queryJs, _ := json.Marshal(queryStr) + + fmt.Println("[esclient]Final ESQuery=\n", string(queryJs)) + + searchService := client.Search().Index("students").SearchSource(searchSource) + + searchResult, err1 := searchService.Do(context.Background()) + + if err1 != nil { + fmt.Println("[ProductsES][GetPIds]Error=", err1) + return + } + for _, hit := range searchResult.Hits.Hits { + var student Student + err := json.Unmarshal(hit.Source, &student) + + if err != nil { + fmt.Println("[Getting Students][Unmarshal] Err=", err) + } + students = append(students, student) + } + t.Log(students) } func TestNewInstance1(t *testing.T) { @@ -37,9 +86,9 @@ func TestNewInstance1(t *testing.T) { b.WriteString(`{"title" : "`) b.WriteString("我是好人") b.WriteString(`"}`) - resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String())) - t.Log(err) - t.Log(resp) + //resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String())) + //t.Log(err) + //t.Log(resp) } func TestNewInstance2(t *testing.T) { @@ -47,7 +96,7 @@ func TestNewInstance2(t *testing.T) { WithEsAddress([]string{"http://192.168.0.188:9200"}), ).Init().Local() - client.Search(func(request *esapi.SearchRequest) { - - }) + //client.Search(func(request *esapi.SearchRequest) { + // + //}) } diff --git a/serve/es/serve.go b/serve/es/serve.go index 985af23..98ef7a7 100644 --- a/serve/es/serve.go +++ b/serve/es/serve.go @@ -2,6 +2,9 @@ package es func Create() error { //_, err := client.Index(). + // Index("students"). + // BodyJson(string(dataJSON)). + // Do(context.Background()) return nil }