【问题标题】:Exclude some text fields in MongoDB text index排除 MongoDB 文本索引中的一些文本字段
【发布时间】:2015-08-07 15:33:53
【问题描述】:

我有一个具有如下文本索引的集合:

db.mycollection.createIndex({ "$**" : "text"},{ "name" : "AllTextIndex"}))

集合的文档有很多文本数据块。我想排除其中一些,以免得到包含与排除块匹配的文本的结果。

但是,我不想像下面那样定义文本索引中的每个字段,以便排除 NotTextSearchableBlock 块:

db.application.ensureIndex({
    "SearchableTextBlock1": "text",
    "SearchableTextBlock2": "text",
    "SearchableTextBlock3": "text"
})

这是一个文档示例:

{
      "_id": "e0832e2d-6fb3-47d8-af79-08628f1f0d84",
      "_class": "com.important.enterprise.acme",
      "fields": {
        "Organization": "testing"
      },
      "NotTextSearchableBlock": {
        "something": {
          "SomethingInside": {
           "Text":"no matcheable text"
          }
        }
      },
      "SearchableTextBlock1": {
        "someKey": "someTextCool"
      },
      "SearchableTextBlock2": {
        "_id": null,
        "fields": {
          "Status": "SomeText"
        }
      },
      "SearchableTextBlock3": {
        "SomeSubBlockArray": [
          {
            "someText": "Pepe"
          }
        ]
      }
}

【问题讨论】:

  • 这有什么更新吗?

标签: mongodb full-text-indexing nosql


【解决方案1】:

为了回答您的问题,没有记录方法可以排除文本索引中的某些字段。(请参阅 mongodb 文档中的 Text Indexes。)

如你所知:

在建立text 索引时,您必须确定要索引的特定文本字段。该字段必须是字符串或字符串元素的数组。

db.mycollection.createIndex({
    mystringfield : "text"
    mystringarray : "text"
})

您还可以使用通配符说明符 $** 为集合文档中的所有文本编制索引。

db.mycollection.createIndex(
   { "$**" : "text" },
   { name : "mytextindex" }
)

【讨论】:

  • 我认为这应该是对mongo这个特性的一个增强,因为有些文档可以有很多文本,所以你可以像我一样想要,但只避免文档的一些子集,我认为我必须在搜索查询中排除这个块,而不是通过文本索引
  • 这种很烂。 mongo 将允许创建通配符索引,但由于它们是 userId 上的唯一索引,因此不允许这样的索引。 db.users.createIndex( { firstName: "text", "lastName": "text", "userId": "text", "address.zip": "text" }, {default_language: "english" })跨度>
  • 让我烦恼的是,一旦创建了文本索引,您就必须搜索整个血腥的东西并且无法指定您要查询的字段...而且您似乎只能创建 1文字索引。我想知道之后是否可以使用加权系统来操纵结果,但这感觉像是一个肮脏的解决方案
猜你喜欢
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 2014-10-22
  • 2018-09-27
  • 1970-01-01
相关资源
最近更新 更多