diff --git a/internal/search/internal/integration/handler_test.go b/internal/search/internal/integration/handler_test.go index b14c427..a32c944 100644 --- a/internal/search/internal/integration/handler_test.go +++ b/internal/search/internal/integration/handler_test.go @@ -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, @@ -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 = "" @@ -1324,6 +1340,7 @@ func (s *HandlerTestSuite) TestSearchLimit() { Name: "", Desc: "test_desc", }, + { ID: 4, Labels: []string{"programming"}, @@ -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)) +} diff --git a/internal/search/internal/repository/dao/builder.go b/internal/search/internal/repository/dao/builder.go index 0e1f211..5161728 100644 --- a/internal/search/internal/repository/dao/builder.go +++ b/internal/search/internal/repository/dao/builder.go @@ -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" ) @@ -13,6 +15,8 @@ type Col struct { Name string // 权重 Boost int + // 是否是精确匹配 + IsTerm bool } func buildCols(cols map[string]Col, queryMetas []domain.QueryMeta) []elastic.Query { @@ -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) } diff --git a/internal/search/internal/repository/dao/case.go b/internal/search/internal/repository/dao/case.go index c4cc0c5..99ef1af 100644 --- a/internal/search/internal/repository/dao/case.go +++ b/internal/search/internal/repository/dao/case.go @@ -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", diff --git a/internal/search/internal/repository/dao/question.go b/internal/search/internal/repository/dao/question.go index f2e6dc3..6346d1c 100644 --- a/internal/search/internal/repository/dao/question.go +++ b/internal/search/internal/repository/dao/question.go @@ -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", diff --git a/internal/search/internal/repository/dao/skill.go b/internal/search/internal/repository/dao/skill.go index d98dfdf..73d7aec 100644 --- a/internal/search/internal/repository/dao/skill.go +++ b/internal/search/internal/repository/dao/skill.go @@ -25,8 +25,8 @@ import ( const ( SkillIndexName = "skill_index" - skillNameBoost = 15 - skillLabelBoost = 3 + skillNameBoost = 30 + skillLabelBoost = 6 skillDescBoost = 2 ) @@ -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",