@@ -28,28 +28,23 @@ import (
2828)
2929
3030type indexData struct {
31- ID string `json:"id,omitempty"`
32- Type string `json:"type"`
33- Fields []string `json:"fields,omitempty"`
34- Unique * bool `json:"unique,omitempty"`
35- Deduplicate * bool `json:"deduplicate,omitempty"`
36- Sparse * bool `json:"sparse,omitempty"`
37- GeoJSON * bool `json:"geoJson,omitempty"`
38- InBackground * bool `json:"inBackground,omitempty"`
39- Estimates * bool `json:"estimates,omitempty"`
40- MinLength int `json:"minLength,omitempty"`
41- ExpireAfter int `json:"expireAfter,omitempty"`
42- Name string `json:"name,omitempty"`
43- }
44-
45- type genericIndexData struct {
46- ID string `json:"id,omitempty"`
47- Type string `json:"type"`
48- Name string `json:"name,omitempty"`
31+ ID string `json:"id,omitempty"`
32+ Type string `json:"type"`
33+ Fields []string `json:"fields,omitempty"`
34+ Unique * bool `json:"unique,omitempty"`
35+ Deduplicate * bool `json:"deduplicate,omitempty"`
36+ Sparse * bool `json:"sparse,omitempty"`
37+ GeoJSON * bool `json:"geoJson,omitempty"`
38+ InBackground * bool `json:"inBackground,omitempty"`
39+ Estimates * bool `json:"estimates,omitempty"`
40+ MinLength int `json:"minLength,omitempty"`
41+ ExpireAfter int `json:"expireAfter,omitempty"`
42+ Name string `json:"name,omitempty"`
43+ FieldValueTypes string `json:"fieldValueTypes,omitempty"`
4944}
5045
5146type indexListResponse struct {
52- Indexes []genericIndexData `json:"indexes,omitempty"`
47+ Indexes []indexData `json:"indexes,omitempty"`
5348}
5449
5550// Index opens a connection to an existing index within the collection.
@@ -70,7 +65,7 @@ func (c *collection) Index(ctx context.Context, name string) (Index, error) {
7065 if err := resp .ParseBody ("" , & data ); err != nil {
7166 return nil , WithStack (err )
7267 }
73- idx , err := newIndex (data . ID , data . Type , data . Name , c )
68+ idx , err := newIndex (data , c )
7469 if err != nil {
7570 return nil , WithStack (err )
7671 }
@@ -116,7 +111,7 @@ func (c *collection) Indexes(ctx context.Context) ([]Index, error) {
116111 }
117112 result := make ([]Index , 0 , len (data .Indexes ))
118113 for _ , x := range data .Indexes {
119- idx , err := newIndex (x . ID , x . Type , x . Name , c )
114+ idx , err := newIndex (x , c )
120115 if err != nil {
121116 return nil , WithStack (err )
122117 }
@@ -269,6 +264,29 @@ func (c *collection) EnsureTTLIndex(ctx context.Context, field string, expireAft
269264 return idx , created , nil
270265}
271266
267+ // EnsureZKDIndex creates a ZKD index in the collection, if it does not already exist.
268+ // Fields is a slice of attribute paths.
269+ // The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
270+ func (c * collection ) EnsureZKDIndex (ctx context.Context , fields []string , options * EnsureZKDIndexOptions ) (Index , bool , error ) {
271+ input := indexData {
272+ Type : string (ZKDIndex ),
273+ Fields : fields ,
274+ // fieldValueTypes is required and the only allowed value is "double". Future extensions of the index will allow other types.
275+ FieldValueTypes : "double" ,
276+ }
277+ if options != nil {
278+ input .InBackground = & options .InBackground
279+ input .Name = options .Name
280+ input .Unique = & options .Unique
281+ //input.Sparse = &options.Sparse
282+ }
283+ idx , created , err := c .ensureIndex (ctx , input )
284+ if err != nil {
285+ return nil , false , WithStack (err )
286+ }
287+ return idx , created , nil
288+ }
289+
272290// ensureIndex creates a persistent index in the collection, if it does not already exist.
273291// Fields is a slice of attribute paths.
274292// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
@@ -293,7 +311,7 @@ func (c *collection) ensureIndex(ctx context.Context, options indexData) (Index,
293311 if err := resp .ParseBody ("" , & data ); err != nil {
294312 return nil , false , WithStack (err )
295313 }
296- idx , err := newIndex (data . ID , data . Type , data . Name , c )
314+ idx , err := newIndex (data , c )
297315 if err != nil {
298316 return nil , false , WithStack (err )
299317 }
0 commit comments