嵌入文字搜索

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.