github.com/ravendb/ravendb-go-client@v0.0.0-20240229102137-4474ee7aa0fa/abstract_index_creation_task.go (about) 1 package ravendb 2 3 // Note: IndexCreationTask combines functionality of Java's 4 // AbstractIndexCreationTask and AbstractMultiMapIndexCreationTask 5 6 // IndexCreationTask is for creating IndexDefinition 7 type IndexCreationTask struct { 8 // for a single map index, set Map 9 // for multiple map index, set Maps 10 Map string 11 Maps []string 12 13 Reduce string 14 15 Conventions *DocumentConventions 16 AdditionalSources map[string]string 17 Priority IndexPriority 18 LockMode IndexLockMode 19 20 StoresStrings map[string]FieldStorage 21 IndexesStrings map[string]FieldIndexing 22 AnalyzersStrings map[string]string 23 IndexSuggestions []string 24 TermVectorsStrings map[string]FieldTermVector 25 SpatialOptionsStrings map[string]*SpatialOptions 26 27 OutputReduceToCollection string 28 29 // Note: in Go IndexName must provided explicitly 30 // In Java it's dynamically calculated as getClass().getSimpleName() 31 IndexName string 32 } 33 34 // NewAbstractIndexCreationTask returns new IndexCreationTask 35 func NewIndexCreationTask(indexName string) *IndexCreationTask { 36 // Note: in Java we subclass IndexCreationTask and indexName is derived 37 // from derived class name. In Go we don't subclass and must provide index name 38 // manually 39 panicIf(indexName == "", "indexName cannot be empty") 40 return &IndexCreationTask{ 41 StoresStrings: make(map[string]FieldStorage), 42 IndexesStrings: make(map[string]FieldIndexing), 43 AnalyzersStrings: make(map[string]string), 44 TermVectorsStrings: make(map[string]FieldTermVector), 45 SpatialOptionsStrings: make(map[string]*SpatialOptions), 46 47 IndexName: indexName, 48 } 49 } 50 51 // CreateIndexDefinition creates IndexDefinition 52 func (t *IndexCreationTask) CreateIndexDefinition() *IndexDefinition { 53 if t.Conventions == nil { 54 t.Conventions = NewDocumentConventions() 55 } 56 57 indexDefinitionBuilder := NewIndexDefinitionBuilder(t.IndexName) 58 indexDefinitionBuilder.indexesStrings = t.IndexesStrings 59 indexDefinitionBuilder.analyzersStrings = t.AnalyzersStrings 60 indexDefinitionBuilder.setMap(t.Map) 61 indexDefinitionBuilder.reduce = t.Reduce 62 indexDefinitionBuilder.storesStrings = t.StoresStrings 63 indexDefinitionBuilder.suggestionsOptions = t.IndexSuggestions 64 indexDefinitionBuilder.termVectorsStrings = t.TermVectorsStrings 65 indexDefinitionBuilder.spatialIndexesStrings = t.SpatialOptionsStrings 66 indexDefinitionBuilder.outputReduceToCollection = t.OutputReduceToCollection 67 indexDefinitionBuilder.additionalSources = t.AdditionalSources 68 69 // validate for single map (Map set), don't validate multiple map (Maps) 70 validate := len(t.Maps) == 0 71 72 def := indexDefinitionBuilder.toIndexDefinition(t.Conventions, validate) 73 def.Maps = append(def.Maps, t.Maps...) 74 return def 75 } 76 77 // IsMapReduce returns true if this is map-reduce index 78 func (t *IndexCreationTask) IsMapReduce() bool { 79 return t.Reduce != "" 80 } 81 82 // Execute executes index in specified document store 83 func (t *IndexCreationTask) Execute(store *DocumentStore, conventions *DocumentConventions, database string) error { 84 return t.putIndex(store, conventions, database) 85 } 86 87 func (t *IndexCreationTask) putIndex(store *DocumentStore, conventions *DocumentConventions, database string) error { 88 oldConventions := t.Conventions 89 defer func() { t.Conventions = oldConventions }() 90 91 conv := conventions 92 if conv == nil { 93 conv = t.Conventions 94 } 95 if conv == nil { 96 conv = store.GetConventions() 97 } 98 t.Conventions = conv 99 100 indexDefinition := t.CreateIndexDefinition() 101 indexDefinition.Name = t.IndexName 102 indexDefinition.LockMode = t.LockMode 103 indexDefinition.Priority = t.Priority 104 105 op := NewPutIndexesOperation(indexDefinition) 106 if database == "" { 107 database = store.GetDatabase() 108 } 109 return store.Maintenance().ForDatabase(database).Send(op) 110 } 111 112 // Index registers field to be indexed 113 func (t *IndexCreationTask) Index(field string, indexing FieldIndexing) { 114 t.IndexesStrings[field] = indexing 115 } 116 117 // Spatial registers field to be spatially indexed 118 func (t *IndexCreationTask) Spatial(field string, indexing func() *SpatialOptions) { 119 v := indexing() 120 t.SpatialOptionsStrings[field] = v 121 } 122 123 // StoreAllFields selects if we're storing all fields or not 124 func (t *IndexCreationTask) StoreAllFields(storage FieldStorage) { 125 t.StoresStrings[IndexingFieldAllFields] = storage 126 } 127 128 // Store registers field to be stored 129 func (t *IndexCreationTask) Store(field string, storage FieldStorage) { 130 t.StoresStrings[field] = storage 131 } 132 133 // Analyze registers field to be analyzed 134 func (t *IndexCreationTask) Analyze(field string, analyzer string) { 135 t.AnalyzersStrings[field] = analyzer 136 } 137 138 // TermVector registers field to have term vectors 139 func (t *IndexCreationTask) TermVector(field string, termVector FieldTermVector) { 140 t.TermVectorsStrings[field] = termVector 141 } 142 143 // Suggestion registers field to be indexed as suggestions 144 func (t *IndexCreationTask) Suggestion(field string) { 145 t.IndexSuggestions = append(t.IndexSuggestions, field) 146 }