package service import ( "errors" "fmt" "sfbase/core" "sfbase/elastic" "sfbase/global" "sfis/db" "strconv" "strings" "time" ) var ( pjt_count = 100 pjt_field = `"_id","jgtime","zbtime","projectname","buyer","buyerclass"` pjt_fields = `"_id","area","city","bidamount","budget","zbtime","jgtime","projectname","projectcode","s_winner","buyer","buyerclass","buyerperson","buyertel","sourceinfourl"` pjt_sort = `{"firsttime":-1}` 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, times string, isDetail bool) (data []map[string]interface{}, httpStatus int, err error) { times = strings.TrimSpace(times) winner = strings.TrimSpace(winner) pjt_name := strings.TrimSpace(projectName) pjt_len := len([]rune(pjt_name)) httpStatus = 200 qstr := "" timestr := "" if times != "" { start := times + " 00:00:00" end := times + " 23:59:59" loc, _ := time.LoadLocation("Local") startTimes := "" endTimes := "" if startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, loc); err == nil { startTimes = fmt.Sprint(startTime.Unix()) } if endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil { endTimes = fmt.Sprint(endTime.Unix()) } // if start == "" && end != "" { // timestr = `{"range": {"firsttime": {"lte": ` + end + `}}}` // } else if start != "" && end == "" { // timestr = `{"range": {"firsttime": {"gte": ` + start + `}}}` // } else if start != "" && end != "" { timestr = `{"range": {"firsttime": {"gte": ` + startTimes + `,"lte": ` + endTimes + `}}}` // } } if pjt_len >= 4 && winner == "" { qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name)) if timestr != "" { qstr = fmt.Sprintf(query, timestr, 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)) if timestr != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, fmt.Sprintf(query_string, pjt_name, pjt_name)) } } else if winner != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "") if timestr != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, "") } } else { err = errors.New("项目名长度小于4") return } 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) fields := "" if isDetail { fields = pjt_fields } else { fields = pjt_field } repl := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, pjt_count, 0, false) if repl != nil && len(*repl) > 0 { data = *repl } return } 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 } }