|
@@ -907,15 +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 (
|
|
|
+ //此处最后少一个},正好NgramStr取[1:]多一个}
|
|
|
FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
|
|
|
- FilterQuery_New = `{"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 {
|
|
@@ -934,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, ",")))
|
|
|
}
|
|
@@ -946,6 +952,64 @@ func GetNgramQuery(query interface{}, mustquery, findfields string) (qstr string
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func GetNgramQuery_New(querystring, querymust interface{}, must, findfields string) (qstring string) {
|
|
|
+ querymust_string := ""
|
|
|
+ var wordsMust []string
|
|
|
+ if q, ok := querymust.(string); ok {
|
|
|
+ if q != "" {
|
|
|
+ wordsMust = strings.Split(q, ",")
|
|
|
+ }
|
|
|
+ } else if q, ok := querymust.([]string); ok {
|
|
|
+ wordsMust = q
|
|
|
+ } else if q, ok := querymust.([]interface{}); ok {
|
|
|
+ wordsMust = util.ObjArrToStringArr(q)
|
|
|
+ }
|
|
|
+ if wordsMust != nil {
|
|
|
+ new_minq := fmt.Sprintf(minq, "%s", findfields)
|
|
|
+ musts := []string{}
|
|
|
+ for _, qs_wordsMust := range wordsMust {
|
|
|
+ qws := strings.Split(qs_wordsMust, "+")
|
|
|
+ mq := []string{}
|
|
|
+ for _, qs_word := range qws {
|
|
|
+ mq = append(mq, fmt.Sprintf(new_minq, qs_word))
|
|
|
+ }
|
|
|
+ musts = append(musts, fmt.Sprintf(NgramMust, strings.Join(mq, ",")))
|
|
|
+ }
|
|
|
+ querymust_string = strings.Join(musts, ",")
|
|
|
+ }
|
|
|
+ //log.Println("must", must, querymust_string)
|
|
|
+
|
|
|
+ //querystring---------------------------------------------
|
|
|
+ query_string := ""
|
|
|
+ var querysShold []string
|
|
|
+ if q, ok := querystring.(string); ok {
|
|
|
+ if q != "" {
|
|
|
+ querysShold = strings.Split(q, ",")
|
|
|
+ }
|
|
|
+ } else if q, ok := querystring.([]string); ok {
|
|
|
+ querysShold = q
|
|
|
+ } else if q, ok := querystring.([]interface{}); ok {
|
|
|
+ querysShold = util.ObjArrToStringArr(q)
|
|
|
+ }
|
|
|
+ if querysShold != nil {
|
|
|
+ for k, name := range strings.Split(findfields, ",") {
|
|
|
+ for _, qs_querysShold := range querysShold {
|
|
|
+ if k > 0 {
|
|
|
+ query_string = query_string + "," + fmt.Sprintf(MatchQueryString, fmt.Sprint(name), qs_querysShold)
|
|
|
+ } else {
|
|
|
+ query_string = query_string + fmt.Sprintf(MatchQueryString, fmt.Sprint(name), qs_querysShold)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //log.Println("querystring", query_string)
|
|
|
+ if querymust_string == "" {
|
|
|
+ qstring = fmt.Sprintf(FilterQuery_New, must, query_string, querymust_string)
|
|
|
+ } else {
|
|
|
+ qstring = fmt.Sprintf(FilterQuery_New, must, query_string, ","+querymust_string)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
func GetByNgram(index, itype string, query interface{}, mustquery, findfields, order, fields string, start, limit int) *[]map[string]interface{} {
|
|
|
return GetByNgramAll(index, itype, query, mustquery, findfields, order, fields, start, limit, false, false)
|
|
|
}
|
|
@@ -985,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 := ""
|
|
@@ -1011,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
|
|
@@ -1019,22 +1084,21 @@ func GetByNgramAll(index, itype string, query interface{}, mustquery, findfields
|
|
|
}
|
|
|
|
|
|
//增加高亮、过滤查询
|
|
|
-func GetByNgramAll_New(index, itype string, query interface{}, mustquery, findfields, order, fields string, start, limit int, highlight bool, filtermode bool) *[]map[string]interface{} {
|
|
|
+func GetByNgramAll_New(index, itype string, querystring, querymust interface{}, mustquery, findfields, order, fields string, start, limit int, highlight bool, filtermode bool) *[]map[string]interface{} {
|
|
|
defer util.Catch()
|
|
|
qstr := ""
|
|
|
- if mustquery != "" && filtermode {
|
|
|
- qstr = GetNgramQuery(query, "", findfields)
|
|
|
- qstr = fmt.Sprintf(FilterQuery_New, mustquery, qstr[1:])
|
|
|
+ if filtermode {
|
|
|
+ qstr = GetNgramQuery_New(querystring, querymust, mustquery, findfields)
|
|
|
} else {
|
|
|
- qstr = GetNgramQuery(query, mustquery, findfields)
|
|
|
+ qstr = GetNgramQuery_New(querystring, "", mustquery, findfields)
|
|
|
}
|
|
|
if qstr != "" {
|
|
|
if highlight {
|
|
|
ws := []string{}
|
|
|
for _, w := range strings.Split(findfields, ",") {
|
|
|
- ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
|
|
|
+ ws = append(ws, w+`:{"force_source": true}`)
|
|
|
}
|
|
|
- qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
|
|
|
+ qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL_New, strings.Join(ws, ",")) + `}`
|
|
|
}
|
|
|
if len(fields) > 0 {
|
|
|
qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
|
|
@@ -1045,9 +1109,68 @@ func GetByNgramAll_New(index, itype string, query interface{}, mustquery, findfi
|
|
|
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", order, qstr)
|
|
|
+ return Get(index, itype, qstr)
|
|
|
+ } else {
|
|
|
+ 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)
|
|
|
+ not_new_minq := fmt.Sprintf(minq, "%s", `"title"`) //排除词只查询标题
|
|
|
+ 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(not_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)
|
|
|
+}
|