elasticsearch_dsl.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package util
  2. import (
  3. "fmt"
  4. . "knowledgeBase/rpc/knowledge/init"
  5. "log"
  6. "strings"
  7. )
  8. /*
  9. 项目中所用到的几类查询dsl语句构建工具类
  10. */
  11. func DSL4SmartResponse(question string, entId string, msgType int) 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, C.Segment)
  21. question = strings.Join(hanlpCutWords, "")
  22. lenQuestion := len([]rune(question))
  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, "http://39.106.145.77:9201/smart/_analyze")
  39. mustque := ElasticSmartIK(question, C.Es.Addr+"/"+C.Es.Index+"/_analyze")
  40. if mustque != "" {
  41. postFilter = fmt.Sprintf(postFilter, mustque)
  42. }
  43. query = fmt.Sprintf(query, "", typeStr, question, queryPercent, entId)
  44. queryDSL := fmt.Sprintf(totalQuery, postFilter, query, `"answer","question"`, 1)
  45. log.Println("queryDSL:", queryDSL)
  46. return queryDSL
  47. }
  48. return ""
  49. }
  50. func DSL4SearchByKwsOrid(keyWords string, entId string) string {
  51. var (
  52. sql = `{"query": {"bool": {"must": [%s%s]}}}`
  53. queryMatch = `{"match":{"keywords":{"query":"%s","minimum_should_match":"%s"}}}`
  54. ridTerms = `,{"term":{"entId":%s}}`
  55. )
  56. queryMatch = fmt.Sprintf(queryMatch, keyWords, "20%")
  57. ridTerms = fmt.Sprintf(ridTerms, entId)
  58. sql = fmt.Sprintf(sql, queryMatch, ridTerms)
  59. //log.Println("sql", sql)
  60. return sql
  61. }
  62. func GetQueryOT(tags, question, keywords, repositoryId string) (qstr string) {
  63. var query = `{"query":{"bool":{"must":[%s%s%s%s]}}}`
  64. queryMatch := ``
  65. queryTerms := ``
  66. queryId := ``
  67. queryQues := ``
  68. if keywords != "" {
  69. queryMatch = `{"match":{"knowledgeKeyWords":{"query":"` + keywords + `","minimum_should_match":"40%"}}},`
  70. //query_match = `{"match":{"questions.question":{"query":"` + keywords + `","minimum_should_match":"20%"}}},`
  71. }
  72. tags = strings.Replace(tags, ` `, `","`, -1)
  73. if tags != "" {
  74. queryTerms = `{"terms":{"tags.code":["` + tags + `"]}},`
  75. }
  76. if repositoryId != "" {
  77. queryId = `{"terms":{"repositoryId":[` + repositoryId + `]}}`
  78. }
  79. if question != "" {
  80. queryQues = `,{"match": {"smart.questions.question": {"query": "` + question + `","fuzziness": "AUTO","operator": "and"}}}`
  81. }
  82. qstr = fmt.Sprintf(query, queryMatch, queryTerms, queryId, queryQues)
  83. return qstr
  84. }