123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- package es
- import (
- "fmt"
- "jyBXCore/rpc/bxcore"
- IC "jyBXCore/rpc/init"
- "strconv"
- "strings"
- "time"
- MC "app.yhyue.com/moapp/jybase/common"
- elastic "app.yhyue.com/moapp/jybase/es"
- "log"
- )
- // GetSearchQuery 整理关键词等查询条件
- func GetSearchQuery(in *bxcore.SearchReq, mustQuery string) (qstr string) {
- var (
- //搜索范围是否只有附件
- //搜索范围只选择附件,是否有附件条件无效;
- isFileSearch = in.SelectType == "filetext"
- wordsMusts, wordsShould, musts, mustNot []string
- findFields string
- selectTypeArr = strings.Split(in.SelectType, ",")
- )
- if selectTypeArr == nil || len(selectTypeArr) == 0 {
- findFields = `"title"`
- } else {
- findFields = fmt.Sprintf(`"%s"`, strings.Join(selectTypeArr, "\",\""))
- }
- switchBool := strings.Contains(findFields, "detail") && strings.Contains(findFields, "title") && IC.C.SearchTypeSwitch
- if mustQuery != "" {
- musts = append(musts, mustQuery)
- }
- //此时关键词中间有IC.C.JYKeyMark进行隔离
- if in.KeyWords != "" {
- var (
- keyWordsMusts []string
- )
- for _, v := range strings.Split(in.KeyWords, IC.C.JYKeyMark) {
- if elastic.ReplaceYH(v) == "" {
- continue
- }
- //单个字 搜索范围 有全文或者附件 无标题 例如:学 虚拟机 detail 搜索的时候加上标题
- //detail 正文不支持单字查询
- if len([]rune(elastic.ReplaceYH(v))) == 1 && DetailFileORTitle(findFields) {
- findFields += `,"title"`
- } else if switchBool && len([]rune(elastic.ReplaceYH(v))) > 1 {
- //标题 全文搜索 搜索类型开关打开 默认搜索全文;(全文包含标题)(单字排除)
- if strings.Contains(findFields, `"title",`) {
- findFields = strings.Replace(findFields, `"title",`, ``, -1)
- } else if strings.Contains(findFields, `,"title"`) {
- findFields = strings.Replace(findFields, `,"title"`, ``, -1)
- }
- }
- keyWordsMusts = append(keyWordsMusts, fmt.Sprintf(fmt.Sprintf(multiMatch, "%s", findFields), elastic.ReplaceYH(v)))
- }
- //搜索关键词模式;默认0:包含所有,1:包含任意
- if in.WordsMode == 1 {
- wordsShould = append(wordsShould, fmt.Sprintf(elastic.NgramMust, strings.Join(keyWordsMusts, ",")))
- } else {
- wordsMusts = append(wordsMusts, keyWordsMusts...)
- }
- }
- //附加词
- if in.AdditionalWords != "" {
- //多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
- var (
- addWordsMusts []string
- )
- for _, aws := range strings.Split(in.AdditionalWords, ",") {
- var (
- addWordsMust []string
- )
- for _, v := range strings.Split(aws, IC.C.JYKeyMark) {
- if elastic.ReplaceYH(v) == "" {
- continue
- }
- //单个字 搜索范围 有全文或者附件 无标题 例如:学 虚拟机 detail 搜索的时候加上标题
- //detail 正文不支持单字查询
- if len([]rune(elastic.ReplaceYH(v))) == 1 && DetailFileORTitle(findFields) {
- findFields += `,"title"`
- } else if switchBool && len([]rune(elastic.ReplaceYH(v))) > 1 {
- //标题 全文搜索 搜索类型开关打开 默认搜索全文;(全文包含标题)(单字排除)
- if strings.Contains(findFields, `"title",`) {
- findFields = strings.Replace(findFields, `"title",`, ``, -1)
- } else if strings.Contains(findFields, `,"title"`) {
- findFields = strings.Replace(findFields, `,"title"`, ``, -1)
- }
- }
- addWordsMust = append(addWordsMust, fmt.Sprintf(fmt.Sprintf(multiMatch, "%s", findFields), elastic.ReplaceYH(v)))
- addWordsMusts = append(addWordsMusts, addWordsMust...)
- if in.WordsMode == 0 {
- wordsMusts = append(wordsMusts, addWordsMust...)
- }
- addWordsMust = []string{}
- }
- //搜索关键词模式;默认0:包含所有,1:包含任意
- if in.WordsMode == 1 {
- wordsShould = append(wordsShould, fmt.Sprintf(elastic.NgramMust, strings.Join(addWordsMusts, ",")))
- addWordsMusts = []string{}
- }
- }
- }
- //搜索关键词模式;默认0:包含所有,1:包含任意
- //包含任意一组
- if len(wordsShould) > 0 {
- musts = append(musts, fmt.Sprintf(queryBoolShould, strings.Join(wordsShould, ",")))
- } else if len(wordsMusts) > 0 {
- musts = append(musts, fmt.Sprintf(elastic.NgramMust, strings.Join(wordsMusts, ",")))
- }
- //排除词
- if notKey := strings.TrimSpace(in.ExclusionWords); notKey != "" {
- notKeyMultiMatch := fmt.Sprintf(multiMatch, "%s", findFields)
- var notKeyMustNot []string
- //多组排除词
- for _, nks := range strings.Split(notKey, ",") {
- //单组排除词 空格分割
- for _, v := range strings.Split(nks, IC.C.JYKeyMark) {
- v = strings.TrimSpace(v)
- if v == "" {
- continue
- }
- if len([]rune(elastic.ReplaceYH(v))) == 1 {
- //单个字 搜索范围 有全文或者附件 无标题 例如:学 虚拟机 detail 搜索的时候加上标题
- if DetailFileORTitle(findFields) {
- notKeyMultiMatch = fmt.Sprintf(multiMatch, "%s", findFields+`,"title"`)
- }
- }
- notKeyMustNot = append(notKeyMustNot, fmt.Sprintf(notKeyMultiMatch, elastic.ReplaceYH(v)))
- }
- }
- mustNot = append(mustNot, fmt.Sprintf(queryBoolShould, strings.Join(notKeyMustNot, ",")))
- }
- //行业
- if in.Industry != "" {
- musts = append(musts, fmt.Sprintf(queryBoolMust, `"`+strings.ReplaceAll(in.Industry, ",", `","`)+`"`))
- }
- //价格
- if in.Price != "" && len(strings.Split(in.Price, "-")) > 1 {
- minPrice, maxPrice := strings.Split(in.Price, "-")[0], strings.Split(in.Price, "-")[1]
- 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(queryBoolShould, fmt.Sprintf(queryBoolMustBoolShould, sq, sq))
- musts = append(musts, query_price)
- }
- }
- }
- //采购单位联系方式
- hasBuyerTel := in.BuyerTel
- if hasBuyerTel != "" {
- if hasBuyerTel == "y" {
- musts = append(musts, fmt.Sprintf(queryExists, "buyertel"))
- } else {
- mustNot = append(mustNot, fmt.Sprintf(queryExists, "buyertel"))
- }
- }
- //中标企业联系方式
- hasWinnerTel := in.WinnerTel
- if hasWinnerTel != "" {
- if hasWinnerTel == "y" {
- musts = append(musts, fmt.Sprintf(queryExists, "winnertel"))
- } else {
- mustNot = append(mustNot, fmt.Sprintf(queryExists, "winnertel"))
- }
- }
- //附件
- fileExists := in.FileExists
- if !isFileSearch && fileExists != "" {
- if fileExists == "1" { //有附件
- musts = append(musts, fmt.Sprintf(queryBoolMustTerm, true))
- } else if fileExists == "-1" { //无附件
- mustNot = append(mustNot, fmt.Sprintf(queryBoolMustTerm, true))
- }
- }
- // 如果是领域化数据则需要加标签
- if in.BidField != "" {
- musts = append(musts, fmt.Sprintf(queryBoolMustTermDomain, in.BidField))
- }
- qstr = fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(mustNot, ","))
- log.Println("qstr:", qstr)
- return
- }
- // GetBidSearchQuery 整理地区、城市、发布时间、信息类型、采购单位类型 查询条件
- func GetBidSearchQuery(in *bxcore.SearchReq) string {
- query := ``
- //省份
- area := in.Province
- if area != "" {
- query += `{"terms":{"area":[`
- for k, v := range strings.Split(area, ",") {
- if k > 0 {
- query += `,`
- }
- query += `"` + v + `"`
- }
- query += `]}}`
- }
- //市
- city := in.City
- if city != "" {
- if len(query) > 0 {
- query += ","
- }
- query += `{"terms":{"city":[`
- for k, v := range strings.Split(city, ",") {
- if k > 0 {
- query += `,`
- }
- query += `"` + v + `"`
- }
- query += `]}}`
- }
- district := in.District
- if district != "" {
- if len(query) > 0 {
- query += ","
- }
- for k, v := range strings.Split(district, ",") {
- if k > 0 {
- query += `,`
- }
- cityName := strings.Split(v, "_")[0]
- districtName := strings.Split(v, "_")[1]
- query_bool_must_and_district := `{"bool":{"must":[{"terms":{"city":["%s"]}},{"terms":{"district":["%s"]}}]}}`
- query += fmt.Sprintf(query_bool_must_and_district, cityName, districtName)
- }
- }
- /*if district != "" {
- for _, v := range strings.Split(district, ",") {
- if len(v) > 0 {
- query += ","
- }
- cityName := strings.Split(v, "_")[0]
- districtName := strings.Split(v, "_")[1]
- query_bool_must_and_district := `{"bool":{"must":[{"terms":{"area":["%s"]}},{"terms":{"district":["%s"]}}]}}`
- query += fmt.Sprintf(query_bool_must_and_district, cityName, districtName)
- query += `{"terms":{"city":[`
- for k, v := range strings.Split(city, ",") {
- if k > 0 {
- query += `,`
- }
- query += `"` + v + `"`
- }
- query += `]}}`
- }
- }*/
- if query != "" {
- query = fmt.Sprintf(queryBoolShould, query)
- }
- //发布时间
- publishTime := in.PublishTime
- if publishTime != "" && len(strings.Split(publishTime, "-")) > 1 {
- if len(query) > 0 {
- query += ","
- }
- startTime := strings.Split(publishTime, "-")[0]
- endTime := strings.Split(publishTime, "-")[1]
- query += `{"range":{"publishtime":{`
- if startTime != "" {
- query += `"gte":` + startTime
- }
- if startTime != "" && endTime != "" {
- query += `,`
- }
- if endTime != "" {
- //电脑端 时间选择 开始时间当天和结束时间当天相同
- if startTime == 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 += `"lt":` + endTime
- }
- query += `}}}`
- }
- //信息类型-二级
- subtype := in.Subtype
- topType := MC.If(in.TopType != "", strings.Split(in.TopType, ","), []string{}).([]string)
- allType := ``
- //二级分类
- if subtype != "" {
- var typeInt = 0
- allType += `{"terms":{"subtype":[`
- for k, v := range strings.Split(subtype, ",") {
- if tType := MC.If(topTypeMap[v] != "" && in.TopType == "", topTypeMap[v], "").(string); tType != "" {
- topType = append(topType, tType)
- typeInt += 1
- continue
- }
- if k > typeInt {
- allType += `,`
- }
- allType += `"` + v + `"`
- }
- allType += `]}}`
- if typeInt == len(strings.Split(subtype, ",")) {
- allType = ``
- }
- }
- //信息类型 一级分类
- log.Println("topType:", topType)
- if len(topType) > 0 {
- if allType != "" {
- allType += ","
- }
- allType += `{"terms":{"toptype":[`
- for k, v := range topType {
- if k > 0 {
- allType += `,`
- }
- allType += `"` + v + `"`
- }
- allType += `]}}`
- }
- if allType != "" {
- if query != "" {
- query += ","
- }
- query += fmt.Sprintf(queryBoolShould, allType)
- }
- //采购单位类型
- buyerClass := in.BuyerClass
- 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
- }
- // DetailFileORTitle 包含正文或 附件 不包含标题
- func DetailFileORTitle(findFields string) bool {
- return (strings.Contains(findFields, `"detail"`) || strings.Contains(findFields, `"filetext"`)) && !strings.Contains(findFields, `"title"`)
- }
|