加入检索

This commit is contained in:
2023-11-30 06:08:33 +08:00
parent 7f87a728ae
commit 429ad157d6

View File

@@ -169,6 +169,7 @@ func NewSchema() (graphql.Schema, error) {
"description": &graphql.ArgumentConfig{Type: graphql.String}, "description": &graphql.ArgumentConfig{Type: graphql.String},
"tags": &graphql.ArgumentConfig{Type: graphql.String}, "tags": &graphql.ArgumentConfig{Type: graphql.String},
"rank": &graphql.ArgumentConfig{Type: graphql.String}, "rank": &graphql.ArgumentConfig{Type: graphql.String},
"text": &graphql.ArgumentConfig{Type: graphql.String}, // 查找图像中的文字
"comment_num": &graphql.ArgumentConfig{Type: graphql.Int}, "comment_num": &graphql.ArgumentConfig{Type: graphql.Int},
"praise_count": &graphql.ArgumentConfig{Type: graphql.Int}, "praise_count": &graphql.ArgumentConfig{Type: graphql.Int},
"collect_count": &graphql.ArgumentConfig{Type: graphql.Int}, "collect_count": &graphql.ArgumentConfig{Type: graphql.Int},
@@ -226,6 +227,67 @@ func NewSchema() (graphql.Schema, error) {
if p.Args["content"] != nil { if p.Args["content"] != nil {
where = append(where, fmt.Sprintf("content='%s'", p.Args["content"])) where = append(where, fmt.Sprintf("content='%s'", p.Args["content"]))
} }
if p.Args["remark"] != nil {
where = append(where, fmt.Sprintf("remark='%s'", p.Args["remark"]))
}
if p.Args["description"] != nil {
where = append(where, fmt.Sprintf("description='%s'", p.Args["description"]))
}
if p.Args["tags"] != nil {
where = append(where, fmt.Sprintf("tags='%s'", p.Args["tags"]))
}
if p.Args["rank"] != nil {
where = append(where, fmt.Sprintf("rank='%s'", p.Args["rank"]))
}
if p.Args["comment_num"] != nil {
where = append(where, fmt.Sprintf("comment_num=%d", p.Args["comment_num"]))
}
if p.Args["praise_count"] != nil {
where = append(where, fmt.Sprintf("praise_count=%d", p.Args["praise_count"]))
}
if p.Args["collect_count"] != nil {
where = append(where, fmt.Sprintf("collect_count=%d", p.Args["collect_count"]))
}
if p.Args["article_id"] != nil {
where = append(where, fmt.Sprintf("article_id=%d", p.Args["article_id"]))
}
if p.Args["user_id"] != nil {
where = append(where, fmt.Sprintf("user_id=%d", p.Args["user_id"]))
}
if p.Args["create_time"] != nil {
where = append(where, fmt.Sprintf("create_time='%s'", p.Args["create_time"]))
}
if p.Args["update_time"] != nil {
where = append(where, fmt.Sprintf("update_time='%s'", p.Args["update_time"]))
}
if p.Args["text"] != nil {
// 通过字符串构建查询
var buf strings.Builder
query := map[string]interface{}{
"query": map[string]interface{}{
"match": map[string]interface{}{
"content": p.Args["text"],
},
},
}
buf.WriteString(fmt.Sprintf("%v", query))
// 执行查询
var sd *models.SearchData
sd = models.ElasticsearchSearch(buf.String())
// 获取搜索结果的 ID 列表
id_list := sd.GetIDList()
// 合并为以逗号分隔的字符串
id_list_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(id_list)), ","), "[]")
// 搜索结果为空
if id_list_str == "" {
return map[string]interface{}{
"list": []Image{},
"next": false,
"total": 0,
}, nil
}
where = append(where, fmt.Sprintf("id IN (%s)", id_list_str))
}
where_str := strings.Join(where, " AND ") where_str := strings.Join(where, " AND ")
// 执行查询 // 执行查询