您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

search.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package search
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/document"
  18. "github.com/blevesearch/bleve/index"
  19. )
  20. type ArrayPositions []uint64
  21. func (ap ArrayPositions) Equals(other ArrayPositions) bool {
  22. if len(ap) != len(other) {
  23. return false
  24. }
  25. for i := range ap {
  26. if ap[i] != other[i] {
  27. return false
  28. }
  29. }
  30. return true
  31. }
  32. type Location struct {
  33. // Pos is the position of the term within the field, starting at 1
  34. Pos uint64 `json:"pos"`
  35. // Start and End are the byte offsets of the term in the field
  36. Start uint64 `json:"start"`
  37. End uint64 `json:"end"`
  38. // ArrayPositions contains the positions of the term within any elements.
  39. ArrayPositions ArrayPositions `json:"array_positions"`
  40. }
  41. type Locations []*Location
  42. type TermLocationMap map[string]Locations
  43. func (t TermLocationMap) AddLocation(term string, location *Location) {
  44. t[term] = append(t[term], location)
  45. }
  46. type FieldTermLocationMap map[string]TermLocationMap
  47. type FieldFragmentMap map[string][]string
  48. type DocumentMatch struct {
  49. Index string `json:"index,omitempty"`
  50. ID string `json:"id"`
  51. IndexInternalID index.IndexInternalID `json:"-"`
  52. Score float64 `json:"score"`
  53. Expl *Explanation `json:"explanation,omitempty"`
  54. Locations FieldTermLocationMap `json:"locations,omitempty"`
  55. Fragments FieldFragmentMap `json:"fragments,omitempty"`
  56. Sort []string `json:"sort,omitempty"`
  57. // Fields contains the values for document fields listed in
  58. // SearchRequest.Fields. Text fields are returned as strings, numeric
  59. // fields as float64s and date fields as time.RFC3339 formatted strings.
  60. Fields map[string]interface{} `json:"fields,omitempty"`
  61. // if we load the document for this hit, remember it so we dont load again
  62. Document *document.Document `json:"-"`
  63. // used to maintain natural index order
  64. HitNumber uint64 `json:"-"`
  65. }
  66. func (dm *DocumentMatch) AddFieldValue(name string, value interface{}) {
  67. if dm.Fields == nil {
  68. dm.Fields = make(map[string]interface{})
  69. }
  70. existingVal, ok := dm.Fields[name]
  71. if !ok {
  72. dm.Fields[name] = value
  73. return
  74. }
  75. valSlice, ok := existingVal.([]interface{})
  76. if ok {
  77. // already a slice, append to it
  78. valSlice = append(valSlice, value)
  79. } else {
  80. // create a slice
  81. valSlice = []interface{}{existingVal, value}
  82. }
  83. dm.Fields[name] = valSlice
  84. }
  85. // Reset allows an already allocated DocumentMatch to be reused
  86. func (dm *DocumentMatch) Reset() *DocumentMatch {
  87. // remember the []byte used for the IndexInternalID
  88. indexInternalID := dm.IndexInternalID
  89. // remember the []interface{} used for sort
  90. sort := dm.Sort
  91. // idiom to copy over from empty DocumentMatch (0 allocations)
  92. *dm = DocumentMatch{}
  93. // reuse the []byte already allocated (and reset len to 0)
  94. dm.IndexInternalID = indexInternalID[:0]
  95. // reuse the []interface{} already allocated (and reset len to 0)
  96. dm.Sort = sort[:0]
  97. return dm
  98. }
  99. func (dm *DocumentMatch) String() string {
  100. return fmt.Sprintf("[%s-%f]", string(dm.IndexInternalID), dm.Score)
  101. }
  102. type DocumentMatchCollection []*DocumentMatch
  103. func (c DocumentMatchCollection) Len() int { return len(c) }
  104. func (c DocumentMatchCollection) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
  105. func (c DocumentMatchCollection) Less(i, j int) bool { return c[i].Score > c[j].Score }
  106. type Searcher interface {
  107. Next(ctx *SearchContext) (*DocumentMatch, error)
  108. Advance(ctx *SearchContext, ID index.IndexInternalID) (*DocumentMatch, error)
  109. Close() error
  110. Weight() float64
  111. SetQueryNorm(float64)
  112. Count() uint64
  113. Min() int
  114. DocumentMatchPoolSize() int
  115. }
  116. type SearcherOptions struct {
  117. Explain bool
  118. IncludeTermVectors bool
  119. }
  120. // SearchContext represents the context around a single search
  121. type SearchContext struct {
  122. DocumentMatchPool *DocumentMatchPool
  123. }