package elastic import ( "context" "encoding/json" "errors" "fmt" "github.com/olivere/elastic/v7" "hongze/hongze_yb/global" "hongze/hongze_yb/utils" ) // 首页搜索 func SearchReport(keyWord string, classifyIdList []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) { indexName := global.CONFIG.EsClient.Prefix + utils.ES_INDEX_RDDP_REPORT 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{}{ "Categories": map[string]interface{}{ "query": keyWord, //"minimum_should_match": "60%", "boost": 2, }, }, }, 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{}{ "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{}{ "ClassifyId": classifyIdList, //分类必须是正常显示状态 }, }, } 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": "", "post_tags": "", } 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 := global.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 } // ReportListSearch 报告列表页的搜索 func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) { indexName := global.CONFIG.EsClient.Prefix + utils.ES_INDEX_RDDP_REPORT 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": "", "post_tags": "", } 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 := global.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 }