Skip to content

Commit

Permalink
fix(search): 修复search测试 (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
juniaoshaonian authored Sep 2, 2024
1 parent 1c1d169 commit a14bd26
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
23 changes: 20 additions & 3 deletions internal/search/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ func (s *HandlerTestSuite) TestBizSearch() {
Status: 2,
Result: 0,
},
{
Id: 5,
Uid: 1,
Labels: []string{"test_label"},
Title: "Elasticsearch标题",
Content: "Elasticsearch内容",
GithubRepo: "Elasticsearch github代码库",
GiteeRepo: "Elasticsearch gitee代码库",
Keywords: "Elasticsearch关键词",
Shorthand: "Elasticsearch速记",
Highlight: "Elasticsearch亮点",
Guidance: "Elasticsearch引导",
Status: 2,
Result: 1,
},
{
Id: 2,
Uid: 1,
Expand Down Expand Up @@ -1290,6 +1305,7 @@ func (s *HandlerTestSuite) TestSearchLimit() {
s.initSkills()
},
after: func(t *testing.T, wantRes web.SearchResult, actual web.SearchResult) {

for idx := range actual.Skills {
require.True(t, actual.Skills[idx].Utime != "")
actual.Skills[idx].Utime = ""
Expand Down Expand Up @@ -1324,6 +1340,7 @@ func (s *HandlerTestSuite) TestSearchLimit() {
Name: "",
Desc: "test_desc",
},

{
ID: 4,
Labels: []string{"programming"},
Expand Down Expand Up @@ -2177,6 +2194,6 @@ func handlerSkillLevel(t *testing.T, sk web.SkillLevel) web.SkillLevel {
return sk
}

//func TestHandler(t *testing.T) {
// suite.Run(t, new(HandlerTestSuite))
//}
func TestHandler(t *testing.T) {
suite.Run(t, new(HandlerTestSuite))
}
15 changes: 12 additions & 3 deletions internal/search/internal/repository/dao/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dao
import (
"strings"

"github.com/ecodeclub/ekit/slice"

"github.com/ecodeclub/webook/internal/search/internal/domain"
"github.com/olivere/elastic/v7"
)
Expand All @@ -13,6 +15,8 @@ type Col struct {
Name string
// 权重
Boost int
// 是否是精确匹配
IsTerm bool
}

func buildCols(cols map[string]Col, queryMetas []domain.QueryMeta) []elastic.Query {
Expand All @@ -29,9 +33,14 @@ func buildCols(cols map[string]Col, queryMetas []domain.QueryMeta) []elastic.Que
queries := make([]elastic.Query, 0, len(colMap))
for colname, keyword := range colMap {
col := cols[colname]
query := elastic.NewMatchQuery(colname, strings.Join(keyword, " "))
if col.Boost != 0 {
query = query.Boost(float64(col.Boost))
var query elastic.Query
if col.IsTerm {
termVals := slice.Map(keyword, func(idx int, src string) any {
return src
})
query = elastic.NewTermsQuery(colname, termVals...).Boost(float64(col.Boost))
} else {
query = elastic.NewMatchQuery(colname, strings.Join(keyword, " ")).Boost(float64(col.Boost))
}
queries = append(queries, query)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/search/internal/repository/dao/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ func NewCaseElasticDAO(client *elastic.Client) *CaseElasticDAO {
Boost: caseTitleBoost,
},
"labels": {
Name: "labels",
Boost: caseLabelBoost,
Name: "labels",
Boost: caseLabelBoost,
IsTerm: true,
},
"keywords": {
Name: "keywords",
Expand Down
5 changes: 3 additions & 2 deletions internal/search/internal/repository/dao/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func NewQuestionDAO(client *elastic.Client) QuestionDAO {
Boost: questionTitleBoost,
},
"labels": {
Name: "labels",
Boost: questionLabelBoost,
Name: "labels",
Boost: questionLabelBoost,
IsTerm: true,
},
"content": {
Name: "content",
Expand Down
9 changes: 5 additions & 4 deletions internal/search/internal/repository/dao/skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

const (
SkillIndexName = "skill_index"
skillNameBoost = 15
skillLabelBoost = 3
skillNameBoost = 30
skillLabelBoost = 6
skillDescBoost = 2
)

Expand Down Expand Up @@ -65,8 +65,9 @@ func NewSkillElasticDAO(client *elastic.Client) SkillDAO {
Boost: skillNameBoost,
},
"labels": {
Name: "labels",
Boost: skillLabelBoost,
Name: "labels",
Boost: skillLabelBoost,
IsTerm: true,
},
"desc": {
Name: "desc",
Expand Down

0 comments on commit a14bd26

Please sign in to comment.