search.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package service
  2. import (
  3. "context"
  4. "regexp"
  5. "strings"
  6. "time"
  7. search "go-common/app/interface/openplatform/article/model/search"
  8. "go-common/library/log"
  9. )
  10. const _sourceType = "article"
  11. // Segment .
  12. func (s *Service) Segment(c context.Context, id int32, content string, withTag int, remarks string) (keywords []string, err error) {
  13. var (
  14. source = _sourceType
  15. trackid = int32(time.Now().Unix())
  16. res *search.TagboxResponse
  17. )
  18. if withTag == 1 {
  19. content = strings.Replace(content, " ", " ", -1)
  20. rule := "\\<[\\S\\s]+?\\>"
  21. reg, _ := regexp.Compile(rule)
  22. content = reg.ReplaceAllString(content, "")
  23. }
  24. req := &search.TagboxRequest{
  25. Id: &id,
  26. SourceType: &source,
  27. Content: &content,
  28. Trackid: &trackid,
  29. Remarks: &remarks,
  30. }
  31. if res, err = s.searchRPC.Segment(c, req); err != nil {
  32. log.Error("s.Segment error(%+v), params(%+v)", err, req)
  33. return
  34. }
  35. if *res.ExecCode != int32(0) {
  36. log.Error("creation: s.segment id(%d), code(%d)", id, res.ExecCode)
  37. return
  38. }
  39. keywords = res.GetKeywords()
  40. return
  41. }