From 429ad157d63491d55a7138d1a6e80a60a71d0b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Thu, 30 Nov 2023 06:08:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=A3=80=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/graphql.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/api/graphql.go b/api/graphql.go index 45b2fb6..849f632 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -169,6 +169,7 @@ func NewSchema() (graphql.Schema, error) { "description": &graphql.ArgumentConfig{Type: graphql.String}, "tags": &graphql.ArgumentConfig{Type: graphql.String}, "rank": &graphql.ArgumentConfig{Type: graphql.String}, + "text": &graphql.ArgumentConfig{Type: graphql.String}, // 查找图像中的文字 "comment_num": &graphql.ArgumentConfig{Type: graphql.Int}, "praise_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 { 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 ") // 执行查询