1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- package services
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/olivere/elastic/v7"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/utils"
- "sort"
- "strconv"
- "strings"
- )
- 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
- }
- //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
- }
|