123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package services
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/olivere/elastic/v7"
- "hongze/hongze_api/utils"
- )
- 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
- )
- func NewClient() (client *elastic.Client, err error) {
- client, err = elastic.NewClient(
- elastic.SetURL(ES_URL),
- elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
- elastic.SetSniff(false))
- return
- }
- // SearchESEnglishReport 查询es中的指标数据
- func SearchESEnglishReport(keyWord string, from, size int) (searchResp *elastic.SearchResult, total int64, err error) {
- indexName := utils.EsEnglishReportIndexName
- client, err := NewClient()
- if err != nil {
- return
- }
- var must []map[string]interface{}
- shouldSub := []map[string]interface{}{
- map[string]interface{}{
- "match": map[string]interface{}{
- "Title": map[string]interface{}{
- "query": keyWord,
- //"minimum_should_match": "60%",
- "boost": 3,
- },
- },
- },
- map[string]interface{}{
- "match": map[string]interface{}{
- "BodyContent": map[string]interface{}{
- "query": keyWord,
- //"minimum_should_match": "60%",
- "boost": 1,
- },
- },
- },
- map[string]interface{}{
- "match_phrase": map[string]interface{}{
- "Title": map[string]interface{}{
- "query": keyWord,
- //"slop": "50",
- "boost": 5,
- },
- },
- },
- map[string]interface{}{
- "match_phrase": map[string]interface{}{
- "BodyContent": map[string]interface{}{
- "query": keyWord,
- "boost": 3,
- },
- },
- },
- }
- mustMap := map[string]interface{}{
- "bool": map[string]interface{}{
- "should": shouldSub,
- },
- }
- must = append(must, mustMap)
- filterMust := []map[string]interface{}{
- map[string]interface{}{
- "term": map[string]interface{}{
- "PublishState": 2, //必须是已发布的报告
- },
- },
- }
- filterMap := map[string]interface{}{
- "bool": map[string]interface{}{
- "must": filterMust,
- },
- }
- source := map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": must,
- "filter": filterMap,
- },
- },
- }
- source["from"] = from
- source["size"] = size
- source["highlight"] = map[string]interface{}{
- "fields": map[string]interface{}{
- "Title":map[string]interface{}{},
- "BodyContent":map[string]interface{}{
- // "pre_tags" : "{{highlight}}",
- // "post_tags": "{{/highlight}}",
- },
- },
- "pre_tags" : "<span style=\"color:#E3B377\">",
- "post_tags": "</span>",
- }
- source["sort"] = []map[string]interface{}{
- map[string]interface{}{
- "PublishTime.keyword":map[string]interface{}{
- "order":"desc",
- },
- },
- map[string]interface{}{
- "_score":map[string]interface{}{
- "order":"desc",
- },
- },
- }
- jsonstr, err := json.Marshal(source)
- fmt.Printf("%s",jsonstr)
- request := client.Search(indexName).Source(source) // sets the JSON request
- //requestJson, err := json.Marshal(request)
- //if err != nil {
- // fmt.Println("requestJson err:", err)
- //}
- //fmt.Println("requestJson ", string(requestJson)
- searchResp, err = request.Do(context.Background())
- if err != nil {
- fmt.Print("结果err:")
- fmt.Println(err.Error())
- return
- }
- fmt.Print("结果正常:")
- fmt.Println(searchResp.Status)
- if searchResp.Status != 0 {
- err = errors.New("查询失败")
- }
- total = searchResp.TotalHits()
- return
- }
|