You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.go 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 bleve
  15. import (
  16. "github.com/blevesearch/bleve/document"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/index/store"
  19. "github.com/blevesearch/bleve/mapping"
  20. "golang.org/x/net/context"
  21. )
  22. // A Batch groups together multiple Index and Delete
  23. // operations you would like performed at the same
  24. // time. The Batch structure is NOT thread-safe.
  25. // You should only perform operations on a batch
  26. // from a single thread at a time. Once batch
  27. // execution has started, you may not modify it.
  28. type Batch struct {
  29. index Index
  30. internal *index.Batch
  31. }
  32. // Index adds the specified index operation to the
  33. // batch. NOTE: the bleve Index is not updated
  34. // until the batch is executed.
  35. func (b *Batch) Index(id string, data interface{}) error {
  36. if id == "" {
  37. return ErrorEmptyID
  38. }
  39. doc := document.NewDocument(id)
  40. err := b.index.Mapping().MapDocument(doc, data)
  41. if err != nil {
  42. return err
  43. }
  44. b.internal.Update(doc)
  45. return nil
  46. }
  47. // IndexAdvanced adds the specified index operation to the
  48. // batch which skips the mapping. NOTE: the bleve Index is not updated
  49. // until the batch is executed.
  50. func (b *Batch) IndexAdvanced(doc *document.Document) (err error) {
  51. if doc.ID == "" {
  52. return ErrorEmptyID
  53. }
  54. b.internal.Update(doc)
  55. return nil
  56. }
  57. // Delete adds the specified delete operation to the
  58. // batch. NOTE: the bleve Index is not updated until
  59. // the batch is executed.
  60. func (b *Batch) Delete(id string) {
  61. if id != "" {
  62. b.internal.Delete(id)
  63. }
  64. }
  65. // SetInternal adds the specified set internal
  66. // operation to the batch. NOTE: the bleve Index is
  67. // not updated until the batch is executed.
  68. func (b *Batch) SetInternal(key, val []byte) {
  69. b.internal.SetInternal(key, val)
  70. }
  71. // SetInternal adds the specified delete internal
  72. // operation to the batch. NOTE: the bleve Index is
  73. // not updated until the batch is executed.
  74. func (b *Batch) DeleteInternal(key []byte) {
  75. b.internal.DeleteInternal(key)
  76. }
  77. // Size returns the total number of operations inside the batch
  78. // including normal index operations and internal operations.
  79. func (b *Batch) Size() int {
  80. return len(b.internal.IndexOps) + len(b.internal.InternalOps)
  81. }
  82. // String prints a user friendly string representation of what
  83. // is inside this batch.
  84. func (b *Batch) String() string {
  85. return b.internal.String()
  86. }
  87. // Reset returns a Batch to the empty state so that it can
  88. // be re-used in the future.
  89. func (b *Batch) Reset() {
  90. b.internal.Reset()
  91. }
  92. // An Index implements all the indexing and searching
  93. // capabilities of bleve. An Index can be created
  94. // using the New() and Open() methods.
  95. //
  96. // Index() takes an input value, deduces a DocumentMapping for its type,
  97. // assigns string paths to its fields or values then applies field mappings on
  98. // them.
  99. //
  100. // The DocumentMapping used to index a value is deduced by the following rules:
  101. // 1) If value implements mapping.bleveClassifier interface, resolve the mapping
  102. // from BleveType().
  103. // 2) If value implements mapping.Classifier interface, resolve the mapping
  104. // from Type().
  105. // 3) If value has a string field or value at IndexMapping.TypeField.
  106. // (defaulting to "_type"), use it to resolve the mapping. Fields addressing
  107. // is described below.
  108. // 4) If IndexMapping.DefaultType is registered, return it.
  109. // 5) Return IndexMapping.DefaultMapping.
  110. //
  111. // Each field or nested field of the value is identified by a string path, then
  112. // mapped to one or several FieldMappings which extract the result for analysis.
  113. //
  114. // Struct values fields are identified by their "json:" tag, or by their name.
  115. // Nested fields are identified by prefixing with their parent identifier,
  116. // separated by a dot.
  117. //
  118. // Map values entries are identified by their string key. Entries not indexed
  119. // by strings are ignored. Entry values are identified recursively like struct
  120. // fields.
  121. //
  122. // Slice and array values are identified by their field name. Their elements
  123. // are processed sequentially with the same FieldMapping.
  124. //
  125. // String, float64 and time.Time values are identified by their field name.
  126. // Other types are ignored.
  127. //
  128. // Each value identifier is decomposed in its parts and recursively address
  129. // SubDocumentMappings in the tree starting at the root DocumentMapping. If a
  130. // mapping is found, all its FieldMappings are applied to the value. If no
  131. // mapping is found and the root DocumentMapping is dynamic, default mappings
  132. // are used based on value type and IndexMapping default configurations.
  133. //
  134. // Finally, mapped values are analyzed, indexed or stored. See
  135. // FieldMapping.Analyzer to know how an analyzer is resolved for a given field.
  136. //
  137. // Examples:
  138. //
  139. // type Date struct {
  140. // Day string `json:"day"`
  141. // Month string
  142. // Year string
  143. // }
  144. //
  145. // type Person struct {
  146. // FirstName string `json:"first_name"`
  147. // LastName string
  148. // BirthDate Date `json:"birth_date"`
  149. // }
  150. //
  151. // A Person value FirstName is mapped by the SubDocumentMapping at
  152. // "first_name". Its LastName is mapped by the one at "LastName". The day of
  153. // BirthDate is mapped to the SubDocumentMapping "day" of the root
  154. // SubDocumentMapping "birth_date". It will appear as the "birth_date.day"
  155. // field in the index. The month is mapped to "birth_date.Month".
  156. type Index interface {
  157. // Index analyzes, indexes or stores mapped data fields. Supplied
  158. // identifier is bound to analyzed data and will be retrieved by search
  159. // requests. See Index interface documentation for details about mapping
  160. // rules.
  161. Index(id string, data interface{}) error
  162. Delete(id string) error
  163. NewBatch() *Batch
  164. Batch(b *Batch) error
  165. // Document returns specified document or nil if the document is not
  166. // indexed or stored.
  167. Document(id string) (*document.Document, error)
  168. // DocCount returns the number of documents in the index.
  169. DocCount() (uint64, error)
  170. Search(req *SearchRequest) (*SearchResult, error)
  171. SearchInContext(ctx context.Context, req *SearchRequest) (*SearchResult, error)
  172. Fields() ([]string, error)
  173. FieldDict(field string) (index.FieldDict, error)
  174. FieldDictRange(field string, startTerm []byte, endTerm []byte) (index.FieldDict, error)
  175. FieldDictPrefix(field string, termPrefix []byte) (index.FieldDict, error)
  176. Close() error
  177. Mapping() mapping.IndexMapping
  178. Stats() *IndexStat
  179. StatsMap() map[string]interface{}
  180. GetInternal(key []byte) ([]byte, error)
  181. SetInternal(key, val []byte) error
  182. DeleteInternal(key []byte) error
  183. // Name returns the name of the index (by default this is the path)
  184. Name() string
  185. // SetName lets you assign your own logical name to this index
  186. SetName(string)
  187. // Advanced returns the indexer and data store, exposing lower level
  188. // methods to enumerate records and access data.
  189. Advanced() (index.Index, store.KVStore, error)
  190. }
  191. // New index at the specified path, must not exist.
  192. // The provided mapping will be used for all
  193. // Index/Search operations.
  194. func New(path string, mapping mapping.IndexMapping) (Index, error) {
  195. return newIndexUsing(path, mapping, Config.DefaultIndexType, Config.DefaultKVStore, nil)
  196. }
  197. // NewMemOnly creates a memory-only index.
  198. // The contents of the index is NOT persisted,
  199. // and will be lost once closed.
  200. // The provided mapping will be used for all
  201. // Index/Search operations.
  202. func NewMemOnly(mapping mapping.IndexMapping) (Index, error) {
  203. return newIndexUsing("", mapping, Config.DefaultIndexType, Config.DefaultMemKVStore, nil)
  204. }
  205. // NewUsing creates index at the specified path,
  206. // which must not already exist.
  207. // The provided mapping will be used for all
  208. // Index/Search operations.
  209. // The specified index type will be used.
  210. // The specified kvstore implementation will be used
  211. // and the provided kvconfig will be passed to its
  212. // constructor. Note that currently the values of kvconfig must
  213. // be able to be marshaled and unmarshaled using the encoding/json library (used
  214. // when reading/writing the index metadata file).
  215. func NewUsing(path string, mapping mapping.IndexMapping, indexType string, kvstore string, kvconfig map[string]interface{}) (Index, error) {
  216. return newIndexUsing(path, mapping, indexType, kvstore, kvconfig)
  217. }
  218. // Open index at the specified path, must exist.
  219. // The mapping used when it was created will be used for all Index/Search operations.
  220. func Open(path string) (Index, error) {
  221. return openIndexUsing(path, nil)
  222. }
  223. // OpenUsing opens index at the specified path, must exist.
  224. // The mapping used when it was created will be used for all Index/Search operations.
  225. // The provided runtimeConfig can override settings
  226. // persisted when the kvstore was created.
  227. func OpenUsing(path string, runtimeConfig map[string]interface{}) (Index, error) {
  228. return openIndexUsing(path, runtimeConfig)
  229. }