@@ -20,21 +20,22 @@ var (
2020)
2121
2222type SearcherConfig struct {
23- Client * elasticsearch.Client
24- TypeRepo models.TypeRepository
25- TypeWhiteList []string
26- CachedTypesMapDuration int
23+ Client * elasticsearch.Client
24+ TypeRepo models.TypeRepository
25+ TypeWhiteList []string
26+ CachedTypesDuration int
2727}
2828
2929// Searcher is an implementation of models.RecordSearcher
3030type Searcher struct {
31- cli * elasticsearch.Client
32- typeWhiteList []string
33- typeWhiteListSet map [string ]bool
34- typeRepository models.TypeRepository
35- cachedTypesMap map [string ]models.Type
36- cachedTypeExpiredOn time.Time
37- cachedTypesMapDuration int
31+ cli * elasticsearch.Client
32+ typeWhiteList []string
33+ typeWhiteListSet map [string ]bool
34+ typeRepository models.TypeRepository
35+ cachedTypes []models.Type
36+ cachedTypesMap map [string ]models.Type
37+ cachedTypeExpiredOn time.Time
38+ cachedTypesDuration int
3839}
3940
4041// NewSearcher creates a new instance of Searcher
@@ -52,11 +53,11 @@ func NewSearcher(config SearcherConfig) (*Searcher, error) {
5253 }
5354
5455 return & Searcher {
55- cli : config .Client ,
56- typeWhiteList : config .TypeWhiteList ,
57- typeWhiteListSet : whiteListSet ,
58- typeRepository : config .TypeRepo ,
59- cachedTypesMapDuration : config .CachedTypesMapDuration ,
56+ cli : config .Client ,
57+ typeWhiteList : config .TypeWhiteList ,
58+ typeWhiteListSet : whiteListSet ,
59+ typeRepository : config .TypeRepo ,
60+ cachedTypesDuration : config .CachedTypesDuration ,
6061 }, nil
6162}
6263
@@ -129,9 +130,14 @@ func (sr *Searcher) buildQuery(ctx context.Context, cfg models.SearchConfig, ind
129130}
130131
131132func (sr * Searcher ) buildQueriesFromIndices (ctx context.Context , indices []string , cfg models.SearchConfig ) ([]elastic.Query , error ) {
133+ types , err := sr .mapIndicesToTypes (indices )
134+ if err != nil {
135+ return nil , err
136+ }
137+
132138 var queries []elastic.Query
133- for _ , index := range indices {
134- fields , err := sr .buildTypeFields (ctx , index )
139+ for _ , typ := range types {
140+ fields , err := sr .buildTypeFields (typ )
135141 if err != nil {
136142 return nil , err
137143 }
@@ -156,20 +162,15 @@ func (sr *Searcher) buildQueriesFromIndices(ctx context.Context, indices []strin
156162 Fuzziness ("AUTO" ),
157163 ).
158164 Filter (
159- elastic .NewTermQuery ("_index" , index ),
165+ elastic .NewTermQuery ("_index" , typ . Name ),
160166 )
161167 queries = append (queries , query )
162168 }
163169
164170 return queries , nil
165171}
166172
167- func (sr * Searcher ) buildTypeFields (ctx context.Context , typeName string ) (fields []string , err error ) {
168- resourceType , err := sr .getType (ctx , typeName )
169- if err != nil {
170- return fields , err
171- }
172-
173+ func (sr * Searcher ) buildTypeFields (resourceType models.Type ) (fields []string , err error ) {
173174 fields = append (
174175 fields ,
175176 fmt .Sprintf ("%s^10" , resourceType .Fields .ID ),
@@ -217,41 +218,52 @@ func (sr *Searcher) searchIndices(localWhiteList []string) []string {
217218 case hasGL || hasLL :
218219 return anyValidStringSlice (localWhiteList , sr .typeWhiteList )
219220 default :
220- return []string {defaultSearchIndex }
221+ return []string {}
221222 }
222223}
223224
224- func (sr * Searcher ) getType (ctx context.Context , typeName string ) (models.Type , error ) {
225- if sr .cachedTypesMap == nil || time .Now ().After (sr .cachedTypeExpiredOn ) {
226- typesMap , err := sr .buildTypesMap (ctx )
227- if err != nil {
228- return models.Type {}, err
229- }
230-
231- sr .cachedTypesMap = typesMap
232- sr .cachedTypeExpiredOn = time .Now ().Add (time .Duration (sr .cachedTypesMapDuration ) * time .Second )
225+ func (sr * Searcher ) mapIndicesToTypes (indices []string ) ([]models.Type , error ) {
226+ types , err := sr .getTypes (context .Background ())
227+ if err != nil {
228+ return types , err
229+ }
230+ if len (indices ) == 0 {
231+ return types , nil
233232 }
234233
235- resourceType , ok := sr .cachedTypesMap [typeName ]
236- if ! ok {
237- return models.Type {}, fmt .Errorf ("type does not exist" )
234+ whitelistedTypes := []models.Type {}
235+ for _ , index := range indices {
236+ typ , ok := sr .cachedTypesMap [index ]
237+ if ok {
238+ whitelistedTypes = append (whitelistedTypes , typ )
239+ }
238240 }
239241
240- return resourceType , nil
242+ return whitelistedTypes , nil
241243}
242244
243- func (sr * Searcher ) buildTypesMap (ctx context.Context ) (map [string ]models.Type , error ) {
244- types , err := sr .typeRepository .GetAll (ctx )
245- if err != nil {
246- return nil , err
245+ func (sr * Searcher ) getTypes (ctx context.Context ) ([]models.Type , error ) {
246+ if sr .cachedTypes == nil || time .Now ().After (sr .cachedTypeExpiredOn ) {
247+ types , err := sr .typeRepository .GetAll (ctx )
248+ if err != nil {
249+ return nil , err
250+ }
251+
252+ sr .cachedTypes = types
253+ sr .cachedTypesMap = sr .buildTypesMap (types )
254+ sr .cachedTypeExpiredOn = time .Now ().Add (time .Duration (sr .cachedTypesDuration ) * time .Second )
247255 }
248256
257+ return sr .cachedTypes , nil
258+ }
259+
260+ func (sr * Searcher ) buildTypesMap (types []models.Type ) map [string ]models.Type {
249261 typesMap := map [string ]models.Type {}
250262 for _ , typ := range types {
251263 typesMap [typ .Name ] = typ
252264 }
253265
254- return typesMap , nil
266+ return typesMap
255267}
256268
257269func anyValidStringSlice (slices ... []string ) []string {
0 commit comments