我正在尝试创建具有权重的文本索引,但我无法通过阅读 API 文档弄清楚如何做。
如何在 mgo 中建立如下索引。
db.products.createIndex({
"primaryCategoryIndexes": "text",
"secondaryCategoryIndexes": "text",
"brandIndex": "text",
"primaryTitleIndexes": "text",
"secondaryTitleIndexes": "text",
"indexCycleId": "text"
}, {
"weights": {
"primaryCategoryIndexes":10,
"secondaryCategoryIndexes": 5,
"brandIndex": 5,
"primaryTitleIndexes": 5,
"secondaryTitleIndexes": 5,
"indexCycleId": 5
})
请您参考如下方法:
由于某些奇怪的原因,删除了评论中对此问题的直接回答。我尽量不继续,但我认为最好将答案发回。
可以使用 mgo.Index
中的字段来完成工作。但是我的某些版本不支持它。
请阅读手册: https://godoc.org/gopkg.in/mgo.v2#Index
type Index struct {
Key []string // Index key fields; prefix name with dash (-) for descending order
Unique bool // Prevent two documents from having the same index key
DropDups bool // Drop documents with the same index key as a previously indexed one
Background bool // Build index in background and return immediately
Sparse bool // Only index documents containing the Key fields
// If ExpireAfter is defined the server will periodically delete
// documents with indexed time.Time older than the provided delta.
ExpireAfter time.Duration
// Name holds the stored index name. On creation if this field is unset it is
// computed by EnsureIndex based on the index key.
Name string
// Properties for spatial indexes.
//
// Min and Max were improperly typed as int when they should have been
// floats. To preserve backwards compatibility they are still typed as
// int and the following two fields enable reading and writing the same
// fields as float numbers. In mgo.v3, these fields will be dropped and
// Min/Max will become floats.
Min, Max int
Minf, Maxf float64
BucketSize float64
Bits int
// Properties for text indexes.
DefaultLanguage string
LanguageOverride string
// Weights defines the significance of provided fields relative to other
// fields in a text index. The score for a given word in a document is derived
// 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
}
顺便说一句,关于golang,有godoc.org和go-serach.org可以搜索go libs,他们通常比谷歌好。