1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package util
- import (
- "fmt"
- "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
- }
|