search.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "net/http"
  19. "strings"
  20. "github.com/88250/gulu"
  21. "github.com/88250/lute/html"
  22. "github.com/gin-gonic/gin"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. func findReplace(c *gin.Context) {
  27. ret := gulu.Ret.NewResult()
  28. defer c.JSON(http.StatusOK, ret)
  29. arg, ok := util.JsonArg(c, ret)
  30. if !ok {
  31. return
  32. }
  33. k := arg["k"].(string)
  34. r := arg["r"].(string)
  35. methodArg := arg["method"]
  36. var method int // 0:文本,1:查询语法,2:SQL,3:正则表达式
  37. if nil != methodArg {
  38. method = int(methodArg.(float64))
  39. }
  40. idsArg := arg["ids"].([]interface{})
  41. var ids []string
  42. for _, id := range idsArg {
  43. ids = append(ids, id.(string))
  44. }
  45. err := model.FindReplace(k, r, ids, method)
  46. if nil != err {
  47. ret.Code = -1
  48. ret.Msg = err.Error()
  49. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  50. return
  51. }
  52. return
  53. }
  54. func searchAsset(c *gin.Context) {
  55. ret := gulu.Ret.NewResult()
  56. defer c.JSON(http.StatusOK, ret)
  57. arg, ok := util.JsonArg(c, ret)
  58. if !ok {
  59. return
  60. }
  61. k := arg["k"].(string)
  62. ret.Data = model.SearchAssetsByName(k)
  63. return
  64. }
  65. func searchTag(c *gin.Context) {
  66. ret := gulu.Ret.NewResult()
  67. defer c.JSON(http.StatusOK, ret)
  68. arg, ok := util.JsonArg(c, ret)
  69. if !ok {
  70. return
  71. }
  72. k := arg["k"].(string)
  73. tags := model.SearchTags(k)
  74. ret.Data = map[string]interface{}{
  75. "tags": tags,
  76. "k": k,
  77. }
  78. }
  79. func searchWidget(c *gin.Context) {
  80. ret := gulu.Ret.NewResult()
  81. defer c.JSON(http.StatusOK, ret)
  82. arg, ok := util.JsonArg(c, ret)
  83. if !ok {
  84. return
  85. }
  86. keyword := arg["k"].(string)
  87. blocks := model.SearchWidget(keyword)
  88. ret.Data = map[string]interface{}{
  89. "blocks": blocks,
  90. "k": keyword,
  91. }
  92. }
  93. func searchTemplate(c *gin.Context) {
  94. ret := gulu.Ret.NewResult()
  95. defer c.JSON(http.StatusOK, ret)
  96. arg, ok := util.JsonArg(c, ret)
  97. if !ok {
  98. return
  99. }
  100. keyword := arg["k"].(string)
  101. blocks := model.SearchTemplate(keyword)
  102. ret.Data = map[string]interface{}{
  103. "blocks": blocks,
  104. "k": keyword,
  105. }
  106. }
  107. func searchEmbedBlock(c *gin.Context) {
  108. ret := gulu.Ret.NewResult()
  109. defer c.JSON(http.StatusOK, ret)
  110. arg, ok := util.JsonArg(c, ret)
  111. if !ok {
  112. return
  113. }
  114. embedBlockID := arg["embedBlockID"].(string)
  115. stmt := arg["stmt"].(string)
  116. excludeIDsArg := arg["excludeIDs"].([]interface{})
  117. var excludeIDs []string
  118. for _, excludeID := range excludeIDsArg {
  119. excludeIDs = append(excludeIDs, excludeID.(string))
  120. }
  121. headingMode := 0 // 0:带标题下方块
  122. headingModeArg := arg["headingMode"]
  123. if nil != headingModeArg {
  124. headingMode = int(headingModeArg.(float64))
  125. }
  126. breadcrumb := false
  127. breadcrumbArg := arg["breadcrumb"]
  128. if nil != breadcrumbArg {
  129. breadcrumb = breadcrumbArg.(bool)
  130. }
  131. blocks := model.SearchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
  132. ret.Data = map[string]interface{}{
  133. "blocks": blocks,
  134. }
  135. }
  136. func searchRefBlock(c *gin.Context) {
  137. ret := gulu.Ret.NewResult()
  138. defer c.JSON(http.StatusOK, ret)
  139. arg, ok := util.JsonArg(c, ret)
  140. if !ok {
  141. return
  142. }
  143. reqId := arg["reqId"]
  144. ret.Data = map[string]interface{}{"reqId": reqId}
  145. if nil == arg["id"] {
  146. return
  147. }
  148. rootID := arg["rootID"].(string)
  149. id := arg["id"].(string)
  150. keyword := arg["k"].(string)
  151. beforeLen := int(arg["beforeLen"].(float64))
  152. blocks, newDoc := model.SearchRefBlock(id, rootID, keyword, beforeLen)
  153. ret.Data = map[string]interface{}{
  154. "blocks": blocks,
  155. "newDoc": newDoc,
  156. "k": html.EscapeHTMLStr(keyword),
  157. "reqId": arg["reqId"],
  158. }
  159. }
  160. func fullTextSearchBlock(c *gin.Context) {
  161. ret := gulu.Ret.NewResult()
  162. defer c.JSON(http.StatusOK, ret)
  163. arg, ok := util.JsonArg(c, ret)
  164. if !ok {
  165. return
  166. }
  167. query := arg["query"].(string)
  168. pathArg := arg["path"]
  169. var path string
  170. if nil != pathArg {
  171. path = pathArg.(string)
  172. }
  173. var box string
  174. if "" != path {
  175. box = strings.Split(path, "/")[0]
  176. path = strings.TrimPrefix(path, box)
  177. }
  178. var types map[string]bool
  179. if nil != arg["types"] {
  180. typesArg := arg["types"].(map[string]interface{})
  181. types = map[string]bool{}
  182. for t, b := range typesArg {
  183. types[t] = b.(bool)
  184. }
  185. }
  186. methodArg := arg["method"]
  187. var method int // 0:文本,1:查询语法,2:SQL,3:正则表达式
  188. if nil != methodArg {
  189. method = int(methodArg.(float64))
  190. }
  191. groupByArg := arg["groupBy"]
  192. var groupBy int // 0:不分组,1:按文档分组
  193. if nil != groupByArg {
  194. groupBy = int(groupByArg.(float64))
  195. }
  196. blocks, matchedBlockCount, matchedRootCount := model.FullTextSearchBlock(query, box, path, types, method, groupBy)
  197. ret.Data = map[string]interface{}{
  198. "blocks": blocks,
  199. "matchedBlockCount": matchedBlockCount,
  200. "matchedRootCount": matchedRootCount,
  201. }
  202. }