elasticsearch_dsl.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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) 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. queryMatch = fmt.Sprintf(queryMatch, keyWords, "20%")
  55. ridTerms = fmt.Sprintf(ridTerms, entId)
  56. sql = fmt.Sprintf(sql, queryMatch, ridTerms)
  57. //log.Println("sql", sql)
  58. return sql
  59. }
  60. func GetQueryOT(tags, question, keywords, repositoryId string) (qstr string) {
  61. var query = `{"query":{"bool":{"must":[%s%s%s%s]}}}`
  62. queryMatch := ``
  63. queryTerms := ``
  64. queryId := ``
  65. queryQues := ``
  66. if keywords != "" {
  67. queryMatch = `{"match":{"knowledgeKeyWords":{"query":"` + keywords + `","minimum_should_match":"40%"}}},`
  68. //query_match = `{"match":{"questions.question":{"query":"` + keywords + `","minimum_should_match":"20%"}}},`
  69. }
  70. tags = strings.Replace(tags, ` `, `","`, -1)
  71. if tags != "" {
  72. queryTerms = `{"terms":{"tags.code":["` + tags + `"]}},`
  73. }
  74. if repositoryId != "" {
  75. queryId = `{"terms":{"repositoryId":[` + repositoryId + `]}}`
  76. }
  77. if question != "" {
  78. queryQues = `,{"match": {"smart.questions.question": {"query": "` + question + `","fuzziness": "AUTO","operator": "and"}}}`
  79. }
  80. qstr = fmt.Sprintf(query, queryMatch, queryTerms, queryId, queryQues)
  81. return qstr
  82. }