elasticsearch_dsl.go 3.2 KB

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