123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package util
- import (
- "encoding/json"
- "fmt"
- "github.com/zeromicro/go-zero/core/logx"
- "strings"
- )
- /*项目中所用到的几类查询dsl语句构建工具类*/
- func DSL4SmartResponse(question string, entId string, msgType int, addr, index, segment string) 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, segment)
- if len(hanlpCutWords) == 0 {
- hanlpCutWords = append(hanlpCutWords, question)
- }
- question = strings.Join(hanlpCutWords, " ")
- lenQuestion := len([]rune(question))
- //logx.Info("lenQuestion", 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, addr+"/"+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, mark int) string {
- var (
- sql = `{"query": {"bool": {"must": [%s%s]}}}`
- queryMatch = `{"match":{"keywords":{"query":"%s","minimum_should_match":"%s"}}}`
- ridTerms = `,{"term":{"entId":%s}}`
- )
- if mark == 1 {
- queryMatch = fmt.Sprintf(queryMatch, keyWords, "30%")
- } else {
- 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
- }
- var queryStr = `{"_source": ["question","intention","answer"],"size": %d, "min_score":%v,"query": {"bool": {"must": [{"term":{"entId":%v}},{"script_score": {"query": {"match_all": {}},"script": {"source": "cosineSimilarity(params.queryVector,'questionVector')+1", "params": {"queryVector": %v}}}}]}}}`
- func GetAnswerQueryStr(question string, entId string, size int, minScore float64) string {
- qv, _ := EncodeVector(question)
- bs, err := json.Marshal(qv)
- if err != nil {
- logx.Info("向量序列化失败:", err)
- }
- return fmt.Sprintf(queryStr, size, minScore, entId, string(bs))
- }
|