diff --git a/session.go b/session.go index 55133c745..5801e225c 100644 --- a/session.go +++ b/session.go @@ -1011,6 +1011,8 @@ type indexSpec struct { DefaultLanguage string "default_language,omitempty" LanguageOverride string "language_override,omitempty" TextIndexVersion int "textIndexVersion,omitempty" + + Collation *Collation "collation,omitempty" } type Index struct { @@ -1049,6 +1051,54 @@ type Index struct { // from the weighted sum of the frequency for each of the indexed fields in // that document. The default field weight is 1. Weights map[string]int + + // Collation defines the collation to use for the index. + Collation *Collation +} + +type Collation struct { + + // Locale defines the collation locale. + Locale string `bson:"locale"` + + // CaseLevel defines whether to turn case sensitivity on at strength 1 or 2. + CaseLevel bool `bson:"caseLevel,omitempty"` + + // CaseFirst may be set to "upper" or "lower" to define whether + // to have uppercase or lowercase items first. Default is "off". + CaseFirst string `bson:"caseFirst,omitempty"` + + // Strength defines the priority of comparison properties, as follows: + // + // 1 (primary) - Strongest level, denote difference between base characters + // 2 (secondary) - Accents in characters are considered secondary differences + // 3 (tertiary) - Upper and lower case differences in characters are + // distinguished at the tertiary level + // 4 (quaternary) - When punctuation is ignored at level 1-3, an additional + // level can be used to distinguish words with and without + // punctuation. Should only be used if ignoring punctuation + // is required or when processing Japanese text. + // 5 (identical) - When all other levels are equal, the identical level is + // used as a tiebreaker. The Unicode code point values of + // the NFD form of each string are compared at this level, + // just in case there is no difference at levels 1-4 + // + // Strength defaults to 3. + Strength int `bson:"strength,omitempty"` + + // NumericOrdering defines whether to order numbers based on numerical + // order and not collation order. + NumericOrdering bool `bson:"numericOrdering,omitempty"` + + // Alternate controls whether spaces and punctuation are considered base characters. + // May be set to "non-ignorable" (spaces and punctuation considered base characters) + // or "shifted" (spaces and punctuation not considered base characters, and only + // distinguished at strength > 3). Defaults to "non-ignorable". + Alternate string `bson:"alternate,omitempty"` + + // Backwards defines whether to have secondary differences considered in reverse order, + // as done in the French language. + Backwards bool `bson:"backwards,omitempty"` } // mgo.v3: Drop Minf and Maxf and transform Min and Max to floats. @@ -1242,6 +1292,7 @@ func (c *Collection) EnsureIndex(index Index) error { Weights: keyInfo.weights, DefaultLanguage: index.DefaultLanguage, LanguageOverride: index.LanguageOverride, + Collation: index.Collation, } if spec.Min == 0 && spec.Max == 0 { @@ -1456,6 +1507,7 @@ func indexFromSpec(spec indexSpec) Index { DefaultLanguage: spec.DefaultLanguage, LanguageOverride: spec.LanguageOverride, ExpireAfter: time.Duration(spec.ExpireAfter) * time.Second, + Collation: spec.Collation, } if float64(int(spec.Min)) == spec.Min && float64(int(spec.Max)) == spec.Max { index.Min = int(spec.Min) @@ -1584,7 +1636,7 @@ func (s *Session) Refresh() { } // SetMode changes the consistency mode for the session. -// +// // The default mode is Strong. // // In the Strong consistency mode reads and writes will always be made to