123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380 |
- package services
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/olivere/elastic/v7"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/utils"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- func NewClient() (client *elastic.Client, err error) {
- //errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
- //file := ""
- //if utils.RunMode == "release" {
- // //file = `/data/rdlucklog/hongze_cygx/eslog.log`
- // file = `./rdlucklog/eslog.log`
- //} else {
- // file = `./rdlucklog/eslog.log`
- //}
- //logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
- //client, err = elastic.NewClient(
- // elastic.SetURL(ES_URL),
- // elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
- // elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
- // elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
- client, err = elastic.NewClient(
- elastic.SetURL(ES_URL),
- elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
- elastic.SetSniff(false))
- return
- }
- //创建文章阅读记录的Es索引
- func CreateIndexNameArticleHistory() {
- indexName := utils.IndexNameArticleHistory
- mappingJson := `{
- "mappings": {
- "dynamic": true,
- "properties": {
- "ArticleId": {
- "type": "integer"
- },
- "Id": {
- "type": "integer"
- },
- "ArticleType": {
- "type": "short"
- },
- "CompanyArticleHistoryNum": {
- "type": "integer"
- },
- "CompanyName": {
- "type": "keyword"
- },
- "CompanyId": {
- "type": "integer"
- },
- "CreateTime": {
- "type": "date",
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
- },
- "Email": {
- "type": "keyword"
- },
- "Mobile": {
- "type": "keyword"
- },
- "PublishDate": {
- "type": "date",
- "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
- },
- "RealName": {
- "type": "keyword"
- },
- "SellerName": {
- "type": "keyword"
- },
- "SellerId": {
- "type": "integer"
- },
- "StopTime": {
- "type": "integer"
- },
- "Title": {
- "type": "keyword"
- },
- "UserId": {
- "type": "integer"
- },
- "UserArticleHistoryNum": {
- "type": "integer"
- }
- }
- }
- }`
- EsCreateIndex(indexName, mappingJson)
- }
- //func UpdateWxUserLabel(cont context.Context) (err error) {
- func AddAllArticleHistory(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("同步阅读记录到es失败;Err:"+err.Error(), 2)
- go utils.SendEmail("同步阅读记录到es失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), ";Err:"+err.Error(), utils.EmailSendToUsers)
- utils.FileLog.Info("同步阅读记录到es失败,Err:%s", err.Error())
- }
- }()
- var updateUserIds string //更改过的用户ID
- userIdMap := make(map[int]int)
- condition := ` AND r.create_time < '` + time.Now().Format(utils.FormatDate) + `' AND r.company_id IN (
- SELECT a.company_id
- FROM company AS a INNER JOIN company_product AS b ON a.company_id = b.company_id
- WHERE a.enabled = 1 AND b.STATUS IN ( '正式', '试用', '冻结' )) `
- totalAll, err := models.GetCygxArticleHistoryCountByCompany(condition)
- if err != nil {
- fmt.Println("GetCygxArticleHistoryCountByCompany Err:totalAll", err.Error())
- return err
- }
- //更改阅读总数
- err = models.UpdateConfigByCode(strconv.Itoa(totalAll), "company_article_history_num")
- if err != nil {
- fmt.Println("UpdateConfigByCode Err:totalAll", err.Error())
- return err
- }
- //处理前一天新增的数据
- condition += ` AND r.create_time >='` + time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + `'`
- total, err := models.GetCygxArticleHistoryCountByCompany(condition)
- //fmt.Println(total)
- //return
- fmt.Println(total)
- if err != nil {
- fmt.Println("GetCygxArticleHistoryCountByCompany Err:", err.Error())
- return err
- }
- for i := 0; i <= total/1000; i++ {
- allList, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 1000*i, 1000)
- if err != nil {
- fmt.Println("GetCygxArticleHistoryRecordByCompanyList Err:", err.Error())
- return err
- }
- for k, v := range allList {
- fmt.Println(v.Id, "___", k)
- err := EsAddArticleHistoryData(v)
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- return err
- }
- if _, ok := userIdMap[v.UserId]; !ok {
- updateUserIds += strconv.Itoa(v.UserId) + ","
- userIdMap[v.UserId] = v.UserId
- }
- }
- }
- //处理前一天新增的数据 end
- //处理前一天被移动的用户
- startDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
- endDate := time.Now().Format(utils.FormatDate)
- var mobiles string
- listUpdateUser, err := models.GetWxUserOpLogList(startDate, endDate)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return err
- }
- if len(listUpdateUser) > 0 {
- for _, v := range listUpdateUser {
- mobiles += "'" + v.Mobile + "',"
- }
- }
- mobiles = strings.TrimRight(mobiles, ",")
- if mobiles != "" {
- condition = ` AND r.mobile IN (` + mobiles + `)`
- //修改用户的阅读记录(es 自动判断,如果有他会修改数据)
- listUpdatePv, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 0, 0)
- if err != nil {
- fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
- return err
- }
- for _, v := range listUpdatePv {
- err := EsAddArticleHistoryData(v)
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- return err
- }
- }
- }
- //处理前一天被移动的用户 end
- //处理前一天被删除的用户
- {
- listDeleteUser, err := models.GetWxUserOpLogDeleteList(startDate, endDate)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return err
- }
- mobiles = ""
- if len(listUpdateUser) > 0 {
- for _, v := range listDeleteUser {
- mobiles += "'" + v.Mobile + "',"
- }
- }
- mobiles = strings.TrimRight(mobiles, ",")
- if mobiles != "" {
- condition = ` AND r.mobile IN (` + mobiles + `)`
- listDeletePv, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 0, 0)
- if err != nil {
- fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
- return err
- }
- //fmt.Println("Es 删除")
- for _, v := range listDeletePv {
- err := EsDeleteData(utils.IndexNameArticleHistory, strconv.Itoa(v.Id))
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- return err
- }
- }
- }
- }
- //处理前一天被删除的用户 end
- //处理新增的阅读记录的用户阅读数量、机构阅读数量(暂未找到批量修改的方法,后期优化处理 2022.7.11)
- updateUserIds = strings.TrimRight(updateUserIds, ",")
- if updateUserIds != "" {
- condition = ` AND r.create_time >='` + time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + `' AND r.user_id IN (` + updateUserIds + `)`
- total, err := models.GetCygxArticleHistoryCountByCompany(condition)
- //fmt.Println(total)
- //return
- fmt.Println(total)
- if err != nil {
- fmt.Println("GetCygxArticleHistoryCountByCompany Err:", err.Error())
- return err
- }
- for i := 0; i <= total/1000; i++ {
- allList, err := models.GetCygxArticleHistoryRecordByCompanyList(condition, 1000*i, 1000)
- if err != nil {
- fmt.Println("GetCygxArticleHistoryRecordByCompanyList Err:", err.Error())
- return err
- }
- for k, v := range allList {
- fmt.Println(v.Id, "___", k)
- err := EsAddArticleHistoryData(v)
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- return err
- }
- if _, ok := userIdMap[v.UserId]; !ok {
- updateUserIds += strconv.Itoa(v.UserId) + ","
- userIdMap[v.UserId] = v.UserId
- }
- }
- }
- }
- return
- }
- //新增数据
- func EsAddArticleHistoryData(item *models.EsUserInteraction) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- }
- }()
- client := utils.Client
- resp, err := client.Index().Index(utils.IndexNameArticleHistory).Id(strconv.Itoa(item.Id)).BodyJson(item).Do(context.Background())
- if err != nil {
- fmt.Println("新增失败:", err.Error())
- return err
- }
- if resp.Status == 0 && resp.Result == "created" {
- //fmt.Println("新增成功")
- err = nil
- return err
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- return
- }
- //indexName:索引名称
- //mappingJson:表结构
- func EsCreateIndex(indexName, mappingJson string) (err error) {
- client := utils.Client
- //if err != nil {
- // return
- //}
- //定义表结构
- exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
- if err != nil {
- return
- }
- if !exists {
- resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
- //BodyJson(bodyJson).Do(context.Background())
- if err != nil {
- fmt.Println("CreateIndex Err:" + err.Error())
- return err
- }
- fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
- } else {
- fmt.Println(indexName + " 已存在")
- }
- return
- }
- //新增和修改数据
- func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- }
- }()
- client := utils.Client
- searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
- if err != nil && !strings.Contains(err.Error(), "404") {
- fmt.Println("Get Err" + err.Error())
- return
- }
- if searchById != nil && searchById.Found {
- resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
- "BodyText": item.BodyText,
- "Title": item.Title,
- "PublishDate": item.PublishDate,
- "CategoryId": item.CategoryId,
- "ExpertBackground": item.ExpertBackground,
- }).Do(context.Background())
- if err != nil {
- return err
- }
- if resp.Status == 0 {
- fmt.Println("修改成功")
- } else {
- fmt.Println("EditData", resp.Status, resp.Result)
- }
- client.CloseIndex(indexName)
- } else {
- resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
- if err != nil {
- fmt.Println("新增失败:", err.Error())
- return err
- }
- if resp.Status == 0 && resp.Result == "created" {
- fmt.Println("新增成功")
- err = nil
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- }
- return
- }
- //新增和修改数据
- func EsAddOrEditDataV4(indexName, docId string, item *ElasticTestArticleDetailV4) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditData Err:", err.Error())
- }
- }()
- client := utils.Client
- //if err != nil {
- // return
- //}
- searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
- if err != nil && !strings.Contains(err.Error(), "404") {
- fmt.Println("Get Err" + err.Error())
- return
- }
- if searchById != nil && searchById.Found {
- resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
- "BodyText": item.BodyText,
- "Title": item.Title,
- "PublishDate": item.PublishDate,
- "IsSummary": item.IsSummary,
- "IsReport": item.IsReport,
- }).Do(context.Background())
- if err != nil {
- return err
- }
- fmt.Println(resp.Status, resp.Result)
- if resp.Status == 0 {
- fmt.Println("修改成功")
- } else {
- fmt.Println("EditData", resp.Status, resp.Result)
- }
- } else {
- resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
- if err != nil {
- fmt.Println("新增失败:", err.Error())
- return err
- }
- if resp.Status == 0 && resp.Result == "created" {
- fmt.Println("新增成功")
- err = nil
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- }
- return
- }
- //删除数据
- func EsDeleteData(indexName, docId string) (err error) {
- client := utils.Client
- //if err != nil {
- // return
- //}
- resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
- if err != nil {
- return
- }
- if resp.Status == 0 {
- fmt.Println("删除成功")
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- return
- }
- func MappingModify(indexName, mappingJson string) {
- client := utils.Client
- //if err != nil {
- // return
- //}
- result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
- fmt.Println(err)
- fmt.Println(result)
- return
- }
- func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
- client := utils.Client
- pageSize := 20
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
- fmt.Println(keyWordArr)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- fmt.Println("-------------------------------")
- fmt.Println(keyWordArr)
- searchMap := make(map[int]int)
- boolquery := elastic.NewBoolQuery()
- keyLen := len(keyWordArr)
- n := 5.0 * float64(keyLen)
- 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")
- //
- //matchArr = append(matchArr, matchq1)
- //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 = n - 5
- }
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(indexName).Highlight(highlight).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, err
- }
- article := new(models.CygxArticle)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, err
- }
- if _, ok := searchMap[article.ArticleId]; !ok {
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- fmt.Println(body)
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- result = append(result, searchItem)
- searchMap[article.ArticleId] = article.ArticleId
- }
- }
- }
- return
- }
- func EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
- client := utils.Client
- pageSize := 20
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
- fmt.Println(keyWordArr)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- fmt.Println("-------------------------------")
- fmt.Println(keyWordArr)
- searchMap := make(map[int]int)
- boolquery := elastic.NewBoolQuery()
- //keyLen := len(keyWordArr)
- //n := float64(keyLen)
- 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>")
- request := client.Search(indexName).Highlight(highlight).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- fmt.Println("err:", err, searchByMatch)
- return
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, err
- }
- article := new(models.CygxArticle)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, err
- }
- if _, ok := searchMap[article.ArticleId]; !ok {
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- searchItem.Body = v.Highlight["BodyText"]
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- result = append(result, searchItem)
- searchMap[article.ArticleId] = article.ArticleId
- }
- }
- }
- return
- }
- func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
- fmt.Println(keyWordArr)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- fmt.Println("-------------------------------")
- fmt.Println(keyWordArr)
- searchMap := make(map[int]int)
- 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")
- //
- //matchArr = append(matchArr, matchq1)
- //matchArr = append(matchArr, matchq2)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- keyWordWeight := GetWeight(keyWordLen)
- fmt.Println(keyWordArr)
- fmt.Println(keyWordWeight)
- for k, v := range keyWordArr {
- if v != "" {
- weight := float64(keyWordWeight[k])
- titleMatchq := elastic.NewMatchQuery("Title", v).Analyzer("ik_smart")
- bodyMatchq := elastic.NewMatchQuery("BodyText", v).Analyzer("ik_smart")
- titleFunctionQuery := elastic.NewFunctionScoreQuery()
- titleFunctionQuery.Query(titleMatchq)
- titleFunctions := elastic.NewWeightFactorFunction(weight)
- titleFunctionQuery.AddScoreFunc(titleFunctions)
- titleFunctionQuery.BoostMode("replace")
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery.Query(bodyMatchq)
- bodyFunctions := elastic.NewWeightFactorFunction(weight)
- bodyFunctionQuery.AddScoreFunc(bodyFunctions)
- bodyFunctionQuery.BoostMode("replace")
- matchArr = append(matchArr, titleFunctionQuery)
- matchArr = append(matchArr, bodyFunctionQuery)
- }
- n++
- }
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- //searchJson, err := json.Marshal(searchByMatch)
- if searchByMatch != nil {
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticle)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- if _, ok := searchMap[article.ArticleId]; !ok {
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- fmt.Println(body)
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- result = append(result, searchItem)
- searchMap[article.ArticleId] = article.ArticleId
- }
- }
- }
- }
- total = searchByMatch.Hits.TotalHits.Value
- return
- }
- func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- //artidArr := make([]elastic.Query, 0)
- //matchArr := make([]elastic.Query, 0)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
- //keyWordWeight := GetWeight(keyWordLen)
- for _, v := range keyWordArr {
- if v != "" {
- matchArr := make([]elastic.Query, 0)
- boolquery := elastic.NewBoolQuery()
- //weight := float64(keyWordWeight[k])
- //multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
- //bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- //bodyFunctionQuery.Query(multiMatch)
- //bodyFunctions := elastic.NewWeightFactorFunction(weight)
- //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
- //bodyFunctionQuery.BoostMode("replace")
- //matchArr = append(matchArr, bodyFunctionQuery)
- //weight := float64(keyWordWeight[k])
- multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery.Query(multiMatch)
- //bodyFunctions := elastic.NewWeightFactorFunction(weight)
- //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
- //bodyFunctionQuery.BoostMode("replace")
- matchArr = append(matchArr, bodyFunctionQuery)
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- if err != nil {
- return nil, 0, err
- }
- if searchByMatch != nil {
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- var isAppend bool
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticle)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- for _, v_result := range result {
- if v_result.ArticleId == searchItem.ArticleId {
- isAppend = true
- }
- }
- if !isAppend {
- result = append(result, searchItem)
- }
- }
- }
- //total += searchByMatch.Hits.TotalHits.Value
- }
- }
- n++
- }
- total = int64(len(result))
- //fmt.Println(result)
- //boolquery.Should(matchArr...)
- //highlight := elastic.NewHighlight()
- //highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- //highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- //request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
- //searchByMatch, err := request.Do(context.Background())
- //if searchByMatch != nil {
- // if searchByMatch.Hits != nil {
- // for _, v := range searchByMatch.Hits.Hits {
- // articleJson, err := v.Source.MarshalJSON()
- // if err != nil {
- // return nil, 0, err
- // }
- // article := new(models.CygxArticle)
- // err = json.Unmarshal(articleJson, &article)
- // if err != nil {
- // return nil, 0, err
- // }
- // searchItem := new(models.SearchItem)
- // searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- // if len(v.Highlight["BodyText"]) > 0 {
- // searchItem.Body = v.Highlight["BodyText"]
- // } else {
- // bodyRune := []rune(article.BodyText)
- // bodyRuneLen := len(bodyRune)
- // if bodyRuneLen > 100 {
- // bodyRuneLen = 100
- // }
- // body := string(bodyRune[:bodyRuneLen])
- // searchItem.Body = []string{body}
- // }
- // var title string
- // if len(v.Highlight["Title"]) > 0 {
- // title = v.Highlight["Title"][0]
- // } else {
- // title = article.Title
- // }
- // searchItem.Title = title
- // searchItem.PublishDate = article.PublishDate
- //
- // result = append(result, searchItem)
- // }
- // }
- // total = searchByMatch.Hits.TotalHits.Value
- //}
- return
- }
- func EsMultiMatchFunctionScoreQueryFix(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- boolquery := elastic.NewBoolQuery()
- matchArr := make([]elastic.Query, 0)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- fmt.Println(keyWordArr)
- keyWordWeight := GetWeight(keyWordLen)
- for k, v := range keyWordArr {
- if v != "" {
- weight := float64(keyWordWeight[k])
- multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery.Query(multiMatch)
- bodyFunctions := elastic.NewWeightFactorFunction(weight)
- bodyFunctionQuery.AddScoreFunc(bodyFunctions)
- bodyFunctionQuery.BoostMode("replace")
- matchArr = append(matchArr, bodyFunctionQuery)
- }
- n++
- }
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- if searchByMatch != nil {
- matchResult, _ := json.Marshal(searchByMatch)
- utils.FileLog.Info("%s", string(matchResult))
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- articleJson, err := v.Source.MarshalJSON()
- utils.FileLog.Info("%s", string(articleJson))
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticle)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- result = append(result, searchItem)
- }
- }
- total = searchByMatch.Hits.TotalHits.Value
- }
- return
- }
- func GetWeight(length int) []int {
- steep := 10
- intArr := make([]int, 0)
- for i := 1; i <= length; i++ {
- if i == 1 {
- intArr = append(intArr, 1)
- } else {
- min := GetArrSum(intArr)
- maxVal := utils.GetRandInt(min, min+steep)
- intArr = append(intArr, maxVal)
- }
- }
- //数组排序
- sort.Slice(intArr, func(i, j int) bool {
- return intArr[i] > intArr[j]
- })
- return intArr
- }
- func GetArrSum(intArr []int) (sum int) {
- for _, val := range intArr {
- //累计求和
- sum += val
- }
- return
- }
- //func init() {
- // fmt.Println("start")
- // keyWord:="阿里巴巴"
- // keyWordArr, _ := GetIndustryMapNameSliceV2(keyWord)
- // keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- // fmt.Println(keyWordArr)
- // fmt.Println("end")
- //}
- func EsMultiMatchFunctionScoreQuerySort(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- //artidArr := make([]elastic.Query, 0)
- //matchArr := make([]elastic.Query, 0)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
- utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
- //keyWordWeight := GetWeight(keyWordLen)
- for _, v := range keyWordArr {
- if v != "" {
- matchArr := make([]elastic.Query, 0)
- boolquery := elastic.NewBoolQuery()
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
- //multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
- multiMatch := elastic.NewMultiMatchQuery(v, "Title").Analyzer("ik_smart").Boost(100)
- bodyFunctionQuery.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery)
- multiMatch = elastic.NewMultiMatchQuery(v, "BodyText").Analyzer("ik_smart").Boost(1)
- bodyFunctionQuery2.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery2)
- //multiMatch = elastic.NewMultiMatchQuery(1, "IsSummary")
- bodyFunctionQuery3.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery3)
- boolquery.Should(matchArr...)
- //multiMatch = elastic.NewMultiMatchQuery(v, "BodyText").Analyzer("ik_smart")
- //bodyFunctionQuery.Query(multiMatch)
- //matchArr = append(matchArr, bodyFunctionQuery)
- //boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(0).Size(pageSize).Query(boolquery)
- if orderColumn == "Matching" {
- request = client.Search(indexName).Highlight(highlight).From(0).Size(pageSize).Query(boolquery)
- }
- searchByMatch, err := request.Do(context.Background())
- if err != nil {
- return nil, 0, err
- }
- if searchByMatch != nil {
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- var isAppend bool
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticleEs)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- searchItem.ExpertBackground = article.ExpertBackground
- searchItem.CategoryId = article.CategoryId
- for _, v_result := range result {
- if v_result.ArticleId == searchItem.ArticleId {
- isAppend = true
- }
- }
- if !isAppend {
- result = append(result, searchItem)
- }
- }
- }
- //total += searchByMatch.Hits.TotalHits.Value
- }
- }
- n++
- }
- total = int64(len(result))
- return
- }
- func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- boolquery := elastic.NewBoolQuery()
- matchArr := make([]elastic.Query, 0)
- //matchArr2 := make([]elastic.Query, 0)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
- for _, v := range keyWordArr {
- if v != "" {
- multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery)
- }
- n++
- }
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- if searchByMatch != nil {
- matchResult, _ := json.Marshal(searchByMatch)
- utils.FileLog.Info("%s", string(matchResult))
- fmt.Println(len(searchByMatch.Hits.Hits))
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- articleJson, err := v.Source.MarshalJSON()
- utils.FileLog.Info("%s", string(articleJson))
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticleEs)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- searchItem.ExpertBackground = article.ExpertBackground
- searchItem.CategoryId = article.CategoryId
- result = append(result, searchItem)
- }
- }
- total = searchByMatch.Hits.TotalHits.Value
- }
- return
- }
- func EsSearchReport(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- for _, v := range keyWordArr {
- fmt.Println(v)
- }
- utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
- for _, v := range keyWordArr {
- if v != "" {
- matchArr := make([]elastic.Query, 0)
- boolquery := elastic.NewBoolQuery()
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
- multiMatch := elastic.NewMultiMatchQuery(v, "Title").Analyzer("ik_smart")
- bodyFunctionQuery.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery)
- multiMatch = elastic.NewMultiMatchQuery(1, "IsReport")
- bodyFunctionQuery2.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery2)
- boolquery.Must(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"))
- request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(0).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- if err != nil {
- return nil, 0, err
- }
- if searchByMatch != nil {
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- var isAppend bool
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticleEs)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- for _, v_result := range result {
- if v_result.ArticleId == searchItem.ArticleId {
- isAppend = true
- }
- }
- if !isAppend {
- result = append(result, searchItem)
- }
- }
- }
- }
- }
- n++
- }
- total = int64(len(result))
- return
- }
- //分页
- func EsMultiMatchFunctionScoreQueryTimeSortPage(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- boolquery := elastic.NewBoolQuery()
- matchArr := make([]elastic.Query, 0)
- n := 0
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- for _, v := range keyWordArr {
- if v != "" {
- multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery)
- }
- n++
- }
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
- searchByMatch, err := request.Do(context.Background())
- if searchByMatch != nil {
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticleEs)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- searchItem.ExpertBackground = article.ExpertBackground
- searchItem.CategoryId = article.CategoryId
- result = append(result, searchItem)
- }
- }
- total = searchByMatch.Hits.TotalHits.Value
- }
- return
- }
- func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
- client := utils.Client
- keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
- keyWordLen := len(keyWordArr)
- if keyWordLen <= 0 {
- keyWordArr = append(keyWordArr, keyWord)
- keyWordLen = len(keyWordArr)
- }
- var keyWords string
- for _, v := range keyWordArr {
- keyWords += v + " "
- }
- // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
- //keyWordWeight := GetWeight(keyWordLen)
- matchArr := make([]elastic.Query, 0)
- boolquery := elastic.NewBoolQuery()
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
- bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
- multiMatch := elastic.NewMultiMatchQuery(keyWords, "Title").Analyzer("ik_smart").Boost(100)
- bodyFunctionQuery.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery)
- multiMatch = elastic.NewMultiMatchQuery(keyWords, "BodyText").Analyzer("ik_smart").Boost(1)
- bodyFunctionQuery2.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery2)
- bodyFunctionQuery3.Query(multiMatch)
- matchArr = append(matchArr, bodyFunctionQuery3)
- boolquery.Should(matchArr...)
- highlight := elastic.NewHighlight()
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
- request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
- if orderColumn == "Matching" {
- request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
- }
- searchByMatch, err := request.Do(context.Background())
- if err != nil {
- return nil, 0, err
- }
- if searchByMatch != nil {
- if searchByMatch.Hits != nil {
- for _, v := range searchByMatch.Hits.Hits {
- var isAppend bool
- articleJson, err := v.Source.MarshalJSON()
- if err != nil {
- return nil, 0, err
- }
- article := new(models.CygxArticleEs)
- err = json.Unmarshal(articleJson, &article)
- if err != nil {
- return nil, 0, err
- }
- searchItem := new(models.SearchItem)
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 100 {
- bodyRuneLen = 100
- }
- body := string(bodyRune[:bodyRuneLen])
- searchItem.Body = []string{body}
- }
- var title string
- if len(v.Highlight["Title"]) > 0 {
- title = v.Highlight["Title"][0]
- } else {
- title = article.Title
- }
- searchItem.Title = title
- searchItem.PublishDate = article.PublishDate
- searchItem.ExpertBackground = article.ExpertBackground
- searchItem.CategoryId = article.CategoryId
- for _, v_result := range result {
- if v_result.ArticleId == searchItem.ArticleId {
- isAppend = true
- }
- }
- if !isAppend {
- result = append(result, searchItem)
- }
- }
- }
- }
- total += searchByMatch.Hits.TotalHits.Value
- return
- }
|