消除错误处理
This commit is contained in:
@@ -56,6 +56,13 @@ type SearchData struct {
|
||||
took int
|
||||
}
|
||||
|
||||
func (sd *SearchData) GetIDList() (id_list []string) {
|
||||
for _, hit := range sd.Hits.Hits {
|
||||
id_list = append(id_list, hit.ID)
|
||||
}
|
||||
return id_list
|
||||
}
|
||||
|
||||
func ElasticsearchSearch(text string) (r *SearchData) {
|
||||
// 通过字符串构建查询
|
||||
var buf bytes.Buffer
|
||||
@@ -68,7 +75,7 @@ func ElasticsearchSearch(text string) (r *SearchData) {
|
||||
}
|
||||
if err := json.NewEncoder(&buf).Encode(query); err != nil {
|
||||
log.Printf("Error encoding query: %s", err)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
es := elasticsearch_init()
|
||||
@@ -83,20 +90,20 @@ func ElasticsearchSearch(text string) (r *SearchData) {
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Error getting response: %s", err)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
// 处理错误
|
||||
if res.IsError() {
|
||||
log.Printf("Error: %s", res.String())
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// 转换返回结果
|
||||
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
|
||||
log.Printf("Error parsing the response body: %s", err)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
return r
|
||||
|
Reference in New Issue
Block a user