如何使用golang和mgo库在mongodb中创建文本索引? [英] How do I create a text index in mongodb with golang and the mgo library?

查看:1091
本文介绍了如何使用golang和mgo库在mongodb中创建文本索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在集合上进行全文搜索,但为了做到这一点,我需要创建一个文本索引( http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/

mgo库提供了一个 EnsureIndex()函数,但它只接受一段字符串作为关键字。我试着将索引写成字符串: {name:text,about:text} 并将它传递给该函数,但它不起作用。

我也设法在mongo shell中手动创建索引,但我真的希望将索引记录在我的go项目中。这可能吗?在此先感谢!

解决方案

驱动程序支持此功能。您只需要将您的字段定义为text,如 $ text:field



在完整的列表中:

  import(
labix.org/v2/mgo

$ b $ 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.EureureIndex(index)
if err!= nil {
panic(err)
}


$ / code>

当从mongo shell中查看时会得到: p>

 > 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,
权重:{
about:1,
name:1
},
default_language :english,
language_override:language,
textIndexVersion:2
}
]


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!

解决方案

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:

> 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
    }
]

这篇关于如何使用golang和mgo库在mongodb中创建文本索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆