elasticsearch_dsl.go 3.0 KB

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