如何在Mongodb中使用全文本搜索来查找字段的未知数目的子代? [英] How to use full text search for unknown number of children of a field in Mongodb?

查看:59
本文介绍了如何在Mongodb中使用全文本搜索来查找字段的未知数目的子代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档,其中包含一个字段description,如下所示:

I have a document with one field description like this:

{
  "_id": "item0",
  "description": {
    "parlist": [
      {
        "listitem": {
          "text": {
            "child": "page rous lady",
            "keyword": "officer e"
          }
        }
      },
      {
        "listitem": {
          "text": "shepherd noble "
        }
      }
    ]
  }
}

如何在description上创建文本索引并搜索特定单词?我不知道description的深度如何,将描述多少个孩子.我尝试过这样的索引创建:

How to create text index on description and search for specific word? I don't know how depth can description go and how many children will description have. I tried with index creation like this:

db.collection.ensureIndex({description:"text"})

,然后进行如下查询:

db.collection.runCommand("text",{$search:"shepherd"})

但是它不起作用.

推荐答案

文本索引不适用于完整的子文档,但是您可以创建包含多个字段的文本索引:

Text-indexes do not work on full sub-documents, but you can create a text index which includes more than one field:

db.collection.ensureIndex(
     {
         "description.parlist.listitem.text": "text",
         "description.parlist.listitem.text.child": "text",
         "description.parlist.listitem.text.keyword": "text"
     }
)

这篇关于如何在Mongodb中使用全文本搜索来查找字段的未知数目的子代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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