123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package common
- import (
- qutil "app.yhyue.com/moapp/jybase/common"
- "fmt"
- "strings"
- )
- // GetCommonQuerySqlWithAggs 此方法用于聚合查询
- func (mae *MarketAnalysisEntity) GetCommonQuerySqlWithAggs() string {
- return fmt.Sprintf(mae.GetCommonQuerySql(), `,"aggs":{%s},"size":%d %s`)
- }
- // GetCommonQuerySql 公共筛选
- func (mae *MarketAnalysisEntity) GetCommonQuerySql() string {
- var (
- musts, bools []string
- name string
- )
- //时间
- if mae.Types == 1 {
- name = "firsttime"
- } else if mae.Types == 2 {
- name = "publishtime"
- }
- musts = append(musts, fmt.Sprintf(`{"range":{"%s":{"gte":%d}}}`, name, mae.FormatParam.STime))
- //地区
- /*if len(mae.FormatParam.Area) > 0 || len(mae.FormatParam.City) > 0 {
- var areaCity []string
- if len(mae.FormatParam.Area) > 0 {
- areaCity = append(areaCity, fmt.Sprintf(`{"terms":{"area":["%s"]}}`, strings.Join(mae.FormatParam.Area, `","`)))
- }
- if len(mae.FormatParam.City) > 0 {
- areaCity = append(areaCity, fmt.Sprintf(`{"terms":{"city":["%s"]}}`, strings.Join(mae.FormatParam.City, `","`)))
- }
- musts = append(musts, fmt.Sprintf(`{"bool":{"should":[%s],"minimum_should_match": 1}}`, strings.Join(areaCity, ",")))
- }
- //行业
- if len(mae.FormatParam.Industry) > 0 {
- musts = append(musts, fmt.Sprintf(`{"terms":{"subscopeclass":["%s"]}}`, strings.Join(mae.FormatParam.Industry, `","`)))
- }
- //类型
- if len(mae.FormatParam.BuyerClass) > 0 {
- musts = append(musts, fmt.Sprintf(`{"terms":{"buyerclass":["%s"]}}`, strings.Join(mae.FormatParam.BuyerClass, `","`)))
- }*/
- if len(mae.FormatParam.SubType) > 0 {
- musts = append(musts, fmt.Sprintf(`{"terms":{"subtype":["%s"]}}`, strings.Join(mae.FormatParam.SubType, `","`)))
- }
- //分析报告中标状态限制
- if mae.Types == 1 {
- musts = append(musts, fmt.Sprintf(query_bool_must, PSearch_DecMust))
- }
- //订阅词
- for _, v := range getAllKeywordArr(mae.FormatParam.KeysItems) {
- if sql := getKeyWordSql(v); sql != "" {
- bools = append(bools, sql)
- }
- }
- return fmt.Sprintf(`{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}}%s}`, strings.Join(musts, ","), strings.Join(bools, ","), qutil.If(len(bools) > 0, 1, 0).(int), "%s")
- }
- // getAllKeywordArr 获取所有匹配词
- func getAllKeywordArr(res []keyWordGroup) (rData []viewKeyWord) {
- for _, kwg := range res {
- rData = append(rData, getGroupKeywordArr(kwg.A_Key)...)
- }
- return
- }
- // getGroupKeywordArr 模糊拆分为多个精准匹配
- func getGroupKeywordArr(res []viewKeyWord) (rData []viewKeyWord) {
- for _, kw := range res {
- if kw.MatchWay == 1 {
- for _, kk := range kw.Keyword {
- rData = append(rData, viewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- for _, kk := range kw.Appended {
- rData = append(rData, viewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- } else {
- rData = append(rData, kw)
- }
- }
- return
- }
- func getKeyWordSql(v viewKeyWord) string {
- var shoulds, must_not []string
- //附加词
- for _, vv := range v.Keyword {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
- }
- for _, vv := range v.Appended {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
- }
- //排除词
- for _, vv := range v.Exclude {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- must_not = append(must_not, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
- }
- //添加
- if len(shoulds) > 0 {
- notStr := ""
- if len(must_not) > 0 {
- notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
- }
- return fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr)
- }
- return ""
- }
|