123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package elastic
- import (
- "context"
- "encoding/json"
- "errors"
- "eta/eta_mini_bridge/utils"
- "fmt"
- "github.com/olivere/elastic/v7"
- )
- // 首页搜索
- func SearchReport(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
- indexName := utils.EsReportIndexName
- var must []map[string]interface{}
- shouldSub := []map[string]interface{}{
- 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{}{
- // "Categories": map[string]interface{}{
- // "query": keyWord,
- // "boost": 4,
- // },
- // },
- // },
- // 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, //必须是已发布的报告
- },
- },
- // map[string]interface{}{
- // "terms": map[string]interface{}{
- // "ClassifyIdFirst": classifyIdFirsts, //分类必须是正常显示状态
- // },
- // },
- // map[string]interface{}{
- // "terms": map[string]interface{}{
- // "ClassifyIdSecond": classifyIdSeconds, //分类必须是正常显示状态
- // },
- // },
- }
- // filterMustNot := []map[string]interface{}{
- // map[string]interface{}{
- // "term": map[string]interface{}{
- // "BodyContent.keyword": map[string]interface{}{
- // "value": "", //过滤没有内容的报告(晨报和周报)bodyContent 不能为空
- // },
- // },
- // },
- // }
- filterMap := map[string]interface{}{
- "bool": map[string]interface{}{
- "must": filterMust,
- // "must_not": filterMustNot,
- },
- }
- source := map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": must,
- "filter": filterMap,
- },
- },
- }
- source["from"] = (pageIndex - 1) * pageSize
- source["size"] = pageSize
- source["highlight"] = map[string]interface{}{
- "fields": map[string]interface{}{
- "Title": map[string]interface{}{},
- // "Categories": map[string]interface{}{},
- // "BodyContent": map[string]interface{}{
- // // "pre_tags" : "{{highlight}}",
- // // "post_tags": "{{/highlight}}",
- // },
- },
- // "pre_tags": "<span style=\"color:#E3B377\">",
- "pre_tags": "<span class=\"report-title_color\">",
- "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 := utils.EsClient.Search(indexName).Source(source) // sets the JSON request
- // request := utils.EsClient.Search().Index(indice...).Source(source) // sets the JSON request
- fmt.Println(source)
- 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
- }
- func SearchReportV2(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
- indices := []string{utils.EsReportIndexName, utils.MINI_REPORT_INDEX_NAME}
- query := elastic.NewBoolQuery().
- Filter(
- elastic.NewBoolQuery().Should(
- elastic.NewTermQuery("PublishState", 2),
- elastic.NewTermQuery("State", 1),
- ),
- ).
- Must(
- elastic.NewBoolQuery().Should(
- elastic.NewMatchPhraseQuery("Title", keyWord).Boost(5),
- ),
- )
- sort1 := elastic.NewFieldSort("PublishTime.keyword").Desc()
- sort2 := elastic.NewScoreSort().Desc()
- searchResp, err = utils.EsClient.Search().
- Index(indices...).
- Query(query).
- SortBy(sort1, sort2).
- Highlight(elastic.NewHighlight().
- PreTags("<span class=\"report-title_color\">").
- PostTags("</span>").
- Field("Title")).
- From((pageIndex - 1) * pageSize).
- Size(pageSize).
- Pretty(true).
- Do(context.Background())
- // searchResp, err = request.Do(context.Background())
- if err != nil {
- fmt.Print("结果err:")
- fmt.Println(err.Error())
- return
- }
- total = searchResp.TotalHits()
- if searchResp.Status != 0 {
- err = errors.New("查询失败")
- }
- return
- }
- // ReportListSearch 报告列表页的搜索
- func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
- indexName := utils.EsReportIndexName
- var must []map[string]interface{}
- shouldSub := []map[string]interface{}{
- map[string]interface{}{
- "match": map[string]interface{}{
- "Title": map[string]interface{}{
- "query": keyWord,
- "boost": 3,
- },
- },
- },
- map[string]interface{}{
- "match": map[string]interface{}{
- "StageStr": map[string]interface{}{
- "query": keyWord,
- "boost": 2,
- },
- },
- },
- map[string]interface{}{
- "match": map[string]interface{}{
- "Abstract": map[string]interface{}{
- "query": keyWord,
- "boost": 2,
- },
- },
- },
- // map[string]interface{}{
- // "match": map[string]interface{}{
- // "ClassifyNameSecond": map[string]interface{}{
- // "query": keyWord,
- // "boost": 2,
- // },
- // },
- // },
- 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{}{
- "Abstract": map[string]interface{}{
- "query": keyWord,
- //"slop": "50",
- "boost": 5,
- },
- },
- },
- }
- mustMap := map[string]interface{}{
- "bool": map[string]interface{}{
- "should": shouldSub,
- },
- }
- must = append(must, mustMap)
- filter := []map[string]interface{}{
- map[string]interface{}{
- "term": map[string]interface{}{
- "PublishState": 2,
- },
- },
- map[string]interface{}{
- "term": map[string]interface{}{
- "ReportChapterId": 0, //排除章节内容
- },
- },
- // map[string]interface{}{
- // "term": map[string]interface{}{
- // "ClassifyIdFirst": classifyIdFirst,
- // },
- // },
- // map[string]interface{}{
- // "terms": map[string]interface{}{
- // "ClassifyIdSecond": classifyIdSeconds,
- // },
- // },
- }
- source := map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": must,
- "filter": filter,
- },
- },
- }
- // source["from"] = (pageIndex - 1) * pageSize
- // source["size"] = pageSize
- // source["highlight"] = map[string]interface{}{
- // "fields": map[string]interface{}{
- // "Title": map[string]interface{}{},
- // "Abstract": map[string]interface{}{},
- // "StageStr": map[string]interface{}{},
- // "ClassifyNameSecond": map[string]interface{}{},
- // },
- // "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 := utils.EsClient.Search(indexName).Source(source) // sets the JSON request
- 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
- }
|