123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- 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{"<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()
- //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("<font color='red'>").PostTags("</font>")
- //
- //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
- }
|