search_response.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package types
  2. import (
  3. "github.com/huichen/wukong/utils"
  4. )
  5. type SearchResponse struct {
  6. // 搜索用到的关键词
  7. Tokens []string
  8. // 搜索到的文档,已排序
  9. Docs []ScoredDocument
  10. // 搜索是否超时。超时的情况下也可能会返回部分结果
  11. Timeout bool
  12. // 搜索到的文档个数。注意这是全部文档中满足条件的个数,可能比返回的文档数要大
  13. NumDocs int
  14. }
  15. type ScoredDocument struct {
  16. DocId uint64
  17. // 文档的打分值
  18. // 搜索结果按照Scores的值排序,先按照第一个数排,如果相同则按照第二个数排序,依次类推。
  19. Scores []float32
  20. // 用于生成摘要的关键词在文本中的字节位置,该切片长度和SearchResponse.Tokens的长度一样
  21. // 只有当IndexType == LocationsIndex时不为空
  22. TokenSnippetLocations []int
  23. // 关键词出现的位置
  24. // 只有当IndexType == LocationsIndex时不为空
  25. TokenLocations [][]int
  26. }
  27. // 为了方便排序
  28. type ScoredDocuments []ScoredDocument
  29. func (docs ScoredDocuments) Len() int {
  30. return len(docs)
  31. }
  32. func (docs ScoredDocuments) Swap(i, j int) {
  33. docs[i], docs[j] = docs[j], docs[i]
  34. }
  35. func (docs ScoredDocuments) Less(i, j int) bool {
  36. // 为了从大到小排序,这实际上实现的是More的功能
  37. for iScore := 0; iScore < utils.MinInt(len(docs[i].Scores), len(docs[j].Scores)); iScore++ {
  38. if docs[i].Scores[iScore] > docs[j].Scores[iScore] {
  39. return true
  40. } else if docs[i].Scores[iScore] < docs[j].Scores[iScore] {
  41. return false
  42. }
  43. }
  44. return len(docs[i].Scores) > len(docs[j].Scores)
  45. }