package util import ( "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/internal/config" "fmt" "github.com/zeromicro/go-zero/core/logx" "log" "strings" ) /* 项目中所用到的几类查询dsl语句构建工具类 */ func DSL4SmartResponse(question string, entId string, msgType int) string { var ( totalQuery = `{"post_filter":{%s},"query":{%s},"_source":[%s],"size":%d}` postFilter = `"script":{"script":"def sk=_source.must_keywords;def n=0;for(item in sk){ n++;if(que.indexOf(item)>-1){return true}};if(n==0){ return true}","params":{"que":"%s"}}` query = `"bool":{"must_not":[%s],"must":[{"match":{"%s":{"query":"%s","minimum_should_match":"%s"}}},{"term":{"entId":"%s"}}]}` ) var typeStr string /*1.首先将问题使用hanlp分词*/ //hanlpCutWords := HanlpGetNormalWords(question, "http://39.106.145.77:8080/api/segment") hanlpCutWords := HanlpGetNormalWords(question, config.C.Segment) log.Println("hanlp分词结果:", hanlpCutWords) if len(hanlpCutWords) == 0 { hanlpCutWords = append(hanlpCutWords, question) } question = strings.Join(hanlpCutWords, "") lenQuestion := len([]rune(question)) //log.Println("len", lenQuestion) if lenQuestion >= 2 { queryPercent := "40%" if lenQuestion < 5 { queryPercent = "85%" } else if lenQuestion < 9 { queryPercent = "60%" } else if lenQuestion < 12 { queryPercent = "55%" } if msgType == 1 { typeStr = "keywords" } else if msgType == 2 { //百度语音过来的 typeStr = "keywords.key_pinyin" } /*2使用sik分词将问题分词以获取更多查询词语*/ //mustque := ElasticSmartIK(question, "http://39.106.145.77:9201/smart/_analyze") mustque := ElasticSmartIK(question, config.C.Es.Addr+"/"+config.C.Es.Index+"/_analyze") if mustque != "" { postFilter = fmt.Sprintf(postFilter, mustque) } query = fmt.Sprintf(query, "", typeStr, question, queryPercent, entId) queryDSL := fmt.Sprintf(totalQuery, postFilter, query, `"answer","question"`, 1) logx.Info("queryDSL:", queryDSL) return queryDSL } return "" } func DSL4SearchByKwsOrid(keyWords string, entId string) string { var ( sql = `{"query": {"bool": {"must": [%s%s]}}}` queryMatch = `{"match":{"keywords":{"query":"%s","minimum_should_match":"%s"}}}` ridTerms = `,{"term":{"entId":%s}}` ) queryMatch = fmt.Sprintf(queryMatch, keyWords, "20%") ridTerms = fmt.Sprintf(ridTerms, entId) sql = fmt.Sprintf(sql, queryMatch, ridTerms) //log.Println("sql", sql) return sql } func GetQueryOT(tags, question, keywords, repositoryId string) (qstr string) { var query = `{"query":{"bool":{"must":[%s%s%s%s]}}}` queryMatch := `` queryTerms := `` queryId := `` queryQues := `` if keywords != "" { queryMatch = `{"match":{"knowledgeKeyWords":{"query":"` + keywords + `","minimum_should_match":"40%"}}},` //query_match = `{"match":{"questions.question":{"query":"` + keywords + `","minimum_should_match":"20%"}}},` } tags = strings.Replace(tags, ` `, `","`, -1) if tags != "" { queryTerms = `{"terms":{"tags.code":["` + tags + `"]}},` } if repositoryId != "" { queryId = `{"terms":{"repositoryId":[` + repositoryId + `]}}` } if question != "" { queryQues = `,{"match": {"smart.questions.question": {"query": "` + question + `","fuzziness": "AUTO","operator": "and"}}}` } qstr = fmt.Sprintf(query, queryMatch, queryTerms, queryId, queryQues) return qstr }