How do I create a text index in mongodb with golang and the mgo library?

huangapple go评论102阅读模式
英文:

How do I create a text index in mongodb with golang and the mgo library?

问题

我正在尝试在一个集合上进行全文搜索,但是为了实现这个目标,我需要创建一个文本索引(http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/)。

mgo库提供了一个EnsureIndex()函数,但是它只接受一个字符串切片作为键。我尝试将索引直接写成字符串形式:{ name: "text", about: "text" },然后将其传递给该函数,但是没有成功。

我也尝试在mongo shell中手动创建索引,但是我真的希望在我的go项目中有文档记录这个索引。这种可能性存在吗?提前感谢!

英文:

I'm trying to do a full text search on a collection, but in order to do that I need to create a text index (http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/)

The mgo library provides an EnsureIndex() function however, it only accepts a slice of strings as a key. I tried just writing the index out as a string: { name: "text", about: "text" } and passing it to that function but it didn't work.

I've also managed to manually create the index in the mongo shell but I'd really like to have the index documented in my go project. Is this possible? Thanks in advance!

答案1

得分: 17

这在驱动程序中是支持的。您只需要将要索引的字段定义为“text”,如$text:field

完整的代码示例如下:

import (
  "labix.org/v2/mgo"
)

func main() {
  session, err := mgo.Dial("127.0.0.1")
  if err != nil {
    panic(err)
  }

  defer session.Close()

  session.SetMode(mgo.Monotonic, true)

  c := session.DB("test").C("texty")

  index := mgo.Index{
    Key: []string{"$text:name", "$text:about"},
  }

  err = c.EnsureIndex(index)
  if err != nil {
    panic(err)
  }
}

在mongo shell中查看时,将显示如下结果:

> db.texty.getIndices()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.texty"
    },
    {
        "v" : 1,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "name_text_about_text",
        "ns" : "test.texty",
        "weights" : {
            "about" : 1,
            "name" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 2
    }
]
英文:

This is supported in the driver. All you need to do is define your fields to be indexed as "text" as in $text:field.

In a complete listing:

import (
  "labix.org/v2/mgo"
)

func main() {

  session, err := mgo.Dial("127.0.0.1")
  if err != nil {
    panic(err)
  }

  defer session.Close()

  session.SetMode(mgo.Monotonic, true)

  c := session.DB("test").C("texty")

  index := mgo.Index{
    Key: []string{"$text:name", "$text:about"},
  }

  err = c.EnsureIndex(index)
  if err != nil {
    panic(err)
  }

}

Which when viewed from the mongo shell will give:

<!-- language: lang-js -->

&gt; db.texty.getIndices()
[
    {
            &quot;v&quot; : 1,
            &quot;key&quot; : {
                    &quot;_id&quot; : 1
            },
            &quot;name&quot; : &quot;_id_&quot;,
            &quot;ns&quot; : &quot;test.texty&quot;
    },
    {
            &quot;v&quot; : 1,
            &quot;key&quot; : {
                    &quot;_fts&quot; : &quot;text&quot;,
                    &quot;_ftsx&quot; : 1
            },
            &quot;name&quot; : &quot;name_text_about_text&quot;,
            &quot;ns&quot; : &quot;test.texty&quot;,
            &quot;weights&quot; : {
                    &quot;about&quot; : 1,
                    &quot;name&quot; : 1
            },
            &quot;default_language&quot; : &quot;english&quot;,
            &quot;language_override&quot; : &quot;language&quot;,
            &quot;textIndexVersion&quot; : 2
    }
]

huangapple
  • 本文由 发表于 2014年7月6日 13:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/24592864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定