MCPcopy Index your code
hub / github.com/TruthHun/BookStack / SearchDocument

Method SearchDocument

models/document_search_result.go:97–125  ·  view source on GitHub ↗

书籍内搜索.

(keyword string, bookId int, page, size int)

Source from the content-addressed store, hash-verified

95
96//书籍内搜索.
97func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int, page, size int) (docs []*DocumentSearchResult, cnt int, err error) {
98 o := orm.NewOrm()
99
100 fields := []string{"document_id", "document_name", "identify", "book_id"}
101 sql := "SELECT %v FROM md_documents WHERE book_id = " + strconv.Itoa(bookId) + " AND (document_name LIKE ? OR `release` LIKE ?) "
102 sqlCount := fmt.Sprintf(sql, "count(document_id) cnt")
103 sql = fmt.Sprintf(sql, strings.Join(fields, ",")) + " order by vcnt desc"
104 if bookId == 0 {
105 // bookId 为 0 的时候,只搜索公开的书籍的文档
106 sql = "SELECT %v FROM md_documents d left join md_books b on d.book_id=b.book_id WHERE b.privately_owned=0 and (d.document_name LIKE ? OR d.`release` LIKE ? )"
107 sqlCount = fmt.Sprintf(sql, "count(d.document_id) cnt")
108 sql = fmt.Sprintf(sql, "d."+strings.Join(fields, ",d.")) + " order by d.vcnt desc"
109 }
110
111 keyword = "%" + keyword + "%"
112
113 var count struct {
114 Cnt int
115 }
116
117 o.Raw(sqlCount, keyword, keyword).QueryRow(&count)
118 cnt = count.Cnt
119
120 limit := fmt.Sprintf(" limit %v offset %v", size, (page-1)*size)
121 if cnt > 0 {
122 _, err = o.Raw(sql+limit, keyword, keyword).QueryRows(&docs)
123 }
124 return
125}
126
127// 根据id查询搜索结果
128func (m *DocumentSearchResult) GetDocsById(id []int, withoutCont ...bool) (docs []DocResult, err error) {

Callers 3

SearchMethod · 0.80
ResultMethod · 0.80
SearchDocMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected