瀏覽代碼

Merge branch 'master' of http://192.168.3.207:10080/group3/SwordFish_Interface_Service

jiaojiao7 4 年之前
父節點
當前提交
231ae59a1d
共有 4 個文件被更改,包括 103 次插入35 次删除
  1. 5 14
      api/v1/projects.go
  2. 7 5
      db/es.go
  3. 47 16
      service/projects.go
  4. 44 0
      test/project/project_test.go

+ 5 - 14
api/v1/projects.go

@@ -16,17 +16,18 @@ func ProjectApiRegister(router *gin.Engine) {
 	{
 		routerGroup.POST("/projectList", getProjectsList)
 		routerGroup.POST("/projectDetail", getProjectDetail)
-		routerGroup.POST("/winnerProjectList", winnerProjectList)
 	}
 }
 
 //获取项目列表接口
 func getProjectsList(c *gin.Context) {
-	projectName := c.Query("name")
-	winner := c.Query("winner")
-	time := c.Query("time")
+	projectName := c.PostForm("name")
+	winner := c.PostForm("winner")
+	time := c.PostForm("time")
+	global.Logger.Info("projectName " + projectName)
 	data := &map[string]interface{}{}
 	if projectName != "" || winner != "" {
+		global.Logger.Info("666")
 		data = service.ProjectListData(projectName, winner, time)
 	}
 	c.JSON(200, data)
@@ -42,13 +43,3 @@ func getProjectDetail(c *gin.Context) {
 	}
 	c.JSON(200, data)
 }
-
-//中标信息查询项目
-func winnerProjectList(c *gin.Context) {
-	winner := c.Query("winner")
-	data := &map[string]interface{}{}
-	if winner != "" {
-		data = service.WinnerProjectListData(winner)
-	}
-	c.JSON(200, data)
-}

+ 7 - 5
db/es.go

@@ -17,20 +17,22 @@ type EsConfig struct {
 }
 
 var (
-	Es *elastic.Elastic
+	Es        *elastic.Elastic
+	EsConfigs EsConfig
 )
 
 func InitEs() {
 	//数据库属性定义在业务中,解析配置文件加载到结构体基本方法viper在spirit中
-	sfisDBConfig := EsConfig{}
-	_, err := core.Viper(global.EnvPathPrefix+"/"+global.Env+"/es.toml", &sfisDBConfig)
+
+	_, err := core.Viper(global.EnvPathPrefix+"/"+global.Env+"/es.toml", &EsConfigs)
 	if err != nil {
 		log.Println("EsConfig:", err)
 		return
 	}
+	log.Println("EsConfigs", EsConfigs)
 	Es = &elastic.Elastic{
-		S_esurl: sfisDBConfig.Project.Addr,
-		I_size:  sfisDBConfig.Project.Pool,
+		S_esurl: EsConfigs.Project.Addr,
+		I_size:  EsConfigs.Project.Pool,
 	}
 	Es.InitElasticSize()
 }

+ 47 - 16
service/projects.go

@@ -5,42 +5,73 @@ import (
 	"sfbase/core"
 	"sfbase/elastic"
 	"sfbase/global"
+	"sfis/db"
+	"strconv"
 	"strings"
 )
 
 var (
-	INDEX        = core.GetStringConf("es.project.index")
-	TYPE         = core.GetStringConf("es.project.itype")
 	pjt_count    = 100
-	pjt_field    = `"area","bidamount","bidstatus","budget","firsttime","projectname","s_winner","buyer","city"`
+	pjt_field    = `"_id","area","city","bidamount","firsttime","projectname","s_winner","buyer","buyerclass"`
 	pjt_sort     = `{"firsttime":-1}`
-	query        = `{"query": {"bool": {"should": [%s]}}}`
+	query        = `{"query": {"bool": {"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
 	query_string = `{"constant_score": {"boost": 2,"query": {"match_phrase": {"projectname.pname": {"analyzer": "my_ngram","query": "%s","slop": 6}}}}},{"multi_match": {"query": "%s","fields": ["projectname.pname"],"analyzer": "ik","minimum_should_match": "100%%"}}`
+	query_winner = `{"term": {"s_winner": "%s"}}`
+	SR           = strings.Replace
+	HL           = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
+	highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
 )
 
-func ProjectListData(projectName, winner, time string) *map[string]interface{} {
+func ProjectListData(projectName, winner, time string) (data *map[string]interface{}) {
 	global.Logger.Info("projectName " + projectName)
 	global.Logger.Info("winner " + winner)
 	global.Logger.Info("time " + time)
 	pjt_name := strings.TrimSpace(projectName)
 	pjt_len := len([]rune(pjt_name))
-	data := &map[string]interface{}{}
+	data = &map[string]interface{}{}
 	qstr := ""
-	if pjt_len >= 4 {
-		qstr = fmt.Sprintf(query, fmt.Sprintf(query_string, pjt_name, pjt_name))
-		global.Logger.Debug("qstr " + qstr)
+	if pjt_len >= 4 && winner == "" {
+		qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name))
+	} else if pjt_len >= 4 && winner != "" {
+		qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), fmt.Sprintf(query_string, pjt_name, pjt_name))
+	} else if winner != "" {
+		qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "")
 	} else {
-		return data
+		return
 	}
-	repl := elastic.GetAllByNgram(INDEX, TYPE, qstr, "", pjt_sort, pjt_field, 0, pjt_count, 0, false)
+	INDEX := core.GetStringConf("es.project.index")
+	TYPE := core.GetStringConf("es.project.itype")
+	Es := db.GetEs()
+	global.Logger.Info("INDEX " + INDEX)
+	global.Logger.Info("TYPE " + TYPE)
+	repl := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, pjt_field, 0, pjt_count, 0, false)
 	if repl != nil && len(*repl) > 0 {
 		(*data)["data"] = repl
 	}
-	return data
+	return
 }
 
-func WinnerProjectListData(winner string) *map[string]interface{} {
-	global.Logger.Info("winner " + winner)
-	data := &map[string]interface{}{}
-	return data
+func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
+	if qstr != "" {
+		if highlight {
+			ws := []string{}
+			for _, w := range strings.Split(findfields, ",") {
+				ws = append(ws, fmt.Sprintf(highlightStr, w, count))
+			}
+			qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
+		}
+		if len(fields) > 0 {
+			qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
+		}
+		if len(order) > 0 {
+			qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
+		}
+		if start > -1 {
+			qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
+		}
+		global.Logger.Info("GetAllByNgram:" + qstr)
+		return Es.Get(index, itype, qstr)
+	} else {
+		return nil
+	}
 }

+ 44 - 0
test/project/project_test.go

@@ -0,0 +1,44 @@
+package project
+
+import (
+	"encoding/json"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"strings"
+	"testing"
+)
+
+var (
+	apiurl = "http://127.0.0.1:8080/sfis/api/v1/projectList"
+)
+
+func Test_Project(t *testing.T) {
+	getData()
+}
+
+func getData() {
+	data := post(apiurl, map[string]string{
+		"name":   "河南大学",
+		"winner": "洛阳丹尼斯量贩有限公司",
+		"time":   "",
+	})
+	log.Println(data)
+}
+
+func post(url string, form map[string]string) (data map[string]interface{}) {
+	str := ""
+	for k, v := range form {
+		str += "&" + k + "=" + v
+	}
+	log.Println(str)
+	res, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
+	if err != nil {
+		log.Println("post err:", err.Error())
+	} else if res.Body != nil {
+		defer res.Body.Close()
+		bs, _ := ioutil.ReadAll(res.Body)
+		json.Unmarshal(bs, &data)
+	}
+	return
+}