index.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package types
  2. type DocumentIndex struct {
  3. // 文本的DocId
  4. DocId string
  5. // 文本的关键词长
  6. TokenLength float32
  7. // 加入的索引键
  8. Keywords []KeywordIndex
  9. }
  10. // 反向索引项,这实际上标注了一个(搜索键,文档)对。
  11. type KeywordIndex struct {
  12. // 搜索键的UTF-8文本
  13. Text string
  14. // 搜索键词频
  15. Frequency float32
  16. // 搜索键在文档中的起始字节位置,按照升序排列
  17. Starts []int
  18. }
  19. // 索引器返回结果
  20. type IndexedDocument struct {
  21. DocId string
  22. // BM25,仅当索引类型为FrequenciesIndex或者LocationsIndex时返回有效值
  23. BM25 float32
  24. // 关键词在文档中的紧邻距离,紧邻距离的含义见computeTokenProximity的注释。
  25. // 仅当索引类型为LocationsIndex时返回有效值。
  26. TokenProximity int32
  27. // 紧邻距离计算得到的关键词位置,和Lookup函数输入tokens的长度一样且一一对应。
  28. // 仅当索引类型为LocationsIndex时返回有效值。
  29. TokenSnippetLocations []int
  30. // 关键词在文本中的具体位置。
  31. // 仅当索引类型为LocationsIndex时返回有效值。
  32. TokenLocations [][]int
  33. }
  34. // 方便批量加入文档索引
  35. type DocumentsIndex []*DocumentIndex
  36. func (docs DocumentsIndex) Len() int {
  37. return len(docs)
  38. }
  39. func (docs DocumentsIndex) Swap(i, j int) {
  40. docs[i], docs[j] = docs[j], docs[i]
  41. }
  42. func (docs DocumentsIndex) Less(i, j int) bool {
  43. return docs[i].DocId < docs[j].DocId
  44. }
  45. // 方便批量删除文档索引
  46. type DocumentsId []string
  47. func (docs DocumentsId) Len() int {
  48. return len(docs)
  49. }
  50. func (docs DocumentsId) Swap(i, j int) {
  51. docs[i], docs[j] = docs[j], docs[i]
  52. }
  53. func (docs DocumentsId) Less(i, j int) bool {
  54. return docs[i] < docs[j]
  55. }