Update image search and pagination logic
This commit is contained in:
		
							
								
								
									
										51
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -164,7 +164,7 @@ func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64)
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 用向量查询相似图片
 | 
						// 用向量查询相似图片
 | 
				
			||||||
	topk := 1000
 | 
						topk := 200
 | 
				
			||||||
	sp, _ := entity.NewIndexIvfFlatSearchParam(64)
 | 
						sp, _ := entity.NewIndexIvfFlatSearchParam(64)
 | 
				
			||||||
	vectors := []entity.Vector{entity.FloatVector(embedding)}
 | 
						vectors := []entity.Vector{entity.FloatVector(embedding)}
 | 
				
			||||||
	resultx, err := milvusConnection.Client.Search(ctx, collection_name, nil, "", []string{"id", "article_id"}, vectors, "embedding", entity.L2, topk, sp)
 | 
						resultx, err := milvusConnection.Client.Search(ctx, collection_name, nil, "", []string{"id", "article_id"}, vectors, "embedding", entity.L2, topk, sp)
 | 
				
			||||||
@@ -278,7 +278,7 @@ func main() {
 | 
				
			|||||||
		// 排序
 | 
							// 排序
 | 
				
			||||||
		// 分页
 | 
							// 分页
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取查询条件(忽略空值), 超级简洁写法
 | 
							// 获取查询条件(忽略空值)
 | 
				
			||||||
		QueryConditions := func(key string) (list []string) {
 | 
							QueryConditions := func(key string) (list []string) {
 | 
				
			||||||
			for _, item := range strings.Split(r.URL.Query().Get(key), ",") {
 | 
								for _, item := range strings.Split(r.URL.Query().Get(key), ",") {
 | 
				
			||||||
				if item != "" {
 | 
									if item != "" {
 | 
				
			||||||
@@ -288,7 +288,7 @@ func main() {
 | 
				
			|||||||
			return list
 | 
								return list
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 拼接查询条件, 超级简洁写法
 | 
							// 拼接查询条件
 | 
				
			||||||
		var addCondition = func(conditions *strings.Builder, key, column string) {
 | 
							var addCondition = func(conditions *strings.Builder, key, column string) {
 | 
				
			||||||
			if values := QueryConditions(key); len(values) > 0 {
 | 
								if values := QueryConditions(key); len(values) > 0 {
 | 
				
			||||||
				if conditions.Len() > 0 {
 | 
									if conditions.Len() > 0 {
 | 
				
			||||||
@@ -305,6 +305,11 @@ func main() {
 | 
				
			|||||||
		addCondition(&conditions, "categories", "categorie")
 | 
							addCondition(&conditions, "categories", "categorie")
 | 
				
			||||||
		addCondition(&conditions, "sets", "sets")
 | 
							addCondition(&conditions, "sets", "sets")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 获取图片列表
 | 
				
			||||||
 | 
							var images ListView
 | 
				
			||||||
 | 
							var image_list []Image
 | 
				
			||||||
 | 
							images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 20)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var ids []int64
 | 
							var ids []int64
 | 
				
			||||||
		if similar := QueryConditions("similar"); len(similar) > 0 {
 | 
							if similar := QueryConditions("similar"); len(similar) > 0 {
 | 
				
			||||||
			id, err := strconv.Atoi(strings.Trim(similar[0], "'"))
 | 
								id, err := strconv.Atoi(strings.Trim(similar[0], "'"))
 | 
				
			||||||
@@ -314,6 +319,15 @@ func main() {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			// 如果指定以某个图片为基准的相似图片列表范围, 获取相似图片ID的列表
 | 
								// 如果指定以某个图片为基准的相似图片列表范围, 获取相似图片ID的列表
 | 
				
			||||||
			ids = (&Image{Id: id}).GetSimilarImagesIdList("default")
 | 
								ids = (&Image{Id: id}).GetSimilarImagesIdList("default")
 | 
				
			||||||
 | 
								images.Total = len(ids)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 按照分页取相应的图片ID
 | 
				
			||||||
 | 
								if len(ids) > images.Page*images.PageSize {
 | 
				
			||||||
 | 
									ids = ids[(images.Page-1)*images.PageSize : images.Page*images.PageSize]
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									ids = ids[(images.Page-1)*images.PageSize:]
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			idsStr := make([]string, len(ids))
 | 
								idsStr := make([]string, len(ids))
 | 
				
			||||||
			for i, v := range ids {
 | 
								for i, v := range ids {
 | 
				
			||||||
				idsStr[i] = strconv.FormatInt(v, 10)
 | 
									idsStr[i] = strconv.FormatInt(v, 10)
 | 
				
			||||||
@@ -328,11 +342,8 @@ func main() {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取图片列表
 | 
							// 开始查询 +" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize
 | 
				
			||||||
		var images ListView
 | 
							rows, err := mysqlConnection.Database.Query(fmt.Sprintf("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images %s", conditions.String()))
 | 
				
			||||||
		var image_list []Image
 | 
					 | 
				
			||||||
		images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 20)
 | 
					 | 
				
			||||||
		rows, err := mysqlConnection.Database.Query("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images"+conditions.String()+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Println("获取图片列表失败", err)
 | 
								log.Println("获取图片列表失败", err)
 | 
				
			||||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
								http.Error(w, err.Error(), http.StatusBadRequest)
 | 
				
			||||||
@@ -439,14 +450,26 @@ func main() {
 | 
				
			|||||||
			images.List[i] = v
 | 
								images.List[i] = v
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取总数
 | 
							if similar := QueryConditions("similar"); len(similar) > 0 {
 | 
				
			||||||
		err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions.String()).Scan(&images.Total)
 | 
								// 总数不变
 | 
				
			||||||
		if err != nil {
 | 
							} else {
 | 
				
			||||||
			log.Println("获取图片总数失败", err)
 | 
								// 获取总数
 | 
				
			||||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
								err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions.String()).Scan(&images.Total)
 | 
				
			||||||
			return
 | 
								if err != nil {
 | 
				
			||||||
 | 
									log.Println("获取图片总数失败", err)
 | 
				
			||||||
 | 
									http.Error(w, err.Error(), http.StatusBadRequest)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//// 获取总数
 | 
				
			||||||
 | 
							//err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions.String()).Scan(&images.Total)
 | 
				
			||||||
 | 
							//if err != nil {
 | 
				
			||||||
 | 
							//	log.Println("获取图片总数失败", err)
 | 
				
			||||||
 | 
							//	http.Error(w, err.Error(), http.StatusBadRequest)
 | 
				
			||||||
 | 
							//	return
 | 
				
			||||||
 | 
							//}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 是否有下一页
 | 
							// 是否有下一页
 | 
				
			||||||
		images.Next = images.Total > images.Page*images.PageSize
 | 
							images.Next = images.Total > images.Page*images.PageSize
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user