-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/search #6
Merged
Merged
Feat/search #6
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa6595f
feat(search): search dto
wsm25 cd7290b
feat(search): basic design
wsm25 74f5441
feat(search): add search methods and fields
wsm25 2c57986
feat(search): fenci
wsm25 dabea6e
feat(search): modify domain and dto models
wsm25 c8d6d75
fix(search): ci
wsm25 28ba13d
feat(search): replace GenSearchIdx with gorm hook
wsm25 76bd0d5
feat(search): minor refactor
wsm25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ type TeacherListFilter struct { | |
Pinyin string | ||
PinyinAbbr string | ||
ContainCourseIDs []int64 | ||
SearchQuery string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package po | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"jcourse_go/util" | ||
"strings" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
type SearchIndex string | ||
|
||
// ref: https://gorm.io/zh_CN/docs/data_types.html | ||
|
||
// warning: need manual migeration! | ||
func (i *SearchIndex) Scan(value interface{}) error { return nil } | ||
func (i SearchIndex) Value() (driver.Value, error) { return nil, nil } | ||
func (i SearchIndex) GormDataType() string { return "tsvector" } | ||
|
||
func (i SearchIndex) GormValue(ctx context.Context, db *gorm.DB) clause.Expr { | ||
return clause.Expr{ | ||
SQL: "to_tsvector('simple', ?)", | ||
Vars: []interface{}{string(i)}, | ||
} | ||
} | ||
|
||
func toIndex(fields []string) SearchIndex { | ||
var sb strings.Builder | ||
for _, field := range fields { | ||
for _, segment := range util.Fenci(field) { | ||
sb.WriteString(segment) | ||
sb.WriteByte(' ') | ||
} | ||
} | ||
return SearchIndex(sb.String()) | ||
} | ||
|
||
func (c *CoursePO) BeforeCreate(*gorm.DB) error { | ||
c.SearchIndex = toIndex([]string{ | ||
c.Name, | ||
c.Code, // 前缀模糊匹配更为适合 | ||
c.MainTeacherName, | ||
c.Department, // 不分词更为适合 | ||
}) | ||
return nil | ||
} | ||
func (c *CoursePO) BeforeSave(tx *gorm.DB) error { | ||
return c.BeforeCreate(tx) | ||
} | ||
|
||
func (t *TeacherPO) BeforeCreate(*gorm.DB) error { | ||
t.SearchIndex = toIndex([]string{ | ||
t.Name, | ||
t.Department, | ||
t.Code, | ||
}) | ||
return nil | ||
} | ||
func (t *TeacherPO) BeforeSave(tx *gorm.DB) error { | ||
return t.BeforeCreate(tx) | ||
} | ||
|
||
func (t *TrainingPlanPO) BeforeCreate(*gorm.DB) error { | ||
t.SearchIndex = toIndex([]string{ | ||
t.Major, | ||
t.Department, | ||
}) | ||
return nil | ||
} | ||
func (t *TrainingPlanPO) BeforeSave(tx *gorm.DB) error { | ||
return t.BeforeCreate(tx) | ||
} | ||
|
||
func (r *ReviewPO) BeforeCreate(*gorm.DB) error { | ||
r.SearchIndex = toIndex([]string{ | ||
r.Comment, | ||
}) | ||
return nil | ||
} | ||
func (r *ReviewPO) BeforeSave(tx *gorm.DB) error { | ||
return r.BeforeCreate(tx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package repository | ||
|
||
import ( | ||
"jcourse_go/util" | ||
"strings" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
func (*CourseQuery) WithSearch(query string) DBOption { return withSearch(query) } | ||
func (*ReviewQuery) WithSearch(query string) DBOption { return withSearch(query) } | ||
func (*TeacherQuery) WithSearch(query string) DBOption { return withSearch(query) } | ||
func (*TrainingPlanQuery) WithSearch(query string) DBOption { return withSearch(query) } | ||
|
||
func withSearch(query string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("search_index @@ to_tsquery('simple', ?)", | ||
userQueryToTsQuery(query), | ||
) | ||
} | ||
} | ||
|
||
// 目前只搜用户名 | ||
func (*UserQuery) WithSearch(query string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("name LIKE ?", query+"%") | ||
} | ||
} | ||
|
||
// 空格分割的每个词都要匹配,词内分词做模糊匹配 | ||
func userQueryToTsQuery(query string) string { | ||
var sb strings.Builder | ||
words := strings.Fields(query) | ||
for i, word := range words { | ||
if i != 0 { | ||
sb.WriteString(" & ") | ||
} | ||
sb.WriteByte('(') | ||
segs := util.Fenci(word) | ||
for j, seg := range segs { | ||
if j != 0 { | ||
sb.WriteString(" | ") | ||
} | ||
sb.WriteString(seg) | ||
} | ||
sb.WriteByte(')') | ||
} | ||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥一个接收器是指针,另外几个不是
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://gorm.io/docs/data_types.html 因为文档如此 抄的(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
行吧,IDE和lint如果没有警告的话就这样了