|
@@ -0,0 +1,252 @@
|
|
|
+package bidsearch
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "jfw/public"
|
|
|
+ "qfw/util"
|
|
|
+ "qfw/util/elastic"
|
|
|
+ "qfw/util/jy"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+//pc、微信、app 招标信息搜索
|
|
|
+
|
|
|
+const (
|
|
|
+ INDEX = "bidding"
|
|
|
+ TYPE = "bidding"
|
|
|
+ bidSearch_sort = `{"publishtime":-1}`
|
|
|
+
|
|
|
+ //招标搜索分页--每页显示数量
|
|
|
+ SearchPageSize_APP = 50
|
|
|
+ SearchPageSize_WX = 50
|
|
|
+ SearchPageSize_PC = 50
|
|
|
+
|
|
|
+ //招标搜索分页--最大页数
|
|
|
+ SearchMaxPageNum_APP = 20
|
|
|
+ SearchMaxPageNum_WX = 20
|
|
|
+ SearchMaxPageNum_PC = 10
|
|
|
+)
|
|
|
+
|
|
|
+//GetWxsearchlistData 移动端招标信息搜索
|
|
|
+func GetWxsearchlistData(keywords, scope, publishtime, subtype, industry, minprice, maxprice, winner, buyerclass, hasBuyerTel, hasWinnerTel string, pageNum, pageSize int, selectType, field string) (list *[]map[string]interface{}, b_word, a_word, s_word string) {
|
|
|
+ b_word, a_word, s_word = jy.InterceptSearchKW(keywords, selectType == "all", len(industry) == 0)
|
|
|
+ if len(b_word) == 0 {
|
|
|
+ return list, b_word, a_word, s_word
|
|
|
+ }
|
|
|
+ findfields := `"title"`
|
|
|
+ if selectType == "all" {
|
|
|
+ findfields = `"title","detail"`
|
|
|
+ }
|
|
|
+ qstr := GetSearchQuery(s_word, industry, minprice, maxprice, hasBuyerTel, hasWinnerTel, findfields, GetBidSearchQuery(scope, publishtime, subtype, winner, buyerclass))
|
|
|
+ if selectType == "all" { //全文搜索
|
|
|
+ list = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, field, (pageNum-1)*pageSize, pageSize, 100, true)
|
|
|
+ } else { //标题搜索
|
|
|
+ list = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, field, (pageNum-1)*pageSize, pageSize, 100, false)
|
|
|
+ }
|
|
|
+
|
|
|
+ if list != nil {
|
|
|
+ public.BidListConvert(industry, list)
|
|
|
+ for _, v := range *list {
|
|
|
+ v["_id"] = util.EncodeArticleId2ByCheck(util.ObjToString(v["_id"]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list, b_word, a_word, s_word
|
|
|
+}
|
|
|
+
|
|
|
+//GetPcBidSearchData pc端招标信息搜索
|
|
|
+func GetPcBidSearchData(searchvalue, area, publishtime, subtype, industry, minprice, maxprice, winner, buyerclass, hasBuyerTel, hasWinnerTel string, start, pageSize int, isGetCount bool, selectType, field string) (count, totalPage int64, list *[]map[string]interface{}) {
|
|
|
+ //selectType:全文搜索(all)、标题搜索(title)
|
|
|
+ findfields := `"title"`
|
|
|
+ if selectType == "all" {
|
|
|
+ findfields = `"title","detail"`
|
|
|
+ }
|
|
|
+ qstr := GetSearchQuery(searchvalue, industry, minprice, maxprice, hasBuyerTel, hasWinnerTel, findfields, GetBidSearchQuery(area, publishtime, subtype, winner, buyerclass))
|
|
|
+ if isGetCount && qstr != "" && start == 0 {
|
|
|
+ count = elastic.Count(INDEX, TYPE, qstr)
|
|
|
+ }
|
|
|
+ if !isGetCount || count > 0 || start > 0 {
|
|
|
+ var repl *[]map[string]interface{}
|
|
|
+ if selectType == "all" {
|
|
|
+ //全文搜索
|
|
|
+ repl = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, field, start, pageSize, 115, true)
|
|
|
+ } else {
|
|
|
+ //标题搜索
|
|
|
+ repl = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, field, start, pageSize, 0, false)
|
|
|
+
|
|
|
+ }
|
|
|
+ if repl != nil && *repl != nil && len(*repl) > 0 {
|
|
|
+ public.BidListConvert(industry, repl)
|
|
|
+ list = repl
|
|
|
+ }
|
|
|
+ }
|
|
|
+ limitCount := int64(SearchPageSize_PC * SearchMaxPageNum_PC)
|
|
|
+ if count > limitCount {
|
|
|
+ count = limitCount
|
|
|
+ }
|
|
|
+ totalPage = (count + int64(SearchPageSize_PC) - 1) / int64(SearchPageSize_PC)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func GetBidSearchQuery(area, publishtime, subtype, winner, buyerclass string) string {
|
|
|
+ query := ``
|
|
|
+ if area != "" {
|
|
|
+ query += `{"terms":{"area":[`
|
|
|
+ for k, v := range strings.Split(area, ",") {
|
|
|
+ if k > 0 {
|
|
|
+ query += `,`
|
|
|
+ }
|
|
|
+ query += `"` + v + `"`
|
|
|
+ }
|
|
|
+ query += `]}}`
|
|
|
+ }
|
|
|
+ if publishtime != "" {
|
|
|
+ if len(query) > 0 {
|
|
|
+ query += ","
|
|
|
+ }
|
|
|
+ starttime, endtime := "", ""
|
|
|
+ now := time.Now()
|
|
|
+ if publishtime == "lately-7" { //最近7天
|
|
|
+ starttime = fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local).Unix())
|
|
|
+ } else if publishtime == "lately-30" { //最近30天
|
|
|
+ starttime = fmt.Sprint(time.Date(now.Year(), now.Month(), now.Day()-30, 0, 0, 0, 0, time.Local).Unix())
|
|
|
+ } else if publishtime == "thisyear" { //去年
|
|
|
+ starttime = fmt.Sprint(time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, time.Local).Unix())
|
|
|
+ endtime = fmt.Sprint(time.Date(now.Year()-1, 12, 31, 23, 59, 59, 0, time.Local).Unix())
|
|
|
+ } else {
|
|
|
+ starttime = strings.Split(publishtime, "_")[0]
|
|
|
+ endtime = strings.Split(publishtime, "_")[1]
|
|
|
+ etTime := time.Now()
|
|
|
+ if endtime != "" {
|
|
|
+ et, _ := strconv.ParseInt(endtime, 0, 64)
|
|
|
+ etTime = time.Unix(et, 0)
|
|
|
+ }
|
|
|
+ endtime = fmt.Sprint(time.Date(etTime.Year(), etTime.Month(), etTime.Day()+1, 0, 0, 0, 0, time.Local).Unix())
|
|
|
+ }
|
|
|
+ query += `{"range":{"publishtime":{`
|
|
|
+ if starttime != "" {
|
|
|
+ query += `"gte":` + starttime
|
|
|
+ }
|
|
|
+ if starttime != "" && endtime != "" {
|
|
|
+ query += `,`
|
|
|
+ }
|
|
|
+ if endtime != "" {
|
|
|
+ query += `"lt":` + endtime
|
|
|
+ }
|
|
|
+ query += `}}}`
|
|
|
+ }
|
|
|
+ if subtype != "" {
|
|
|
+ if len(query) > 0 {
|
|
|
+ query += ","
|
|
|
+ }
|
|
|
+ query += `{"terms":{"subtype":[`
|
|
|
+ for k, v := range strings.Split(subtype, ",") {
|
|
|
+ if k > 0 {
|
|
|
+ query += `,`
|
|
|
+ }
|
|
|
+ query += `"` + v + `"`
|
|
|
+ }
|
|
|
+ query += `]}}`
|
|
|
+ }
|
|
|
+ if winner != "" {
|
|
|
+ if len(query) > 0 {
|
|
|
+ query += ","
|
|
|
+ }
|
|
|
+ query += `{"terms":{"s_winner":[`
|
|
|
+ for k, v := range strings.Split(winner, ",") {
|
|
|
+ if k > 0 {
|
|
|
+ query += `,`
|
|
|
+ }
|
|
|
+ query += `"` + v + `"`
|
|
|
+ }
|
|
|
+ query += `]}}`
|
|
|
+ }
|
|
|
+ if buyerclass != "" {
|
|
|
+ if len(query) > 0 {
|
|
|
+ query += ","
|
|
|
+ }
|
|
|
+ query += `{"terms":{"buyerclass":[`
|
|
|
+ for k, v := range strings.Split(buyerclass, ",") {
|
|
|
+ if k > 0 {
|
|
|
+ query += `,`
|
|
|
+ }
|
|
|
+ query += `"` + v + `"`
|
|
|
+ }
|
|
|
+ query += `]}}`
|
|
|
+ }
|
|
|
+ return query
|
|
|
+}
|
|
|
+
|
|
|
+func GetSearchQuery(keyword, industry, minprice, maxprice, hasBuyerTel, hasWinnerTel, findfields, mustquery string) (qstr string) {
|
|
|
+ multi_match := `{"multi_match": {"query": "%s","type": "phrase", "fields": [%s]}}`
|
|
|
+ query := `{"query":{"bool":{"must":[%s],"must_not":[%s]}}}`
|
|
|
+ query_bool_should := `{"bool":{"should":[%s],"minimum_should_match": 1}}`
|
|
|
+ query_bools_must := `{"bool":{"must":[{"range":{"bidamount":{%s}}}]}},{"bool":{"must":[{"range":{"budget":{%s}}}],"must_not":[{"range":{"bidamount":{"gte":-1}}}]}}`
|
|
|
+ query_bool_must := `{"bool":{"must":[{"terms":{"s_subscopeclass":[%s]}}]}}`
|
|
|
+ query_missing := `{"constant_score":{"filter":{"missing":{"field":"%s"}}}}`
|
|
|
+ gte := `"gte": %s`
|
|
|
+ lte := `"lte": %s`
|
|
|
+ musts, must_not := []string{}, []string{}
|
|
|
+ if mustquery != "" {
|
|
|
+ musts = append(musts, mustquery)
|
|
|
+ }
|
|
|
+ if keyword != "" {
|
|
|
+ multi_match = fmt.Sprintf(multi_match, "%s", findfields)
|
|
|
+ shoulds := []string{}
|
|
|
+ for _, v := range strings.Split(keyword, "+") {
|
|
|
+ shoulds = append(shoulds, fmt.Sprintf(multi_match, elastic.ReplaceYH(v)))
|
|
|
+ }
|
|
|
+ musts = append(musts, fmt.Sprintf(elastic.NgramMust, strings.Join(shoulds, ",")))
|
|
|
+ }
|
|
|
+ if industry != "" {
|
|
|
+ industrys := strings.Split(industry, ",")
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_must, `"`+strings.Join(industrys, `","`)+`"`))
|
|
|
+ }
|
|
|
+ if minprice != "" || maxprice != "" {
|
|
|
+ sq := ``
|
|
|
+ if minprice != "" {
|
|
|
+ min, _ := strconv.ParseFloat(minprice, 64)
|
|
|
+ minprice = fmt.Sprintf("%.0f", min*10000)
|
|
|
+ if minprice == "0" {
|
|
|
+ minprice = ""
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if maxprice != "" {
|
|
|
+ max, _ := strconv.ParseFloat(maxprice, 64)
|
|
|
+ maxprice = fmt.Sprintf("%.0f", max*10000)
|
|
|
+ if maxprice == "0" {
|
|
|
+ maxprice = ""
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if minprice != "" {
|
|
|
+ sq += fmt.Sprintf(gte, minprice)
|
|
|
+ }
|
|
|
+ if minprice != "" && maxprice != "" {
|
|
|
+ sq += `,`
|
|
|
+ }
|
|
|
+ if maxprice != "" {
|
|
|
+ sq += fmt.Sprintf(lte, maxprice)
|
|
|
+ }
|
|
|
+ if minprice != "" || maxprice != "" {
|
|
|
+ query_price := fmt.Sprintf(query_bool_should, fmt.Sprintf(query_bools_must, sq, sq))
|
|
|
+ musts = append(musts, query_price)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if hasBuyerTel != "" {
|
|
|
+ if hasBuyerTel == "y" {
|
|
|
+ must_not = append(must_not, fmt.Sprintf(query_missing, "buyertel"))
|
|
|
+ } else {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_missing, "buyertel"))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if hasWinnerTel != "" {
|
|
|
+ if hasWinnerTel == "y" {
|
|
|
+ must_not = append(must_not, fmt.Sprintf(query_missing, "winnertel"))
|
|
|
+ } else {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_missing, "winnertel"))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ qstr = fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(must_not, ","))
|
|
|
+ return
|
|
|
+}
|