嵌入文字搜索

This commit is contained in:
2023-11-20 04:48:55 +08:00
parent fdb3edfb61
commit b94b2d60c2
3 changed files with 125 additions and 121 deletions

View File

@@ -29,7 +29,7 @@ func elasticsearch_init() (es *elasticsearch.Client) {
return es
}
func ElasticsearchSearch(text string) interface{} {
func ElasticsearchSearch(text string) map[string]interface{} {
var (
r map[string]interface{}
)
@@ -44,7 +44,8 @@ func ElasticsearchSearch(text string) interface{} {
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
log.Printf("Error encoding query: %s", err)
return nil
}
es := elasticsearch_init()
@@ -52,24 +53,27 @@ func ElasticsearchSearch(text string) interface{} {
// Perform the search request.
res, err := es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex("news"),
es.Search.WithIndex("my_index"),
es.Search.WithBody(&buf),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
log.Printf("Error getting response: %s", err)
return nil
}
defer res.Body.Close()
// Check response status
if res.IsError() {
log.Fatalf("Error: %s", res.String())
log.Printf("Error: %s", res.String())
return nil
}
// Deserialize the response into a map.
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
log.Printf("Error parsing the response body: %s", err)
return nil
}
// Print the response status, number of results, and request duration.

View File

@@ -3,8 +3,11 @@ package models
import (
"database/sql"
"errors"
"fmt"
"log"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
@@ -13,6 +16,8 @@ type MysqlConnection struct {
Database *sql.DB
}
var connection *sql.DB
// 初始化数据库连接
func (m *MysqlConnection) Init() (err error) {
user := Viper.Get("mysql.user").(string)
@@ -21,11 +26,12 @@ func (m *MysqlConnection) Init() (err error) {
port := Viper.Get("mysql.port").(int)
database := Viper.Get("mysql.database").(string)
sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local"
m.Database, err = sql.Open("mysql", sqlconf) // 连接数据库
connection, err = sql.Open("mysql", sqlconf) // 连接数据库
if err != nil {
log.Println("连接数据库失败", err)
return
}
m.Database = connection
return
}
@@ -75,3 +81,61 @@ func (m *MysqlConnection) GetImages(page int, size int) (images []byte, err erro
images = append(images, []byte("]")...)
return
}
type User struct {
Id int `json:"id"`
UserName string `json:"user_name"`
Avatar string `json:"avatar"`
CreateTime time.Time `json:"create_time"`
UpdateTime time.Time `json:"update_time"`
}
// 获取一组用户信息
func QueryUserList(id_list []int) (users []User) {
count := len(id_list)
if count == 0 {
return
}
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
rows, err := connection.Query("SELECT id, user_name, avatar, update_time, create_time FROM web_member WHERE id IN (" + idstr + ") LIMIT " + strconv.Itoa(count))
if err != nil {
log.Println("获取用户列表失败", err)
return
}
defer rows.Close()
for rows.Next() {
var user User
rows.Scan(&user.Id, &user.UserName, &user.Avatar, &user.UpdateTime, &user.CreateTime)
users = append(users, user)
}
return
}
type Article struct {
Id int `json:"id"`
Title string `json:"title"`
Tags string `json:"tags"`
CreateTime time.Time `json:"create_time"`
UpdateTime time.Time `json:"update_time"`
}
// 获取一组文章信息
func QueryArticleList(id_list []int) (articles []Article) {
count := len(id_list)
if count == 0 {
return
}
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
rows, err := connection.Query("SELECT id, title, tags, update_time, create_time FROM web_article WHERE id IN (" + idstr + ")")
if err != nil {
log.Println("获取文章列表失败", err)
return
}
defer rows.Close()
for rows.Next() {
var article Article
rows.Scan(&article.Id, &article.Title, &article.Tags, &article.UpdateTime, &article.CreateTime)
articles = append(articles, article)
}
return
}