elasticsearch_dsl.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/core/logx"
  5. . "knowledgeBase/rpc/knowledge/init"
  6. "log"
  7. "strings"
  8. )
  9. /*
  10. 项目中所用到的几类查询dsl语句构建工具类
  11. */
  12. func DSL4SmartResponse(question string, entId string, msgType int) string {
  13. var (
  14. totalQuery = `{"post_filter":{%s},"query":{%s},"_source":[%s],"size":%d}`
  15. 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"}}`
  16. query = `"bool":{"must_not":[%s],"must":[{"match":{"%s":{"query":"%s","minimum_should_match":"%s"}}},{"term":{"entId":"%s"}}]}`
  17. )
  18. var typeStr string
  19. /*1.首先将问题使用hanlp分词*/
  20. //hanlpCutWords := HanlpGetNormalWords(question, "http://39.106.145.77:8080/api/segment")
  21. hanlpCutWords := HanlpGetNormalWords(question, C.Segment)
  22. question = strings.Join(hanlpCutWords, "")
  23. lenQuestion := len([]rune(question))
  24. log.Println("len", lenQuestion)
  25. if lenQuestion >= 2 {
  26. queryPercent := "40%"
  27. if lenQuestion < 5 {
  28. queryPercent = "85%"
  29. } else if lenQuestion < 9 {
  30. queryPercent = "60%"
  31. } else if lenQuestion < 12 {
  32. queryPercent = "55%"
  33. }
  34. if msgType == 1 {
  35. typeStr = "keywords"
  36. } else if msgType == 2 { //百度语音过来的
  37. typeStr = "keywords.key_pinyin"
  38. }
  39. /*2使用sik分词将问题分词以获取更多查询词语*/
  40. //mustque := ElasticSmartIK(question, "http://39.106.145.77:9201/smart/_analyze")
  41. mustque := ElasticSmartIK(question, C.Es.Addr+"/"+C.Es.Index+"/_analyze")
  42. if mustque != "" {
  43. postFilter = fmt.Sprintf(postFilter, mustque)
  44. }
  45. query = fmt.Sprintf(query, "", typeStr, question, queryPercent, entId)
  46. queryDSL := fmt.Sprintf(totalQuery, postFilter, query, `"answer","question"`, 1)
  47. logx.Info("queryDSL:", queryDSL)
  48. return queryDSL
  49. }
  50. return ""
  51. }
  52. func DSL4SearchByKwsOrid(keyWords string, entId string) string {
  53. var (
  54. sql = `{"query": {"bool": {"must": [%s%s]}}}`
  55. queryMatch = `{"match":{"keywords":{"query":"%s","minimum_should_match":"%s"}}}`
  56. ridTerms = `,{"term":{"entId":%s}}`
  57. )
  58. queryMatch = fmt.Sprintf(queryMatch, keyWords, "20%")
  59. ridTerms = fmt.Sprintf(ridTerms, entId)
  60. sql = fmt.Sprintf(sql, queryMatch, ridTerms)
  61. //log.Println("sql", sql)
  62. return sql
  63. }
  64. func GetQueryOT(tags, question, keywords, repositoryId string) (qstr string) {
  65. var query = `{"query":{"bool":{"must":[%s%s%s%s]}}}`
  66. queryMatch := ``
  67. queryTerms := ``
  68. queryId := ``
  69. queryQues := ``
  70. if keywords != "" {
  71. queryMatch = `{"match":{"knowledgeKeyWords":{"query":"` + keywords + `","minimum_should_match":"40%"}}},`
  72. //query_match = `{"match":{"questions.question":{"query":"` + keywords + `","minimum_should_match":"20%"}}},`
  73. }
  74. tags = strings.Replace(tags, ` `, `","`, -1)
  75. if tags != "" {
  76. queryTerms = `{"terms":{"tags.code":["` + tags + `"]}},`
  77. }
  78. if repositoryId != "" {
  79. queryId = `{"terms":{"repositoryId":[` + repositoryId + `]}}`
  80. }
  81. if question != "" {
  82. queryQues = `,{"match": {"smart.questions.question": {"query": "` + question + `","fuzziness": "AUTO","operator": "and"}}}`
  83. }
  84. qstr = fmt.Sprintf(query, queryMatch, queryTerms, queryId, queryQues)
  85. return qstr
  86. }