如何用权重定义mgo中的mongodb文本索引

问题描述 投票:-2回答:1

我正在尝试使用权重创建文本索引,但我无法通过阅读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
  })
mongodb go mgo
1个回答
0
投票

出于某种奇怪的原因,删除了评论中对此问题的直接答案。我尽量不去做,但我认为最好回复一下。

可以使用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,它们通常胜过Google。

© www.soinside.com 2019 - 2024. All rights reserved.