Commit 2d80e9d5 by mushishixian

autospu接口

parent 82930e17
package main
import (
"flag"
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/web"
"log"
"search_server/boot"
_ "search_server/controller"
"search_server/framework/gin_"
"search_server/pkg/config"
)
func main() {
var path string
flag.StringVar(&path, "config", "../conf/config.ini", "配置文件")
flag.Parse()
if err := boot.Boot(path); err != nil {
log.Println(err)
}
r := gin.New()
gin_.BootStrap(r)
port := config.Get("web.port").String()
//web改成micro 就是grpc,并直接注册到etcd里面
service := web.NewService(
web.Name("go.micro.api.http.bom"),
web.Handler(r),
web.Address(":"+port),
)
if err := service.Init(); err != nil {
log.Println(err)
}
if err := service.Run(); err != nil {
log.Println(err)
}
}
File mode changed
......@@ -3,66 +3,35 @@ package controller
import (
"github.com/gin-gonic/gin"
"search_server/framework/gin_"
"search_server/protopb/course"
"search_server/protopb/bom"
"search_server/service"
)
func init() {
courseService := service.NewCourseServiceImpl()
gin_.NewBuilder().WithService(courseService).
bomService := service.NewBomServiceImpl()
gin_.NewBuilder().WithService(bomService).
WithMiddleware(Check_Middleware()).
WithMiddleware(Cors_Middleware()).
WithEndpoint(Courselist_Endpoint(courseService)).
WithRequest(Courselist_Request()).
WithResponse(Course_Response()).Build("/course", "GET")
gin_.NewBuilder().WithService(courseService).
WithMiddleware(Check_Middleware()).
WithEndpoint(Coursedetail_Endpoint(courseService)).
WithRequest(Coursedetail_Request()).
WithResponse(Course_Response()).Build("/detail/:course_id", "GET")
}
//详细相关
func Coursedetail_Endpoint(c *service.CourseServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &course.DetailResponse{Course: new(course.CourseModel), Counts: make([]*course.CourseCounts, 0)}
err = c.GetDetail(context, request.(*course.DetailRequest), rsp)
return rsp, err
}
}
//这个函数的作用是怎么处理请求
func Coursedetail_Request() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &course.DetailRequest{}
err := context.BindUri(bReq)
if err != nil {
return nil, err
}
err = context.BindHeader(bReq)
if err != nil {
return nil, err
}
return bReq, nil
}
WithEndpoint(AutoSpuEndpoint(bomService)).
WithRequest(AutoSpuRequest()).
WithResponse(AutoSpuResponse()).Build("/autospu", "GET")
}
//获取列表相关
func Courselist_Endpoint(c *service.CourseServiceImpl) gin_.Endpoint {
func AutoSpuEndpoint(c *service.BomServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &course.ListResponse{}
err = c.ListForTop(context, request.(*course.ListRequest), rsp)
rsp := &bom.AutoSpuResponse{}
err = c.AutoSpu(context, request.(*bom.AutoSpuRequest), rsp)
return rsp, err
}
}
//这个函数的作用是怎么处理请求
func Courselist_Request() gin_.EncodeRequestFunc {
func AutoSpuRequest() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &course.ListRequest{}
bReq := &bom.AutoSpuRequest{}
err := context.BindQuery(bReq) //使用的是query 参数
bReq.GoodsName = context.Query("goods_name")
if err != nil {
return nil, err
}
......@@ -71,7 +40,7 @@ func Courselist_Request() gin_.EncodeRequestFunc {
}
//这个函数作用是:怎么处理响应结果
func Course_Response() gin_.DecodeResponseFunc {
func AutoSpuResponse() gin_.DecodeResponseFunc {
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
......
......@@ -16,6 +16,7 @@ require (
github.com/prometheus/common v0.9.1
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.5.1 // indirect
github.com/tidwall/gjson v1.6.0
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece // indirect
......
......@@ -2,6 +2,7 @@ package es
import (
"github.com/imroc/req"
"math/rand"
"search_server/pkg/config"
)
......@@ -19,9 +20,11 @@ type HitsResult struct {
}
func CurlES(index string, param string) (result string, err error) {
goodsServerUrl := config.Get("es.url").String()
endpoints := config.Get("es.urls").Strings(",")
//随机获取一个节点进行请求
esUrl := endpoints[rand.Intn(len(endpoints))]
params := req.BodyJSON(param)
resp, err := req.Get(goodsServerUrl+"/"+index+"/_search", params)
resp, err := req.Get(esUrl+"/"+index+"/_search", params)
if err != nil {
return
}
......
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: bom.proto
package bom
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type GoodsNameModel struct {
GoodsName string `protobuf:"bytes,1,opt,name=goods_name,json=goodsName,proto3" json:"goods_name,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *GoodsNameModel) Reset() { *m = GoodsNameModel{} }
func (m *GoodsNameModel) String() string { return proto.CompactTextString(m) }
func (*GoodsNameModel) ProtoMessage() {}
func (*GoodsNameModel) Descriptor() ([]byte, []int) {
return fileDescriptor_f689add15dae9986, []int{0}
}
func (m *GoodsNameModel) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_GoodsNameModel.Unmarshal(m, b)
}
func (m *GoodsNameModel) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_GoodsNameModel.Marshal(b, m, deterministic)
}
func (m *GoodsNameModel) XXX_Merge(src proto.Message) {
xxx_messageInfo_GoodsNameModel.Merge(m, src)
}
func (m *GoodsNameModel) XXX_Size() int {
return xxx_messageInfo_GoodsNameModel.Size(m)
}
func (m *GoodsNameModel) XXX_DiscardUnknown() {
xxx_messageInfo_GoodsNameModel.DiscardUnknown(m)
}
var xxx_messageInfo_GoodsNameModel proto.InternalMessageInfo
func (m *GoodsNameModel) GetGoodsName() string {
if m != nil {
return m.GoodsName
}
return ""
}
type AutoSpuRequest struct {
// @inject_tag: uri:"goods_name"
GoodsName string `protobuf:"bytes,1,opt,name=goods_name,json=goodsName,proto3" json:"goods_name,omitempty" uri:"goods_name"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *AutoSpuRequest) Reset() { *m = AutoSpuRequest{} }
func (m *AutoSpuRequest) String() string { return proto.CompactTextString(m) }
func (*AutoSpuRequest) ProtoMessage() {}
func (*AutoSpuRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_f689add15dae9986, []int{1}
}
func (m *AutoSpuRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_AutoSpuRequest.Unmarshal(m, b)
}
func (m *AutoSpuRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_AutoSpuRequest.Marshal(b, m, deterministic)
}
func (m *AutoSpuRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_AutoSpuRequest.Merge(m, src)
}
func (m *AutoSpuRequest) XXX_Size() int {
return xxx_messageInfo_AutoSpuRequest.Size(m)
}
func (m *AutoSpuRequest) XXX_DiscardUnknown() {
xxx_messageInfo_AutoSpuRequest.DiscardUnknown(m)
}
var xxx_messageInfo_AutoSpuRequest proto.InternalMessageInfo
func (m *AutoSpuRequest) GetGoodsName() string {
if m != nil {
return m.GoodsName
}
return ""
}
type AutoSpuResponse struct {
// @inject_tag: json:"error_code"
ErrorCode int32 `protobuf:"varint,1,opt,name=error_code,json=errorCode,proto3" json:"error_code"`
// @inject_tag: json:"error_message"
ErrorMessage string `protobuf:"bytes,2,opt,name=error_message,json=errorMessage,proto3" json:"error_message"`
Data []string `protobuf:"bytes,3,rep,name=data,proto3" json:"data,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *AutoSpuResponse) Reset() { *m = AutoSpuResponse{} }
func (m *AutoSpuResponse) String() string { return proto.CompactTextString(m) }
func (*AutoSpuResponse) ProtoMessage() {}
func (*AutoSpuResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_f689add15dae9986, []int{2}
}
func (m *AutoSpuResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_AutoSpuResponse.Unmarshal(m, b)
}
func (m *AutoSpuResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_AutoSpuResponse.Marshal(b, m, deterministic)
}
func (m *AutoSpuResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_AutoSpuResponse.Merge(m, src)
}
func (m *AutoSpuResponse) XXX_Size() int {
return xxx_messageInfo_AutoSpuResponse.Size(m)
}
func (m *AutoSpuResponse) XXX_DiscardUnknown() {
xxx_messageInfo_AutoSpuResponse.DiscardUnknown(m)
}
var xxx_messageInfo_AutoSpuResponse proto.InternalMessageInfo
func (m *AutoSpuResponse) GetErrorCode() int32 {
if m != nil {
return m.ErrorCode
}
return 0
}
func (m *AutoSpuResponse) GetErrorMessage() string {
if m != nil {
return m.ErrorMessage
}
return ""
}
func (m *AutoSpuResponse) GetData() []string {
if m != nil {
return m.Data
}
return nil
}
func init() {
proto.RegisterType((*GoodsNameModel)(nil), "bom.GoodsNameModel")
proto.RegisterType((*AutoSpuRequest)(nil), "bom.AutoSpuRequest")
proto.RegisterType((*AutoSpuResponse)(nil), "bom.AutoSpuResponse")
}
func init() { proto.RegisterFile("bom.proto", fileDescriptor_f689add15dae9986) }
var fileDescriptor_f689add15dae9986 = []byte{
// 211 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x90, 0xcd, 0x6a, 0x84, 0x30,
0x10, 0xc7, 0xb1, 0xf6, 0x83, 0x0c, 0xfd, 0x80, 0xb4, 0x07, 0x29, 0x14, 0xa4, 0xbd, 0x78, 0xb2,
0xd0, 0xf6, 0x05, 0x6a, 0x0f, 0x3d, 0xb9, 0x87, 0xf8, 0x00, 0x12, 0xcd, 0x20, 0xc2, 0xc6, 0x71,
0x93, 0xb8, 0xcf, 0xbf, 0x24, 0xd9, 0x5d, 0xf0, 0xb4, 0xb7, 0xf0, 0xcb, 0xfc, 0x66, 0xfe, 0x33,
0xc0, 0x3a, 0xd2, 0xe5, 0x6c, 0xc8, 0x11, 0x4f, 0x3b, 0xd2, 0xef, 0x9f, 0xf0, 0xf8, 0x4f, 0xa4,
0xec, 0x46, 0x6a, 0xac, 0x49, 0xe1, 0x96, 0xbf, 0x01, 0x0c, 0x9e, 0xb4, 0x93, 0xd4, 0x98, 0x25,
0x79, 0x52, 0x30, 0xc1, 0x86, 0x53, 0x8d, 0x17, 0x7e, 0x17, 0x47, 0xcd, 0xbc, 0x08, 0xdc, 0x2d,
0x68, 0xdd, 0x25, 0x61, 0x84, 0xa7, 0xb3, 0x60, 0x67, 0x9a, 0x2c, 0x7a, 0x03, 0x8d, 0x21, 0xd3,
0xf6, 0xa4, 0xa2, 0x71, 0x23, 0x58, 0x20, 0x7f, 0xa4, 0x90, 0x7f, 0xc0, 0x43, 0xfc, 0xd6, 0x68,
0xad, 0x1c, 0x30, 0xbb, 0x0a, 0x3d, 0xef, 0x03, 0xac, 0x23, 0xe3, 0x1c, 0xae, 0x95, 0x74, 0x32,
0x4b, 0xf3, 0xb4, 0x60, 0x22, 0xbc, 0xbf, 0x2a, 0x80, 0x8a, 0x74, 0x83, 0x66, 0x3f, 0xf6, 0xc8,
0x7f, 0xe0, 0xee, 0x38, 0x98, 0x3f, 0x97, 0x7e, 0xed, 0x75, 0xee, 0xd7, 0x97, 0x35, 0x8c, 0xd9,
0xba, 0xdb, 0x70, 0x9c, 0xef, 0x43, 0x00, 0x00, 0x00, 0xff, 0xff, 0x31, 0x7b, 0x77, 0x8f, 0x29,
0x01, 0x00, 0x00,
}
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: bom.proto
package bom
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/go-micro/v2/api"
client "github.com/micro/go-micro/v2/client"
server "github.com/micro/go-micro/v2/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for BomService service
func NewBomServiceEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for BomService service
type BomService interface {
AutoSpu(ctx context.Context, in *AutoSpuRequest, opts ...client.CallOption) (*AutoSpuResponse, error)
}
type bomService struct {
c client.Client
name string
}
func NewBomService(name string, c client.Client) BomService {
return &bomService{
c: c,
name: name,
}
}
func (c *bomService) AutoSpu(ctx context.Context, in *AutoSpuRequest, opts ...client.CallOption) (*AutoSpuResponse, error) {
req := c.c.NewRequest(c.name, "BomService.AutoSpu", in)
out := new(AutoSpuResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for BomService service
type BomServiceHandler interface {
AutoSpu(context.Context, *AutoSpuRequest, *AutoSpuResponse) error
}
func RegisterBomServiceHandler(s server.Server, hdlr BomServiceHandler, opts ...server.HandlerOption) error {
type bomService interface {
AutoSpu(ctx context.Context, in *AutoSpuRequest, out *AutoSpuResponse) error
}
type BomService struct {
bomService
}
h := &bomServiceHandler{hdlr}
return s.Handle(s.NewHandler(&BomService{h}, opts...))
}
type bomServiceHandler struct {
BomServiceHandler
}
func (h *bomServiceHandler) AutoSpu(ctx context.Context, in *AutoSpuRequest, out *AutoSpuResponse) error {
return h.BomServiceHandler.AutoSpu(ctx, in, out)
}
......@@ -2,45 +2,22 @@ syntax = "proto3";
package bom;
service BomService {
rpc ListForTop(ListRequest) returns (ListResponse);
rpc GetDetail(DetailRequest) returns(DetailResponse);
}
message CourseModel{
int32 course_id=1;
string course_name=2;
string course_disp_name=3;
string course_intr=4;
float course_price=5;
float course_price2=6;
// @inject_tag: gorm:"type:timestamp"
int64 addtime=7;
rpc AutoSpu(AutoSpuRequest) returns (AutoSpuResponse);
}
message ListRequest {
// @inject_tag: form:"size"
int32 size = 1;
}
message DetailRequest{
// @inject_tag: uri:"course_id"
int32 course_id=1;
// @inject_tag: header:"fetch_type"
int32 fetch_type=2;
}
//计数
message CourseCounts{
// @inject_tag: json:"-"
int32 count_id=1;
// @inject_tag: json:"-"
int32 course_id=2;
string count_key=3;
int32 count_value=4;
message GoodsNameModel{
string goods_name = 1;
}
message ListResponse {
repeated CourseModel result = 1;
message AutoSpuRequest {
// @inject_tag: uri:"goods_name"
string goods_name = 1;
}
message DetailResponse{
CourseModel course=1;
repeated CourseCounts counts=2;
message AutoSpuResponse{
// @inject_tag: json:"error_code"
int32 error_code = 1;
// @inject_tag: json:"error_message"
string error_message = 2;
repeated string data = 3;
}
......@@ -2,11 +2,13 @@ package service
import (
"context"
"fmt"
"github.com/prometheus/common/log"
"github.com/tidwall/gjson"
elastic "gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/dao"
"search_server/pkg/es"
"search_server/protopb/bom"
. "search_server/protopb/course"
"strings"
)
......@@ -17,15 +19,12 @@ import (
type BomServiceImpl struct{}
func (bs *BomServiceImpl) ListForTop(ctx context.Context, req *ListRequest, rsp *ListResponse) error {
course := make([]*CourseModel, 0)
err := dao.GetCourseListBySql(req.Size).Find(&course).Error
if err != nil {
return err
}
rsp.Result = course
func (bs *BomServiceImpl) AutoSpu(ctx context.Context, req *bom.AutoSpuRequest, rsp *bom.AutoSpuResponse) error {
goodsNameSlice := AutoSpu(req.GoodsName)
rsp.Data = goodsNameSlice
return nil
}
func (bs *BomServiceImpl) GetDetail(ctx context.Context, req *DetailRequest, rsp *DetailResponse) error {
//只取课程详细
if req.FetchType == 0 || req.FetchType == 1 || req.FetchType == 3 {
......@@ -48,7 +47,7 @@ func NewBomServiceImpl() *BomServiceImpl {
}
//构建请求参数
func AutoSpu(goodsName string) (searchRequestStr string) {
func AutoSpu(goodsName string) (goodsNameList []string) {
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName = replace.ReplaceAllString(goodsName, "")
......@@ -57,12 +56,18 @@ func AutoSpu(goodsName string) (searchRequestStr string) {
query := getTermQuery(goodsName)
source := elastic.NewSearchSource().Query(query)
source = source.From(0).Size(10)
searchRequestStr, _ = elastic.NewSearchRequest().Source(source).Body()
fmt.Println(searchRequestStr)
searchRequestStr, _ := elastic.NewSearchRequest().Source(source).Body()
result, err := es.CurlES("lie_spu", searchRequestStr)
fmt.Println(err)
fmt.Println(result)
return searchRequestStr
//只要其中的goods_name
if err != nil {
log.Error(err)
}
goodsList := gjson.Get(result, "hits.hits.#._source").Array()
for _, goods := range goodsList {
goodsName := gjson.Get(goods.String(), "auto_goods_name").String()
goodsNameList = append(goodsNameList, goodsName)
}
return goodsNameList
}
//构建term条件
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment