logo search

This commit is contained in:
eson 2023-10-25 10:15:15 +08:00
parent a3197b7d55
commit fe5ec165f2

View File

@ -27,10 +27,16 @@ type PreLogoSearchResult struct {
// 搜索 // 搜索
func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode string, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) { func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode string, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywords := regexp.MustCompile(`\s+`).Split(keywordsStr, -1) keywordsList := regexp.MustCompile(`\s+`).Split(keywordsStr, -1)
for i, v := range keywords { var keywords []string
keywords[i] = "+" + v + "*" for _, v := range keywordsList {
if len(v) > 1 {
keywords = append(keywords, "+"+v+"*")
} }
}
if len(keywords) != 0 {
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) and zip_code = ? limit %d;", count) sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) and zip_code = ? limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "), zipcode) tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "), zipcode)
@ -44,8 +50,12 @@ func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode strin
if resp[i].RestaurantType == nil { if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("") resp[i].RestaurantType = FsString("")
} }
if len(*resp[i].ResourceUrl) < 10 {
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))]) resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
} }
}
}
if resp == nil { if resp == nil {
resp = make([]PreLogoSearchResult, 0) resp = make([]PreLogoSearchResult, 0)
@ -56,10 +66,15 @@ func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode strin
// 搜索建议 // 搜索建议
func (p *FsPreprocessLogoModel) PreLogoSearchSuggestions(ctx context.Context, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) { func (p *FsPreprocessLogoModel) PreLogoSearchSuggestions(ctx context.Context, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywords := regexp.MustCompile(`\s+`).Split(keywordsStr, -1) keywordsList := regexp.MustCompile(`\s+`).Split(keywordsStr, -1)
for i, v := range keywords { var keywords []string
keywords[i] = "+" + v + "*" for _, v := range keywordsList {
if len(v) > 1 {
keywords = append(keywords, "+"+v+"*")
} }
}
if len(keywords) != 0 {
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) limit %d;", count) sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " ")) tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "))
@ -73,9 +88,15 @@ func (p *FsPreprocessLogoModel) PreLogoSearchSuggestions(ctx context.Context, ke
if resp[i].RestaurantType == nil { if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("") resp[i].RestaurantType = FsString("")
} }
if len(*resp[i].ResourceUrl) < 10 {
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))]) resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
} }
}
}
if resp == nil { if resp == nil {
resp = make([]PreLogoSearchResult, 0) resp = make([]PreLogoSearchResult, 0)
} }