search_response.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. type ScoredDocument struct {
  14. DocId uint64
  15. // 文档的打分值
  16. // 搜索结果按照Scores的值排序,先按照第一个数排,如果相同则按照第二个数排序,依次类推。
  17. Scores []float32
  18. // 用于生成摘要的关键词在文本中的字节位置,该切片长度和SearchResponse.Tokens的长度一样
  19. // 只有当IndexType == LocationsIndex时不为空
  20. TokenSnippetLocations []int
  21. // 关键词出现的位置
  22. // 只有当IndexType == LocationsIndex时不为空
  23. TokenLocations [][]int
  24. }
  25. // 为了方便排序
  26. type ScoredDocuments []ScoredDocument
  27. func (docs ScoredDocuments) Len() int {
  28. return len(docs)
  29. }
  30. func (docs ScoredDocuments) Swap(i, j int) {
  31. docs[i], docs[j] = docs[j], docs[i]
  32. }
  33. func (docs ScoredDocuments) Less(i, j int) bool {
  34. // 为了从大到小排序,这实际上实现的是More的功能
  35. for iScore := 0; iScore < utils.MinInt(len(docs[i].Scores), len(docs[j].Scores)); iScore++ {
  36. if docs[i].Scores[iScore] > docs[j].Scores[iScore] {
  37. return true
  38. } else if docs[i].Scores[iScore] < docs[j].Scores[iScore] {
  39. return false
  40. }
  41. }
  42. return len(docs[i].Scores) > len(docs[j].Scores)
  43. }