翻页
This commit is contained in:
		@@ -5,6 +5,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
@@ -55,10 +56,49 @@ type SearchData struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 获取搜索结果的 ID 列表
 | 
			
		||||
func (sd SearchData) GetIDList() (id_list []string) {
 | 
			
		||||
func (sd SearchData) GetIDList(first, last, after, before int) (id_list []string) {
 | 
			
		||||
	for _, hit := range sd.Hits.Hits {
 | 
			
		||||
		id_list = append(id_list, hit.ID)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 如果 after 不为 0, 从这个ID开始向后取切片
 | 
			
		||||
	if after != 0 {
 | 
			
		||||
		after_str := fmt.Sprint(after)
 | 
			
		||||
		for i, id := range id_list {
 | 
			
		||||
			if id == after_str {
 | 
			
		||||
				id_list = id_list[i+1:]
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 如果 before 不为 0, 从这个ID开始向前取切片
 | 
			
		||||
	if before != 0 {
 | 
			
		||||
		before_str := fmt.Sprint(before)
 | 
			
		||||
		for i, id := range id_list {
 | 
			
		||||
			if id == before_str {
 | 
			
		||||
				id_list = id_list[:i]
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 如果 first 不为 0, 取切片的前 first 个元素
 | 
			
		||||
	if first != 0 {
 | 
			
		||||
		if first > len(id_list) {
 | 
			
		||||
			first = len(id_list)
 | 
			
		||||
		}
 | 
			
		||||
		id_list = id_list[:first]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 如果 last 不为 0, 取切片的后 last 个元素
 | 
			
		||||
	if last != 0 {
 | 
			
		||||
		if last > len(id_list) {
 | 
			
		||||
			last = len(id_list)
 | 
			
		||||
		}
 | 
			
		||||
		id_list = id_list[len(id_list)-last:]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return id_list
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -80,13 +120,14 @@ func ElasticsearchSearch(text string) (r SearchData) {
 | 
			
		||||
 | 
			
		||||
	es := elasticsearch_init()
 | 
			
		||||
 | 
			
		||||
	// 执行查询
 | 
			
		||||
	// 执行查询(最大返回200条)
 | 
			
		||||
	res, err := es.Search(
 | 
			
		||||
		es.Search.WithContext(context.Background()),
 | 
			
		||||
		es.Search.WithIndex("web_images"),
 | 
			
		||||
		es.Search.WithBody(&buf),
 | 
			
		||||
		es.Search.WithTrackTotalHits(true),
 | 
			
		||||
		es.Search.WithPretty(),
 | 
			
		||||
		es.Search.WithSize(200),
 | 
			
		||||
	)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("Error getting response: %s", err)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user