123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package services
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/olivere/elastic/v7"
- )
- const (
- ES_URL = "http://es-cn-nif227b580019rgw6.public.elasticsearch.aliyuncs.com:9200" //<1>
- ES_USERNAME = "elastic" //<2>
- ES_PASSWORD = "hongze@2021" //<3>
- //Grafana pwd-> 20521bb9
- //Grafana username-> emon
- )
- type tracelog struct{}
- // 实现输出
- func (tracelog) Printf(format string, v ...interface{}) {
- fmt.Printf(format, v...)
- }
- func RemoveDuplicatesAndEmpty(a []string) (ret []string) {
- a_len := len(a)
- for i := 0; i < a_len; i++ {
- if (i > 0 && a[i-1] == a[i]) || len(a[i]) == 0 {
- continue
- }
- ret = append(ret, a[i])
- }
- return
- }
- func init21123() {
- fmt.Println("start")
- client, err := elastic.NewClient(elastic.SetURL(ES_URL), elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD), elastic.SetSniff(false))
- if err != nil {
- fmt.Println("err:", err)
- }
- fmt.Println(client)
- keyWordStr := "医疗器械"
- queryString := elastic.NewQueryStringQuery(`Title:(` + keyWordStr + `)`)
- boolqueryJson, err := json.Marshal(queryString)
- fmt.Println("err:", err)
- fmt.Println("queryString ", string(boolqueryJson))
- var esIndex = "cygx_article"
- //boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(esIndex).Highlight(highlight).Query(queryString)
- requestJson, err := json.Marshal(request)
- fmt.Println("err:", err)
- fmt.Println("requestJson ", string(requestJson))
- searchByMatch, err := request.Do(context.Background())
- if searchByMatch.Hits != nil {
- }
- fmt.Println(searchByMatch)
- fmt.Println("end")
- }
|