Skip to content

Commit 9a2573d

Browse files
committed
Initial collation support in indexes.
For now tested by hand due to new configdb option constraints introduced in current 3.3. Test harness will need to change yet again to adapt to latest supported options. :-(
1 parent 293ca5c commit 9a2573d

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

session.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,8 @@ type indexSpec struct {
10111011
DefaultLanguage string "default_language,omitempty"
10121012
LanguageOverride string "language_override,omitempty"
10131013
TextIndexVersion int "textIndexVersion,omitempty"
1014+
1015+
Collation *Collation "collation,omitempty"
10141016
}
10151017

10161018
type Index struct {
@@ -1049,6 +1051,54 @@ type Index struct {
10491051
// from the weighted sum of the frequency for each of the indexed fields in
10501052
// that document. The default field weight is 1.
10511053
Weights map[string]int
1054+
1055+
// Collation defines the collation to use for the index.
1056+
Collation *Collation
1057+
}
1058+
1059+
type Collation struct {
1060+
1061+
// Locale defines the collation locale.
1062+
Locale string `bson:"locale"`
1063+
1064+
// CaseLevel defines whether to turn case sensitivity on at strength 1 or 2.
1065+
CaseLevel bool `bson:"caseLevel,omitempty"`
1066+
1067+
// CaseFirst may be set to "upper" or "lower" to define whether
1068+
// to have uppercase or lowercase items first. Default is "off".
1069+
CaseFirst string `bson:"caseFirst,omitempty"`
1070+
1071+
// Strength defines the priority of comparison properties, as follows:
1072+
//
1073+
// 1 (primary) - Strongest level, denote difference between base characters
1074+
// 2 (secondary) - Accents in characters are considered secondary differences
1075+
// 3 (tertiary) - Upper and lower case differences in characters are
1076+
// distinguished at the tertiary level
1077+
// 4 (quaternary) - When punctuation is ignored at level 1-3, an additional
1078+
// level can be used to distinguish words with and without
1079+
// punctuation. Should only be used if ignoring punctuation
1080+
// is required or when processing Japanese text.
1081+
// 5 (identical) - When all other levels are equal, the identical level is
1082+
// used as a tiebreaker. The Unicode code point values of
1083+
// the NFD form of each string are compared at this level,
1084+
// just in case there is no difference at levels 1-4
1085+
//
1086+
// Strength defaults to 3.
1087+
Strength int `bson:"strength,omitempty"`
1088+
1089+
// NumericOrdering defines whether to order numbers based on numerical
1090+
// order and not collation order.
1091+
NumericOrdering bool `bson:"numericOrdering,omitempty"`
1092+
1093+
// Alternate controls whether spaces and punctuation are considered base characters.
1094+
// May be set to "non-ignorable" (spaces and punctuation considered base characters)
1095+
// or "shifted" (spaces and punctuation not considered base characters, and only
1096+
// distinguished at strength > 3). Defaults to "non-ignorable".
1097+
Alternate string `bson:"alternate,omitempty"`
1098+
1099+
// Backwards defines whether to have secondary differences considered in reverse order,
1100+
// as done in the French language.
1101+
Backwards bool `bson:"backwards,omitempty"`
10521102
}
10531103

10541104
// mgo.v3: Drop Minf and Maxf and transform Min and Max to floats.
@@ -1242,6 +1292,7 @@ func (c *Collection) EnsureIndex(index Index) error {
12421292
Weights: keyInfo.weights,
12431293
DefaultLanguage: index.DefaultLanguage,
12441294
LanguageOverride: index.LanguageOverride,
1295+
Collation: index.Collation,
12451296
}
12461297

12471298
if spec.Min == 0 && spec.Max == 0 {
@@ -1456,6 +1507,7 @@ func indexFromSpec(spec indexSpec) Index {
14561507
DefaultLanguage: spec.DefaultLanguage,
14571508
LanguageOverride: spec.LanguageOverride,
14581509
ExpireAfter: time.Duration(spec.ExpireAfter) * time.Second,
1510+
Collation: spec.Collation,
14591511
}
14601512
if float64(int(spec.Min)) == spec.Min && float64(int(spec.Max)) == spec.Max {
14611513
index.Min = int(spec.Min)
@@ -1584,7 +1636,7 @@ func (s *Session) Refresh() {
15841636
}
15851637

15861638
// SetMode changes the consistency mode for the session.
1587-
//
1639+
//
15881640
// The default mode is Strong.
15891641
//
15901642
// In the Strong consistency mode reads and writes will always be made to

0 commit comments

Comments
 (0)