package services import ( "bytes" "context" "encoding/json" "fmt" "github.com/elastic/go-elasticsearch/v7" "hongze/hongze_cygx/models" ) /* Addresses []string // A list of Elasticsearch nodes to use. Logger estransport.Logger // The logger object. Selector estransport.Selector // The selector object. */ //官方库 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) { //pageSize := 20 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{""}, "post_tags": []string{""}, "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() //boolQuery := elastic.NewBoolQuery() // //matchArr := make([]elastic.Query, 0) ////matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart") ////matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart") //matchq1 := elastic.NewMatchPhraseQuery("Title", keyWord) //.Analyzer("ik_smart") //matchq2 := elastic.NewMatchPhraseQuery("BodyText", keyWord) //matchArr = append(matchArr, matchq1) //matchArr = append(matchArr, matchq2) ////matchArr = append(matchArr, matchq2) ////for _, v := range keyWordArr { //// if v != "" { //// matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart") //// matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart") //// matchArr = append(matchArr, matchq1) //// matchArr = append(matchArr, matchq2) //// } //// n-- ////} ////boolquery.Should(matchArr...) // //boolQuery.Should(matchArr...) // //highlight := elastic.NewHighlight() //highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText")) //highlight = highlight.PreTags("").PostTags("") // //service := client.Search().Index(indexName).Highlight(highlight).Query(boolQuery).Size(pageSize) //resp, err := service.Do(context.Background()) //if err != nil { // return nil, err //} // //if resp.TotalHits() == 0 { // return nil, nil //} //for _, e := range resp.Each(reflect.TypeOf(&models.SearchItem{})) { // us := e.(*models.SearchItem) // result = append(result, us) //} //return result, nil return }