95 lines
2.2 KiB
Go
Raw Normal View History

2024-11-14 00:39:42 +08:00
package model_handler
2024-11-14 04:26:12 +08:00
import (
"catface/app/global/variable"
"context"
"encoding/json"
"fmt"
"strings"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
2024-11-14 01:42:50 +08:00
/**
* @description: 用于处理 ES-highlight 模块分析出来的 []String.
* @param {[]interface{}} strs
2024-11-14 04:26:12 +08:00
* @return {*} []String 还原为 String这里就是简单的拼接了一下
2024-11-14 01:42:50 +08:00
*/
2024-11-14 00:39:42 +08:00
func TransStringSliceToString(strs []interface{}) string {
var result string
for _, str := range strs {
if s, ok := str.(string); ok {
result += s
}
}
return result
}
2024-11-14 04:26:12 +08:00
// func concatKeywordsToSlice(highlightKeyword string, oriKeywords []string) []string {
// return append(oriKeywords, highlightKeyword)
// }
func MergeSouceWithHighlight(hit map[string]interface{}) map[string]interface{} {
// 1. Get data
source := hit["_source"].(map[string]interface{})
highlight := hit["highlight"].(map[string]interface{})
// 2. Merge data
for k, v := range highlight {
if _, ok := source[k]; ok {
switch source[k].(type) {
case string:
source[k] = TransStringSliceToString(v.([]interface{}))
case []interface{}:
source[k+"_highlight"] = v // TODO 过滤,交给前端?
}
}
}
return source
}
/**
* @description: ES 发送的请求返回结果
* @param {string} body
* @param {string} index
* @return {*}
*/
func SearchRequest(body string, index string) ([]interface{}, error) {
ctx := context.Background()
req := esapi.SearchRequest{
Index: []string{index},
Body: strings.NewReader(body),
}
res, err := req.Do(ctx, variable.ElasticClient)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.IsError() {
var k map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&k); err != nil {
return nil, fmt.Errorf("error parsing the response body: %s", err)
} else {
return nil, fmt.Errorf("[%s] %s: %s",
res.Status(),
k["error"].(map[string]interface{})["type"],
k["error"].(map[string]interface{})["reason"],
)
}
}
var result map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil, err
}
hits, ok := result["hits"].(map[string]interface{})["hits"].([]interface{})
if !ok {
return nil, fmt.Errorf("error extracting hits from response")
}
return hits, nil
}