elasticsearch_dsl.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package util
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. /*项目中所用到的几类查询dsl语句构建工具类*/
  7. func DSL4SmartResponse(question string, entId string, msgType int, addr, index, segment string) string {
  8. var (
  9. totalQuery = `{"post_filter":{%s},"query":{%s},"_source":[%s],"size":%d}`
  10. 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"}}`
  11. query = `"bool":{"must_not":[%s],"must":[{"match":{"%s":{"query":"%s","minimum_should_match":"%s"}}},{"term":{"entId":"%s"}}]}`
  12. )
  13. var typeStr string
  14. /*1.首先将问题使用hanlp分词*/
  15. hanlpCutWords := HanlpGetNormalWords(question, segment)
  16. if len(hanlpCutWords) == 0 {
  17. hanlpCutWords = append(hanlpCutWords, question)
  18. }
  19. question = strings.Join(hanlpCutWords, " ")
  20. lenQuestion := len([]rune(question))
  21. //logx.Info("lenQuestion", lenQuestion)
  22. if lenQuestion >= 2 {
  23. queryPercent := "40%"
  24. if lenQuestion < 5 {
  25. queryPercent = "85%"
  26. } else if lenQuestion < 9 {
  27. queryPercent = "60%"
  28. } else if lenQuestion < 12 {
  29. queryPercent = "55%"
  30. }
  31. if msgType == 1 {
  32. typeStr = "keywords"
  33. } else if msgType == 2 { //百度语音过来的
  34. typeStr = "keywords.key_pinyin"
  35. }
  36. /*2使用sik分词将问题分词以获取更多查询词语*/
  37. mustque := ElasticSmartIK(question, addr+"/"+index+"/_analyze")
  38. if mustque != "" {
  39. postFilter = fmt.Sprintf(postFilter, mustque)
  40. }
  41. query = fmt.Sprintf(query, "", typeStr, question, queryPercent, entId)
  42. queryDSL := fmt.Sprintf(totalQuery, postFilter, query, `"answer","question"`, 1)
  43. //logx.Info("queryDSL:", queryDSL)
  44. return queryDSL
  45. }
  46. return ""
  47. }
  48. func DSL4SearchByKwsOrid(keyWords string, entId string, mark int) string {
  49. var (
  50. sql = `{"query": {"bool": {"must": [%s%s]}}}`
  51. queryMatch = `{"match":{"keywords":{"query":"%s","minimum_should_match":"%s"}}}`
  52. ridTerms = `,{"term":{"entId":%s}}`
  53. )
  54. if mark == 1 {
  55. queryMatch = fmt.Sprintf(queryMatch, keyWords, "30%")
  56. } else {
  57. queryMatch = fmt.Sprintf(queryMatch, keyWords, "20%")
  58. }
  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. }
  87. var queryStr = `{"_source": ["question","intention","answer"],"size": %d, "min_score":%v,
  88. "query": {"bool": {"must": [{"term":{"entId":%v}"},{"script_score": {"query": {"match_all": {}},"script": {"source": "cosineSimilarity(params.queryVector,'questionVector')+1", "params": {"queryVector": %v}}}}]}}}`
  89. func GetAnswerQueryStr(question string, entId string, size int, minScore float64) string {
  90. qv, _ := EncodeVector(question)
  91. return fmt.Sprintf(queryStr, size, minScore, entId, qv)
  92. }