diff --git a/api/consts/option.go b/api/consts/option.go index 115c3a69..07d5366f 100644 --- a/api/consts/option.go +++ b/api/consts/option.go @@ -55,3 +55,6 @@ const ( ZoneMomentPriorityInZone_Increment = 2000000000 ZoneMomentPriorityInZone_Decrement = -2000000000 ) +const ( + StreamerAcct_MatchPhraseLen = 3 +) diff --git a/app/mix/dao/elasticsearch.go b/app/mix/dao/elasticsearch.go index eeb51bf1..99c54100 100644 --- a/app/mix/dao/elasticsearch.go +++ b/app/mix/dao/elasticsearch.go @@ -9,6 +9,7 @@ import ( "service/library/elasticsearchdb" "service/library/logger" + "service/api/consts" streameracctproto "service/api/proto/streamer_acct/proto" "github.com/gin-gonic/gin" @@ -70,9 +71,15 @@ func (es *ElasticSearch) GetStreamerAcctListFuzzilyByName(ctx *gin.Context, req list = make([]*dbstruct.EsStreamerAcct, 0) - delFlagClause := elastic.NewMatchQuery("del_flag", 0) - nameClause := elastic.NewMatchQuery("name", req.Name) + var nameClause elastic.Query + // 查询文字长度小于短语匹配的,进行Term匹配,否则进行短语匹配 + if len(req.Name) < consts.StreamerAcct_MatchPhraseLen { + nameClause = elastic.NewTermQuery("name", req.Name) + } else { + nameClause = elastic.NewMatchPhraseQuery("name", req.Name) + } query := elastic.NewBoolQuery() + delFlagClause := elastic.NewMatchQuery("del_flag", 0) query.Must(nameClause, delFlagClause) res, err := es.clientMix.Search(es.getIndexStreamerAcct()).Query(query).From(req.Offset).Size(req.Limit).Sort("ct", true).Do(ctx) @@ -98,11 +105,19 @@ func (es *ElasticSearch) GetStreamerAcctListFuzzilyByUserId(ctx *gin.Context, re list = make([]*dbstruct.EsStreamerAcct, 0) - delFlagClause := elastic.NewMatchQuery("del_flag", 0) - nameClause := elastic.NewMatchQuery("name", req.UserIdString) - userIdStringClause := elastic.NewMatchQuery("user_id_string", req.UserIdString) + var nameClause elastic.Query + var userIdStringClause elastic.Query + // 查询文字长度小于短语匹配的,进行Term匹配,否则进行短语匹配 + if len(req.UserIdString) < consts.StreamerAcct_MatchPhraseLen { + nameClause = elastic.NewTermQuery("name", req.UserIdString) + userIdStringClause = elastic.NewTermQuery("user_id_string", req.UserIdString) + } else { + nameClause = elastic.NewMatchQuery("name", req.UserIdString) + userIdStringClause = elastic.NewMatchPhraseQuery("user_id_string", req.UserIdString) + } orClause := elastic.NewBoolQuery().Should(nameClause, userIdStringClause) query := elastic.NewBoolQuery().Must() + delFlagClause := elastic.NewMatchQuery("del_flag", 0) query.Must(orClause, delFlagClause) res, err := es.clientMix.Search(es.getIndexStreamerAcct()).Query(query).From(req.Offset).Size(req.Limit).Sort("ct", true).Do(ctx)