123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package services
- import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "github.com/elastic/go-elasticsearch/v7"
- "hongze/hongze_cygx/models"
- )
- func NewEsV7Client() (client *elasticsearch.Client, err error) {
- conf := elasticsearch.Config{
- Addresses: []string{ES_URL},
- Username: ES_USERNAME,
- Password: ES_PASSWORD,
- }
- client, err = elasticsearch.NewClient(conf)
- return
- }
- func EsV7Search(indexName, keyWord string) (result []*models.SearchItem, err error) {
-
- client, err := NewEsV7Client()
- if err != nil {
- fmt.Println("NewEsV7Client Err:", err.Error())
- return
- }
- var buf bytes.Buffer
- query := map[string]interface{}{
- "query": map[string]interface{}{
- "match_phrase": map[string]interface{}{
- "BodyText": map[string]interface{}{
- "query": "康立明",
- },
- },
- },
- "highlight": map[string]interface{}{
- "pre_tags": []string{"<font color='red'>"},
- "post_tags": []string{"</font>"},
- "fields": map[string]interface{}{
- "BodyText": map[string]interface{}{},
- },
- },
- }
- if err := json.NewEncoder(&buf).Encode(query); err != nil {
- fmt.Println(err, "Error encoding query")
- }
- res, err := client.Search(
- client.Search.WithIndex(indexName),
- client.Search.WithBody(&buf),
- client.Search.WithTrackTotalHits(true),
- client.Search.WithPretty(),
- client.Search.WithContext(context.Background()),
- )
- if err != nil {
- fmt.Println("search err:", err)
- }
- defer res.Body.Close()
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return
- }
|