Skip to content
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

* 搜索引擎新增根据内容获取分词结果接口 #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,19 @@ func (engine *Engine) FlushIndex() {
}
}

// 获取文本的分词结果
func (engine *Engine) Tokens(text []byte) (tokens []string) {
querySegments := engine.segmenter.Segment(text)
for _, s := range querySegments {
token := s.Token().Text()
if !engine.stopTokens.IsStopToken(token) {
tokens = append(tokens, token)
}
}

return tokens
}

// 查找满足搜索条件的文档,此函数线程安全
func (engine *Engine) Search(request types.SearchRequest) (output types.SearchResponse) {
if !engine.initialized {
Expand All @@ -296,13 +309,7 @@ func (engine *Engine) Search(request types.SearchRequest) (output types.SearchRe
// 收集关键词
tokens := []string{}
if request.Text != "" {
querySegments := engine.segmenter.Segment([]byte(request.Text))
for _, s := range querySegments {
token := s.Token().Text()
if !engine.stopTokens.IsStopToken(token) {
tokens = append(tokens, s.Token().Text())
}
}
tokens = engine.Tokens([]byte(request.Text))
} else {
for _, t := range request.Tokens {
tokens = append(tokens, t)
Expand Down