package util import ( "context" "encoding/json" "errors" "fmt" "log" "models" "mongodb" "qfw/util" // "qfw/util/elastic" esv "es" sql "sqlmodel" "strings" "sync" "time" es "github.com/olivere/elastic" "gopkg.in/mgo.v2/bson" ) const ( TitleMatchType = "1" //标题匹配 DetailMatchType = "2" //全文匹配 PurchasingMatchType = "3" //标的物匹配 AttachmentsMatchType = "4" //附件匹配 ProjectnameMatchType = "5" //项目名称匹配 BuyerMatchType = "6" //采购单位匹配 SWinnerMatchType = "7" //中标单位匹配 BuyerMatchType1 = "8" //采购单位(正则)匹配 SWinnerMatchType1 = "9" //中标单位(正则)匹配 TitleMatchTypeStr = "title" //标题匹配 DetailMatchTypeStr = "detail" //全文匹配 PurchasingMatchTypeStr = "purchasing" //标的物匹配 AttachmentsMatchTypeStr = "filetext" //附件匹配 ProjectnameMatchTypeStr = "projectname.pname" //项目名称匹配 BuyerMatchTypeStr = "buyer" //项目名称匹配 BuyerRegexpMatchTypeStr = "buyer.mbuyer" //采购单位正则匹配 SWinnerMatchTypeStr = "s_winner" //项目名称匹配 SWinnerRegexpMatchTypeStr = "s_winner.mwinner" //中标单位正则匹配 TitleMatchTypeField = "title" //标题匹配 DetailMatchTypeField = "detail" //全文匹配 PurchasingMatchTypeField = "purchasing" //标的物匹配 AttachmentsMatchTypeField = "filetext" //附件匹配 ProjectnameMatchTypeField = "projectname.pname" //项目名称匹配 BuyerMatchTypeField = "buyer" //项目名称匹配 SWinnerMatchTypeField = "s_winner" //项目名称匹配 MultiMatchType = "phrase" Standard = 1 //1:标准字段包, Advanced = 2 //2:高级字段包 Standardstr = "standard" //1:标准字段包 Advancedstr = "advanced" //2:高级字段包 Url = "https://www.jianyu360.com/article/content/%s.html" ) func UtilEsSaveData(sdataid string, datas *[]map[string]interface{}) error { defer util.Catch() if !Mgo.Del("tagsdata", bson.M{"s_dataid": sdataid}) { return errors.New("mongo del err") } for _, v := range *datas { Mgo.Save("tagsdata", v) } // if Mgo.SaveBulk("tagsdata", *datas...) { // return nil // } else { // return errors.New("mongo save err") // } return nil } type MySource struct { Querys string } func (m *MySource) Source() (interface{}, error) { mp := make(map[string]interface{}) json.Unmarshal([]byte(m.Querys), &mp) return mp["query"], nil } // 客户规则 func UtilEsFind1(tags map[string]interface{}) (error, int64) { defer util.Catch() ch := make(chan bool, 20) wg := &sync.WaitGroup{} arrsync := &sync.RWMutex{} sdataid := util.ObjToString(tags["s_dataid"]) esquery := util.ObjToString(tags["s_esquery"]) if IsNewSql != 0 { esquery = util.ObjToString(tags["s_esquery_search"]) } clearKey := util.ObjToString(tags["s_globalclearkey"]) clearKeyMatch := util.ObjToString(tags["s_globalclearkeymatch"]) if len(esquery) < 1 || len(sdataid) < 1 { return errors.New("s_esquery or s_dataid no found"), 0 } i_maxnum := util.Int64All(tags["i_maxnum"]) if i_maxnum <= 0 { i_maxnum = 100 } // maths := make([]map[string]string, 0) if orules, ok := tags["o_rules"].([]interface{}); ok { for _, v := range orules { orule, _ := v.(map[string]interface{}) maths = append(maths, map[string]string{ "s_matchkey": util.ObjToString(orule["s_matchkey"]), "s_keymatch": util.ObjToString(orule["s_keymatch"]), }) } } else if orules, ok := tags["o_rules"].([]map[string]interface{}); ok { for _, v := range orules { maths = append(maths, map[string]string{ "s_matchkey": util.ObjToString(v["s_matchkey"]), "s_keymatch": util.ObjToString(v["s_keymatch"]), }) } } else { return errors.New("o_rules no found"), 0 } ctx, _ := context.WithTimeout(context.Background(), 5*time.Minute) esCon := esv.VarEs.(*esv.EsV7) client := esCon.GetEsConn() defer esCon.DestoryEsConn(client) // esquery = esquery[:len(esquery)-1] + `,"size":` + fmt.Sprintf("%d", i_maxnum) + `}` cc := &MySource{ Querys: esquery, } searchResult, err := client.Search(EsIndex).Query(cc).Size(int(i_maxnum)).Do(ctx) if err == nil && searchResult.Hits != nil { datas := make([]map[string]interface{}, 0) util.Debug("es查询到的数量:", searchResult.Hits.TotalHits.Value) util.Debug("es查询到的数量(实际):", len(searchResult.Hits.Hits)) for _, v := range searchResult.Hits.Hits { wg.Add(1) ch <- true go func(tmpHit *es.SearchHit) { defer func() { <-ch arrsync.Unlock() wg.Done() }() arrsync.Lock() item := make(map[string]interface{}) if json.Unmarshal(tmpHit.Source, &item) == nil { item["appid"] = tags["appid"] item["rulename"] = tags["s_name"] info_id := util.ObjToString(item["id"]) item["info_id"] = info_id item["s_dataid"] = sdataid // item["s_jyhref"] = fmt.Sprintf(Url, util.CommonEncodeArticle("content", info_id)) // subType := util.ObjToString(item["subtype"]) subTypeStr := "拟建,采购意向,预告,预审,预审结果,论证意见,需求公示" if strings.Contains(subTypeStr, subType) { item["s_jyhref"] = `https://www.jianyu360.cn/article/bdprivate/` + util.CommonEncodeArticle("bdprivate", info_id) + `.html` } else { // item["s_jyhref"] = `https://www.jianyu360.cn/article/content/` + qu.CommonEncodeArticle("content", info_id) + `.html` item["s_jyhref"] = `https://www.jianyu360.cn/article/entservice/` + util.SE3.EncodeString(info_id) + `.html?appid` } // item["i_createtime"] = time.Now().Unix() delete(item, "_id") //清理词清空 for _, v := range strings.Split(clearKey, ",") { ClearKeys(clearKeyMatch, v, item) } //二次匹配关键词 matchKeys := GetMactchKey(maths, item) if len(maths) > 0 { if matchKeys != "" { item["s_matchkey"] = matchKeys } } findwinner := strings.TrimSpace(util.ObjToString(item["winner"])) if findwinner != "" { finddata := MgoEnps.FindOne(EnpsColl, bson.M{"company_name": findwinner}) if finddata != nil { if legal_person := util.ObjToString(finddata["legal_person"]); legal_person != "" { item["legal_person"] = legal_person } if email := util.ObjToString(finddata["company_email"]); email != "" { item["company_email"] = email } if phone := util.ObjToString(finddata["company_phone"]); phone != "" { item["company_phone"] = phone } item["qyk"] = finddata if company_area := util.ObjToString(finddata["company_area"]); company_area != "" { item["company_area"] = company_area } if company_city := util.ObjToString(finddata["company_city"]); company_city != "" { item["company_city"] = company_city } if company_district := util.ObjToString(finddata["company_district"]); company_district != "" { item["company_district"] = company_district } //中标单位的社会征信代码 if credit_no := util.ObjToString(finddata["credit_no"]); credit_no != "" { item["winner_credit_no"] = credit_no } } } agency := strings.TrimSpace(util.ObjToString(item["agency"])) if agency != "" { finddata := MgoEnps.FindOne(EnpsColl, bson.M{"company_name": agency}) if finddata != nil { //社会征信代码 if credit_no := util.ObjToString(finddata["credit_no"]); credit_no != "" { item["agency_credit_no"] = credit_no } } } buyer := strings.TrimSpace(util.ObjToString(item["buyer"])) if buyer != "" { finddata := MgoBuyer.FindOne(BuyerEntColl, bson.M{"company_name": buyer}) if finddata != nil { //社会征信代码 if credit_no := util.ObjToString(finddata["credit_no"]); credit_no != "" { item["buyer_credit_no"] = credit_no } } } //匹配公告附件 info := MgoBidding.FindOne("bidding", bson.M{"_id": mongodb.StringTOBsonId(info_id)}) if info != nil { log.Println("id", item["info_id"]) if info["projectinfo"] != nil { projectInfo := util.ObjToMap(info["projectinfo"]) if projectInfo != nil && len(*projectInfo) > 0 { if (*projectInfo)["attachments"] != nil { filesArr := []map[string]interface{}{} fileUrl := util.ObjToString(Sysconfig["file_downloadurl"]) if attachments, oks := (*projectInfo)["attachments"].(map[string]interface{}); oks { for _, v := range attachments { if atta, ok := v.(map[string]interface{}); ok { if atta["fid"] != nil && util.ObjToString(atta["fid"]) != "" { fileMap := map[string]interface{}{ "filename": atta["filename"], "url": fileUrl + util.ObjToString(atta["fid"]), } filesArr = append(filesArr, fileMap) } else { if atta["org_url"] != nil { fileMap := map[string]interface{}{ "filename": atta["filename"], "url": util.ObjToString(atta["org_url"]), } filesArr = append(filesArr, fileMap) } } } } } log.Println("查询附件结果:", info_id, "附件数量:", len(filesArr)) if len(filesArr) > 0 { item["filehref"] = filesArr } } } } } else { log.Println("id", item["info_id"], "bidding为空") } //项目id projectId := GetProjectId(info_id) if projectId != "" { item["projectId"] = SE2.EncodeString(projectId) } datas = append(datas, item) } else { log.Println("id", item["_id"], "信息为空") } }(v) } wg.Wait() count := Es.Count(EsIndex, EsType, esquery) Mgo.Update("entniche_rule", bson.M{"_id": tags["_id"]}, bson.M{ "$set": bson.M{ "i_estotal": count, }}, false, false) return UtilEsSaveData(sdataid, &datas), count } else { util.Debug(err) return err, 0 } } func ClearKeys(types, key string, data map[string]interface{}) { if strings.Contains(types, "1") { title := util.ObjToString(data["title"]) data["title"] = strings.ReplaceAll(title, key, "") } if strings.Contains(types, "2") { detail := util.ObjToString(data["detail"]) data["detail"] = strings.ReplaceAll(detail, key, "") } if strings.Contains(types, "3") { purchasing := util.ObjToString(data["purchasing"]) data["purchasing"] = strings.ReplaceAll(purchasing, key, "") } if strings.Contains(types, "4") { filetext := util.ObjToString(data["filetext"]) data["filetext"] = strings.ReplaceAll(filetext, key, "") } if strings.Contains(types, "5") { projectname := util.ObjToString(data["projectname"]) data["projectname"] = strings.ReplaceAll(projectname, key, "") } if strings.Contains(types, "6") || strings.Contains(types, "8") { buyer := util.ObjToString(data["buyer"]) data["buyer"] = strings.ReplaceAll(buyer, key, "") } if strings.Contains(types, "7") || strings.Contains(types, "9") { winner := util.ObjToString(data["s_winner"]) data["s_winner"] = strings.ReplaceAll(winner, key, "") } } func GetTimeInterval(startTime, entTime time.Time, n int) []map[string]interface{} { timeList := []map[string]interface{}{} endTimeTmp := startTime for endTimeTmp.Unix() < entTime.Unix() { var endTime_ time.Time startTime = endTimeTmp endTimeTmp = startTime.AddDate(0, n, 0) if endTimeTmp.Unix() > entTime.Unix() { endTime_ = entTime timeList = append(timeList, map[string]interface{}{"startTime": startTime.Unix(), "endTime": endTime_.Unix()}) break } else { endTime_ = endTimeTmp timeList = append(timeList, map[string]interface{}{"startTime": startTime.Unix(), "endTime": endTime_.Unix()}) } } //log.Println(timeList) return timeList } func Utiltags(tag map[string]interface{}) string { defer util.Catch() tmpbyte, _ := json.Marshal(tag) tab := models.Tag{} err := json.Unmarshal(tmpbyte, &tab) if err != nil { return "json err:" + err.Error() } //log.Println("前端数据", tab) QueryObjecct := sql.QueryObjecct{} ffBoolObject := sql.BoolObject{} adsBoolObect := sql.NewEsObject{} if tab.Sarea != "" { adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{Area: strings.Split(tab.Sarea, ",")}}) } if tab.Scity != "" { adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{City: strings.Split(tab.Scity, ",")}}) } if tab.Sdistrict != "" { //城市——区县 cityds := strings.Split(tab.Sdistrict, ",") var ds []string for _, v := range cityds { ds = append(ds, strings.Split(v, "-")[1]) } adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{District: ds}}) } if len(adsBoolObect.Bool.Should) > 0 { ffBoolObject.Must = append(ffBoolObject.Must, adsBoolObect) } if tab.Stoptype != "" || tab.Ssubtype != "" { if len(tab.Stoptype) > 0 { toptypeSubtype := sql.ToptypeSubtype{} toptypeSubtype.Toptype = strings.Split(tab.Stoptype, ",") ffBoolObject.Must = append(ffBoolObject.Must, sql.ToptypeSubtypeMust{&toptypeSubtype}) } if len(tab.Ssubtype) > 0 { toptypeSubtype := sql.ToptypeSubtype{} toptypeSubtype.Subtype = strings.Split(tab.Ssubtype, ",") ffBoolObject.Must = append(ffBoolObject.Must, sql.ToptypeSubtypeMust{&toptypeSubtype}) } } if tab.Istarttime > 0 && tab.Iendtime > 0 { ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime: &sql.Publishtime{ Gte: tab.Istarttime, Lt: tab.Iendtime, }}}) } else if tab.Istarttime > 0 { ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime1: &sql.Publishtime1{ Gte: tab.Istarttime, }}}) } else if tab.Iendtime > 0 { ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime2: &sql.Publishtime2{ Lt: tab.Iendtime, }}}) } if tab.Sglobalbuyerclass != "" { if len(tab.Sglobalbuyerclass) > 0 { buyerclass := sql.BuyerclassObject{ Terms: struct { Buyerclass []string `json:"buyerclass,omitempty"` }{ Buyerclass: strings.Split(tab.Sglobalbuyerclass, ","), }, } ffBoolObject.Must = append(ffBoolObject.Must, buyerclass) } } if tab.Sglobaltopscopeclass != "" || tab.Sglobalsubscopeclass != "" { if len(tab.Sglobaltopscopeclass) > 0 { topScopeclass := sql.Scopeclass{} topScopeclass.Globaltopscopeclass = strings.Split(tab.Sglobaltopscopeclass, ",") ffBoolObject.Must = append(ffBoolObject.Must, sql.ScopeclassMust{&topScopeclass}) } else if len(tab.Sglobalsubscopeclass) > 0 { subScopeclass := sql.Scopeclass{} subScopeclass.Globalsubscopeclass = strings.Split(tab.Sglobalsubscopeclass, ",") ffBoolObject.Must = append(ffBoolObject.Must, sql.ScopeclassMust{&subScopeclass}) } } fqBoolObject := sql.BoolObject{} if len(tab.Sexistfields) > 0 { tmpsfields := strings.Split(tab.Sexistfields, ",") for _, v := range tmpsfields { fqBoolObject.MustNot = append(fqBoolObject.MustNot, sql.ExistfieldsObjectMust{ ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct { Missing struct { Field string `json:"field,omitempty"` } `json:"missing,omitempty"` }{ Missing: struct { Field string `json:"field,omitempty"` }{Field: v}, }, }, }) } } newEsObject := sql.NewEsObject{} if tab.Sglobaladdkey != "" && tab.Sglobaladdkeymatch != "" { if tmps := strsToArr(tab.Sglobaladdkeymatch, "str"); tmps != nil { newEsObject = method1(newEsObject, tab.Sglobaladdkey, tab.Sglobaladdkeymatch, tmps) } } if len(newEsObject.Bool.Should) > 0 || len(newEsObject.Bool.MustNot) > 0 || len(newEsObject.Bool.Must) > 0 { fqBoolObject.Must = append(fqBoolObject.Must, newEsObject) } nots := sql.NewEsObject{} if tab.Sglobalnotkey != "" && tab.Sglobalnotkeymatch != "" { if tmps := strsToArr(tab.Sglobalnotkeymatch, "str"); tmps != nil { nots = method1(nots, tab.Sglobalnotkey, tab.Sglobalnotkeymatch, tmps) } } if len(nots.Bool.Should) > 0 { fqBoolObject.MustNot = append(fqBoolObject.MustNot, nots) } torules := sql.BoolObject{} for _, v := range tab.Orules { tmpses := sql.NewEsObject{} if skeymatchs := strsToArr(v.Skeymatch, "str"); skeymatchs != nil { tmpnewEsObject := sql.NewEsObject{} tmpnewEsObject = method1(tmpnewEsObject, v.Smatchkey, v.Skeymatch, skeymatchs) if len(tmpnewEsObject.Bool.Should) > 0 || len(tmpnewEsObject.Bool.Must) > 0 || len(tmpnewEsObject.Bool.MustNot) > 0 { tmpses.Bool.Must = append(tmpses.Bool.Must, tmpnewEsObject) } } if saddkeymatchs := strsToArr(v.Saddkeymatch, "str"); saddkeymatchs != nil { addkeyarr := sql.NewEsObject{} addkeyarr = method1(addkeyarr, v.Saddkey, v.Saddkeymatch, saddkeymatchs) if len(addkeyarr.Bool.Should) > 0 { tmpses.Bool.Must = append(tmpses.Bool.Must, addkeyarr) } } if snotkeymatchs := strsToArr(v.Snotkeymatch, "str"); snotkeymatchs != nil { tmpses = method2(tmpses, v.Snotkey, v.Snotkeymatch, snotkeymatchs) } if len(v.Stopscopeclass) > 0 { sArr := strings.Split(v.Stopscopeclass, ",") tmpses.Bool.Must = append(tmpses.Bool.Must, sql.TopscopeclassObject{ Terms: struct { Topscopeclass []string `json:"s_topscopeclass,omitempty"` }{ Topscopeclass: sArr, }, }) } else if len(v.Ssubscopeclass) > 0 { tmpses.Bool.Must = append(tmpses.Bool.Must, sql.SubscopeclassObject{ Terms: struct { Subscopeclass []string `json:"s_subscopeclass,omitempty"` }{ Subscopeclass: strings.Split(v.Ssubscopeclass, ","), }, }) } if len(v.Sbuyerclass) > 0 { tmpses.Bool.Must = append(tmpses.Bool.Must, sql.BuyerclassObject{ Terms: struct { Buyerclass []string `json:"buyerclass,omitempty"` }{ Buyerclass: strings.Split(v.Sbuyerclass, ","), }, }) } torules.Should = append(torules.Should, tmpses) } bidamountSql := sql.NewEsObject{} if tab.Sbidamount != "" { if strings.Contains(tab.Sbidamount, "大于") && strings.Contains(tab.Sbidamount, "小于") { arr := strings.Split(tab.Sbidamount, ",") limit := &sql.BudgetOrBidamount{ Gte: util.Float64All(strings.Replace(arr[0], "大于", "", -1)), Lt: util.Float64All(strings.Replace(arr[1], "小于", "", -1)), } bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}}) } else if strings.Contains(tab.Sbidamount, "大于") { limit := &sql.BudgetOrBidamount{ Gte: util.Float64All(strings.Replace(tab.Sbidamount, "大于", "", -1)), } bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}}) } else if strings.Contains(tab.Sbidamount, "小于") { limit := &sql.BudgetOrBidamount{ Lt: util.Float64All(strings.Replace(tab.Sbidamount, "小于", "", -1)), } bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}}) } if tab.SbidamountFieldExist == 1 { fqBoolObject.MustNot = append(fqBoolObject.MustNot, sql.ExistfieldsObjectMust{ ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct { Missing struct { Field string `json:"field,omitempty"` } `json:"missing,omitempty"` }{ Missing: struct { Field string `json:"field,omitempty"` }{Field: "bidamount"}, }, }, }) } } budgetSql := sql.NewEsObject{} if tab.Sbudget != "" { if strings.Contains(tab.Sbudget, "大于") && strings.Contains(tab.Sbudget, "小于") { arr := strings.Split(tab.Sbudget, ",") limit := &sql.BudgetOrBidamount{ Gte: util.Float64All(strings.Replace(arr[0], "大于", "", -1)), Lt: util.Float64All(strings.Replace(arr[1], "小于", "", -1)), } budgetSql.Bool.Should = append(budgetSql.Bool.Should, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}}) } else if strings.Contains(tab.Sbudget, "大于") { limit := &sql.BudgetOrBidamount{ Gte: util.Float64All(strings.Replace(tab.Sbudget, "大于", "", -1)), } budgetSql.Bool.Should = append(budgetSql.Bool.Should, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}}) } else if strings.Contains(tab.Sbudget, "小于") { limit := &sql.BudgetOrBidamount{ Lt: util.Float64All(strings.Replace(tab.Sbudget, "小于", "", -1)), } budgetSql.Bool.Should = append(budgetSql.Bool.Should, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}}) } if tab.SbudgetFieldExist == 1 { fqBoolObject.MustNot = append(fqBoolObject.MustNot, sql.ExistfieldsObjectMust{ ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct { Missing struct { Field string `json:"field,omitempty"` } `json:"missing,omitempty"` }{ Missing: struct { Field string `json:"field,omitempty"` }{Field: "budget"}, }, }, }) } } if len(ffBoolObject.Must) > 0 || len(ffBoolObject.MustNot) > 0 || len(ffBoolObject.Should) > 0 { QueryObjecct.Bool = &ffBoolObject } if len(fqBoolObject.Must) > 0 || len(fqBoolObject.MustNot) > 0 || len(fqBoolObject.Should) > 0 { if QueryObjecct.Bool == nil { QueryObjecct.Bool = &fqBoolObject } else { if fqBoolObject.Must != nil { QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, fqBoolObject.Must...) } if fqBoolObject.Should != nil { QueryObjecct.Bool.Should = append(QueryObjecct.Bool.Should, fqBoolObject.Should...) } if fqBoolObject.MustNot != nil { QueryObjecct.Bool.MustNot = append(QueryObjecct.Bool.MustNot, fqBoolObject.MustNot...) } } if len(torules.Should) > 0 { QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, map[string]interface{}{ "bool": torules, }) } } else if len(torules.Should) > 0 { if QueryObjecct.Bool == nil { QueryObjecct.Bool = &sql.BoolObject{} } QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, map[string]interface{}{ "bool": torules, }) } if (len(budgetSql.Bool.Should) > 0 || len(bidamountSql.Bool.Should) > 0) && (len(fqBoolObject.Must) > 0 || len(fqBoolObject.MustNot) > 0 || len(fqBoolObject.Should) > 0) { if QueryObjecct.Bool == nil { QueryObjecct.Bool = &sql.BoolObject{} } if len(budgetSql.Bool.Should) > 0 { QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, budgetSql) } if len(bidamountSql.Bool.Should) > 0 { QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, bidamountSql) } } else { if QueryObjecct.Bool == nil { QueryObjecct.Bool = &sql.BoolObject{} } if len(budgetSql.Bool.Should) > 0 { QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, budgetSql) } if len(bidamountSql.Bool.Should) > 0 { QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, bidamountSql) } } rdata := make(map[string]interface{}) rdata["query"] = QueryObjecct esbytes, _ := json.Marshal(rdata) esStr := strings.Replace(string(esbytes), "regexp1", "regexp", -1) esStr = strings.Replace(esStr, "publishtime1", "publishtime", -1) esStr = strings.Replace(esStr, "publishtime2", "publishtime", -1) return strings.Replace(esStr, "regexp2", "regexp", -1) } func strsToArr(strs string, typestr string) []string { if len(strs) > 0 { tmps := strings.Split(strs, ",") switch typestr { case "str": for index, vint := range tmps { switch vint { case TitleMatchType: tmps[index] = TitleMatchTypeStr case DetailMatchType: tmps[index] = DetailMatchTypeStr case PurchasingMatchType: tmps[index] = PurchasingMatchTypeStr case AttachmentsMatchType: tmps[index] = AttachmentsMatchTypeStr case ProjectnameMatchType: tmps[index] = ProjectnameMatchTypeStr case BuyerMatchType: tmps[index] = BuyerMatchTypeStr case SWinnerMatchType: tmps[index] = SWinnerMatchTypeStr case BuyerMatchType1: tmps[index] = BuyerRegexpMatchTypeStr case SWinnerMatchType1: tmps[index] = SWinnerRegexpMatchTypeStr } } return tmps case "field": for index, vint := range tmps { switch vint { case TitleMatchType: tmps[index] = TitleMatchTypeField case DetailMatchType: tmps[index] = DetailMatchTypeField case PurchasingMatchType: tmps[index] = PurchasingMatchTypeField case AttachmentsMatchType: tmps[index] = AttachmentsMatchTypeField case ProjectnameMatchType: tmps[index] = ProjectnameMatchTypeField case BuyerMatchType: tmps[index] = BuyerMatchTypeField case SWinnerMatchType: tmps[index] = SWinnerMatchTypeField case BuyerMatchType1: tmps[index] = BuyerMatchTypeField case SWinnerMatchType1: tmps[index] = SWinnerMatchTypeField } } return tmps } } return nil } func init() { EsIndex = util.ObjToString(Sysconfig["elasticsearch_index"]) EsType = util.ObjToString(Sysconfig["elasticsearch_type"]) } func method1(newEsObject sql.NewEsObject, keyword, keymatch string, tmps []string) sql.NewEsObject { keywordArr := []string{} for _, vv := range strings.Split(keyword, ",") { if vv == "" { continue } //log.Println(vv, tmps, keymatch) if len(tmps) == 1 && (strings.Contains(keymatch, "8") || strings.Contains(keymatch, "9")) { //中标单位、采购单位、中标单位(正则)、采购单位(正则) 单选 strs := "" if strings.Contains(vv, "&&") { //strs = ".*" + strings.Replace(vv, "&&", ".*", -1) + ".*" strs = strings.Replace(vv, "&&", ".*", -1) } else { strs = vv } if tmps[0] == "buyer.mbuyer" { newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: strs, Type: MultiMatchType, Fields: tmps, }, }) } else if tmps[0] == "s_winner.mwinner" { newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: strs, Type: MultiMatchType, Fields: tmps, }, }) } } else if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) { addkeylines := strings.Split(vv, "&&") keywordArr = append(keywordArr, addkeylines[0]) } else { addkeylines := strings.Split(vv, "&&") //log.Println(addkeylines, "addkeylines") if len(addkeylines) > 1 { addkeyline := sql.NewEsObject{} for _, vvv := range addkeylines { if vvv == "" { continue } addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: vvv, Type: MultiMatchType, Fields: tmps, }, }) } if len(addkeyline.Bool.Must) > 0 { newEsObject.Bool.Should = append(newEsObject.Bool.Should, addkeyline) } } else { newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: vv, Type: MultiMatchType, Fields: tmps, }, }) } } } if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) { addkeyline := sql.NewEsObject{} if tmps[0] == "buyer" { addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.BuyerMatch{ Buyer: &sql.Buyer{Buyer: keywordArr}, }) } else if tmps[0] == "s_winner" { addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.WinnerMatch{ Winner: &sql.Winner{Winner: keywordArr}, }) } if len(addkeyline.Bool.Must) > 0 { newEsObject.Bool.Should = append(newEsObject.Bool.Should, addkeyline) } } return newEsObject } func method2(newEsObject sql.NewEsObject, keyword, keymatch string, tmps []string) sql.NewEsObject { notKeyWordArr := []string{} for _, vv := range strings.Split(keyword, ",") { if vv == "" { continue } if len(tmps) == 1 && (strings.Contains(keymatch, "8") || strings.Contains(keymatch, "9")) { //中标单位、采购单位、中标单位(正则)、采购单位(正则) 单选 //strs := "" //if strings.Contains(vv, "&&") { // strs = ".*" + strings.Replace(vv, "&&", ".*", -1) + ".*" //} else { // strs = ".*" + vv + ".*" //} if tmps[0] == "buyer.mbuyer" { newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: vv, Type: MultiMatchType, Fields: tmps, }, }) } else if tmps[0] == "s_winner.mwinner" { newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: vv, Type: MultiMatchType, Fields: tmps, }, }) } } else if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) { notkeylines := strings.Split(vv, "&&") notKeyWordArr = append(notKeyWordArr, notkeylines[0]) } else { notkeylines := strings.Split(vv, "&&") if len(notkeylines) > 1 { notkeyline := sql.NewEsObject{} for _, vvv := range notkeylines { if vvv == "" { continue } notkeyline.Bool.Must = append(notkeyline.Bool.Must, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: vvv, Type: MultiMatchType, Fields: tmps, }, }) } if len(notkeyline.Bool.Must) > 0 { newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, notkeyline) } } else { newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{ MultiMatch: &sql.MultiMatch{ Query: vv, Type: MultiMatchType, Fields: tmps, }, }) } } } if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) { var notkeyline interface{} if tmps[0] == "buyer" { notkeyline = sql.BuyerMatch{ Buyer: &sql.Buyer{Buyer: notKeyWordArr}, } } else if tmps[0] == "s_winner" { notkeyline = sql.WinnerMatch{ Winner: &sql.Winner{Winner: notKeyWordArr}, } } if notkeyline != nil { newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, notkeyline) } } return newEsObject } /** * 一级公告行业处理 d、t、p */ func method3(arr []string) []string { var sArr []string for _, v := range arr { sArr = append(sArr, v+"d") sArr = append(sArr, v+"t") sArr = append(sArr, v+"p") } return sArr } func GetMactchKey(match []map[string]string, data map[string]interface{}) string { keyWord := []string{} for _, keys := range match { types := keys["s_keymatch"] key := keys["s_matchkey"] if strings.Contains(types, "1") { title := util.ObjToString(data["title"]) keyWord = KeyWordToData(types, title, key, keyWord) } if strings.Contains(types, "2") { detail := util.ObjToString(data["detail"]) keyWord = KeyWordToData(types, detail, key, keyWord) } if strings.Contains(types, "3") { purchasing := util.ObjToString(data["purchasing"]) keyWord = KeyWordToData(types, purchasing, key, keyWord) } if strings.Contains(types, "4") { filetext := util.ObjToString(data["filetext"]) keyWord = KeyWordToData(types, filetext, key, keyWord) } if strings.Contains(types, "5") { projectname := util.ObjToString(data["projectname"]) keyWord = KeyWordToData(types, projectname, key, keyWord) } if strings.Contains(types, "6") || strings.Contains(types, "8") { buyer := util.ObjToString(data["buyer"]) keyWord = KeyWordToData(types, buyer, key, keyWord) } if strings.Contains(types, "7") || strings.Contains(types, "9") { winner := util.ObjToString(data["s_winner"]) keyWord = KeyWordToData(types, winner, key, keyWord) } } keyMap := map[string]bool{} keyArr := []string{} for _, key := range keyWord { keyMap[key] = true } for k, _ := range keyMap { keyArr = append(keyArr, k) } return strings.Join(keyArr, ",") } func KeyWordToData(types, item, key string, keyWord []string) []string { for _, mk := range strings.Split(key, ",") { if strings.Contains(mk, "&&") { arr := strings.Split(mk, "&&") isok := true for _, s := range arr { if s != "" { if !strings.Contains(strings.ToUpper(item), strings.ToUpper(s)) { isok = false } } } if isok { keyWord = append(keyWord, mk) } } else { if strings.Contains(strings.ToUpper(item), strings.ToUpper(mk)) { keyWord = append(keyWord, mk) } } } return keyWord } func GetProjectId(id string) string { query := `{"query": {"bool": {"must": [{"term": {"ids": "%s"}}],"must_not": [],"should": []}}}` querys := fmt.Sprintf(query, id) projectId := "" projectIdArr := []string{} data := Es.Get("projectset", "projectset", querys) if data != nil && *data != nil && len(*data) > 0 { for _, v := range *data { projectIdArr = append(projectIdArr, util.ObjToString(v["_id"])) } projectId = strings.Join(projectIdArr, ",") log.Println("projectId", projectId) } else { log.Println("ES未查到项目id", id) } return projectId }