dongtangu8403 2017-11-06 05:37
浏览 154

使用mgo使用Golang和MongoDB实现全文搜索

I am trying to print the 6 objects I know contain my search term. I am able to see the query array of the right length/size/capacity. But when I iterate the array and print, they do not contain any information.

My initial reaction is that maybe I have the wrong interface for my results.

code snippet:

var sTerm = "Google"
index := mgo.Index{
    Key: []string{"product.maker", "product.product"},
}

err = col.EnsureIndex(index)
//err = col.EnsureIndexKey("product.maker")
check(err)

//query := col.Find(bson.M{"$text": bson.M{"$search": sTerm}})
query := col.Find(bson.M{"product.maker": sTerm})
//query := col.Find(bson.M{"product.maker": &bson.RegEx{Pattern: sTerm, Options: "i"}})
fmt.Println(query)

var res []struct{ Value int }
err = query.All(&res)
check(err)
fmt.Println(res)
for i := 0; i < len(res); i++ {
    fmt.Println(res[i])
}

output:

&{{0 0} 0xc042056d00 {{devbase.devices map[product.maker:Google] 0 0 <nil> 0 <nil> 0 {<nil> <nil> <nil> false false [] 0 0 } false []} 0.25 0}}
[{0} {0} {0} {0} {0} {0}]
{0}
{0}
{0}
{0}
{0}
{0}

For now, I am hardcoding the search term, I have already set up a http handler function for the response and a Gorilla mux.

  • 写回答

1条回答 默认 最新

  • doulao5916 2017-11-06 05:46
    关注

    NEVER MIND!

    var res = []bson.M{}
    

    I know I know... I will go shoot myself now. haha! Thanks anyways!

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据