-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e81b20
commit 6726879
Showing
16 changed files
with
488 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package domain | ||
|
||
// 定义的查询元数据 | ||
type QueryMeta struct { | ||
// 查询的内容 | ||
Keyword string | ||
// 是否是全量字段的 | ||
IsAll bool | ||
// 需要查询的关键字 | ||
Col string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dao | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/ecodeclub/webook/internal/search/internal/domain" | ||
"github.com/olivere/elastic/v7" | ||
) | ||
|
||
// 用于构建查询 | ||
type Col struct { | ||
// 列名 | ||
Name string | ||
// 权重 | ||
Boost int | ||
} | ||
|
||
func buildCols(cols map[string]Col, queryMetas []domain.QueryMeta) []elastic.Query { | ||
colMap := make(map[string][]string, len(cols)) | ||
for _, meta := range queryMetas { | ||
if meta.IsAll { | ||
for _, col := range cols { | ||
colMap = setCol(colMap, col.Name, meta.Keyword) | ||
} | ||
} else { | ||
colMap = setCol(colMap, meta.Col, meta.Keyword) | ||
} | ||
} | ||
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)) | ||
} | ||
queries = append(queries, query) | ||
} | ||
return queries | ||
} | ||
|
||
func setCol(colMap map[string][]string, col, keyword string) map[string][]string { | ||
ks, ok := colMap[col] | ||
if ok { | ||
ks = append(ks, keyword) | ||
colMap[col] = ks | ||
} else { | ||
colMap[col] = []string{ | ||
keyword, | ||
} | ||
} | ||
return colMap | ||
} |
Oops, something went wrong.