searcher.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package dao
  2. import (
  3. "go-common/app/service/main/riot-search/model"
  4. "github.com/go-ego/riot/types"
  5. )
  6. // SearchIDOnly only return aids
  7. func (d *Dao) SearchIDOnly(arg *model.RiotSearchReq) *model.IDsResp {
  8. if arg.Keyword == "" {
  9. return nil
  10. }
  11. var docIDs map[uint64]bool
  12. if len(arg.IDs) != 0 {
  13. docIDs = make(map[uint64]bool, len(arg.IDs))
  14. for _, id := range arg.IDs {
  15. docIDs[id] = true
  16. }
  17. }
  18. output := d.searcher.Search(types.SearchReq{
  19. Text: arg.Keyword,
  20. DocIds: docIDs,
  21. Timeout: d.c.Riot.Timeout,
  22. RankOpts: &types.RankOpts{
  23. // 从第几条结果开始输出
  24. OutputOffset: (arg.Pn - 1) * arg.Ps,
  25. // 最大输出的搜索结果数,为 0 时无限制
  26. MaxOutputs: arg.Ps,
  27. },
  28. })
  29. docLength := len(output.Docs.(types.ScoredDocs))
  30. tokenLength := len(output.Tokens)
  31. res := &model.IDsResp{
  32. IDs: make([]uint64, docLength),
  33. Tokens: make([]string, tokenLength),
  34. Page: &model.Page{
  35. PageNum: arg.Pn,
  36. PageSize: arg.Ps,
  37. Total: docLength,
  38. },
  39. }
  40. for i, doc := range output.Docs.(types.ScoredDocs) {
  41. res.IDs[i] = doc.DocId
  42. }
  43. copy(res.Tokens, output.Tokens)
  44. res.IDs, res.Page.Total = uniqueIDs(res.IDs)
  45. return res
  46. }
  47. func uniqueIDs(IDs []uint64) (uIDs []uint64, length int) {
  48. m := make(map[uint64]struct{})
  49. for _, ID := range IDs {
  50. if _, ok := m[ID]; !ok {
  51. m[ID] = struct{}{}
  52. uIDs = append(uIDs, ID)
  53. length++
  54. }
  55. }
  56. return
  57. }
  58. // Search return archives info
  59. func (d *Dao) Search(arg *model.RiotSearchReq) *model.DocumentsResp {
  60. if arg.Keyword == "" {
  61. return nil
  62. }
  63. var docIDs map[uint64]bool
  64. if len(arg.IDs) != 0 {
  65. docIDs = make(map[uint64]bool, len(arg.IDs))
  66. for _, id := range arg.IDs {
  67. docIDs[id] = true
  68. }
  69. }
  70. output := d.searcher.Search(types.SearchReq{
  71. Text: arg.Keyword,
  72. DocIds: docIDs,
  73. Timeout: d.c.Riot.Timeout,
  74. RankOpts: &types.RankOpts{
  75. // 从第几条结果开始输出
  76. OutputOffset: (arg.Pn - 1) * arg.Ps,
  77. // 最大输出的搜索结果数,为 0 时无限制
  78. MaxOutputs: arg.Ps,
  79. },
  80. })
  81. docLength := len(output.Docs.(types.ScoredDocs))
  82. tokenLength := len(output.Tokens)
  83. res := &model.DocumentsResp{
  84. Documents: make([]model.Document, docLength),
  85. Tokens: make([]string, tokenLength),
  86. Page: &model.Page{
  87. PageNum: arg.Pn,
  88. PageSize: arg.Ps,
  89. Total: docLength,
  90. },
  91. }
  92. for i, doc := range output.Docs.(types.ScoredDocs) {
  93. res.Documents[i].ID = doc.DocId
  94. res.Documents[i].Content = doc.Content
  95. }
  96. copy(res.Tokens, output.Tokens)
  97. return res
  98. }
  99. // Has return DocId exists
  100. func (d *Dao) Has(id uint64) bool {
  101. return d.searcher.HasDoc(id)
  102. }