浏览代码

推送时"的问题

renzheng 8 年之前
父节点
当前提交
3a694ee5fb
共有 1 个文件被更改,包括 70 次插入8 次删除
  1. 70 8
      common/src/qfw/util/elastic/elasticutil.go

+ 70 - 8
common/src/qfw/util/elastic/elasticutil.go

@@ -907,18 +907,21 @@ func Count(index, itype string, query interface{}) int64 {
 //{"multi_match": {"query": "$word","type": "phrase", "fields": [$field],"analyzer": "my_ngram"}}
 //"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {"detail": {"fragment_size": 1,"number_of_fragments": 1},"title": {"fragment_size": 1,"number_of_fragments": 1}}}
 const (
-	FilterQuery  = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
-	NgramStr     = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match" : 1}}}`
-	NgramMust    = `{"bool":{"must":[%s]}}`
-	minq         = `{"multi_match": {"query": "%s","type": "phrase", "fields": [%s],"analyzer": "my_ngram"}}`
-	HL           = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
-	highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
+	//此处最后少一个},正好NgramStr取[1:]多一个}
+	FilterQuery     = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
+	NgramStr        = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match" : 1}}}`
+	NgramMust       = `{"bool":{"must":[%s]}}`
+	NgramMustAndNot = `{"bool":{"must":[%s],"must_not":[%s]}}`
+	minq            = `{"multi_match": {"query": "%s","type": "phrase", "fields": [%s],"analyzer": "my_ngram"}}`
+	HL              = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
+	highlightStr    = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
 
 	FilterQuery_New  = `{"query":{"bool":{"must": [%s%s%s],"should":[]}}}`
 	MatchQueryString = `{"match": {%s: { "query":"%s", "operator": "and"}}}`
 	HL_New           = `"highlight": {"pre_tags": ["<HL>"],"post_tags": ["<HL>"],"fields": {%s}}`
 )
 
+//替换了"号
 func GetNgramQuery(query interface{}, mustquery, findfields string) (qstr string) {
 	var words []string
 	if q, ok := query.(string); ok {
@@ -937,7 +940,7 @@ func GetNgramQuery(query interface{}, mustquery, findfields string) (qstr string
 			qws := strings.Split(qs_words, "+")
 			mq := []string{}
 			for _, qs_word := range qws {
-				mq = append(mq, fmt.Sprintf(new_minq, qs_word))
+				mq = append(mq, fmt.Sprintf(new_minq, ReplaceYH(qs_word)))
 			}
 			musts = append(musts, fmt.Sprintf(NgramMust, strings.Join(mq, ",")))
 		}
@@ -1046,6 +1049,7 @@ func GetByNgramOther(index, itype string, query interface{}, mustquery, findfiel
 }
 
 //增加高亮、过滤查询
+//替换了"号
 func GetByNgramAll(index, itype string, query interface{}, mustquery, findfields, order, fields string, start, limit int, highlight bool, filtermode bool) *[]map[string]interface{} {
 	defer util.Catch()
 	qstr := ""
@@ -1072,7 +1076,7 @@ func GetByNgramAll(index, itype string, query interface{}, mustquery, findfields
 		if start > -1 {
 			qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
 		}
-		//log.Println("ngram-find", qstr)
+		//	log.Println("ngram-find", qstr)
 		return Get(index, itype, qstr)
 	} else {
 		return nil
@@ -1111,3 +1115,61 @@ func GetByNgramAll_New(index, itype string, querystring, querymust interface{},
 		return nil
 	}
 }
+
+type KeyConfig struct {
+	Keys      []string `json:"key"`
+	NotKeys   []string `json:"notkey"`
+	InfoTypes []string `json:"infotype"`
+	Areas     []string `json:"area"`
+}
+
+//替换了"号
+func GetResForJY(index, itype string, keys []KeyConfig, allquery, findfields, SortQuery, fields string, start, limit int) *[]map[string]interface{} {
+	if len(keys) > 0 {
+		qstr := ""
+		new_minq := fmt.Sprintf(minq, "%s", findfields)
+		musts := []string{}
+		for _, qs_words := range keys {
+			mq := []string{}
+			notmq := []string{}
+			for _, qs_word := range qs_words.Keys {
+				mq = append(mq, fmt.Sprintf(new_minq, ReplaceYH(qs_word)))
+			}
+			for _, qs_word := range qs_words.NotKeys {
+				notmq = append(notmq, fmt.Sprintf(new_minq, ReplaceYH(qs_word)))
+			}
+			if len(qs_words.Areas) > 0 {
+				mq = append(mq, fmt.Sprintf(`{"terms":{"area":["%s"]}}`, strings.Join(qs_words.Areas, `","`)))
+			}
+			if len(qs_words.InfoTypes) > 0 {
+				mq = append(mq, fmt.Sprintf(`{"terms":{"toptype":["%s"]}}`, strings.Join(qs_words.InfoTypes, `","`)))
+			}
+			musts = append(musts, fmt.Sprintf(NgramMustAndNot, strings.Join(mq, ","), strings.Join(notmq, ",")))
+		}
+		qstr = fmt.Sprintf(NgramStr, "", strings.Join(musts, ","))
+
+		qstr = fmt.Sprintf(FilterQuery, allquery, qstr[1:])
+		ws := []string{}
+		for _, w := range strings.Split(findfields, ",") {
+			ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
+		}
+		qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
+		if len(fields) > 0 {
+			qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
+		}
+		if len(SortQuery) > 0 {
+			qstr = qstr[:len(qstr)-1] + `,"sort":` + SortQuery + `}`
+		}
+		if start > -1 {
+			qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
+		}
+		//log.Println("jy-ngram-find", qstr)
+		return Get(index, itype, qstr)
+	} else {
+		return nil
+	}
+}
+
+func ReplaceYH(src string) (rpl string) {
+	return strings.Replace(src, `"`, `\"`, -1)
+}