3

I have a collection that has a text index like this:

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

The collection's documents have many text data blocks. I want to exclude some of them in order to not get results that includes text matched from the excluded blocks.

However, I don't want to define each field in the text index like below in order to exclude the, for example, NotTextSearchableBlock block:

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

Here is a document example:

{
      "_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"
          }
        ]
      }
}
Community
  • 1
  • 1

1 Answers1

1

To answer your question, there is no documented way to exclude certain fields from a text index. (See Text Indexes in mongodb's documention.)

As you already know:

When establishing a text index, you have to identify the specific text field(s) to index. The field must be a string or an array of string elements.

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

You may also index all text found within a collection's documents by using the wildcard specifier $**.

db.mycollection.createIndex(
   { "$**" : "text" },
   { name : "mytextindex" }
)
whyceewhite
  • 6,317
  • 7
  • 43
  • 51
  • 1
    I think it should be an enhancement for this feature of mongo, because some documents can has a lot of text, so you could want, like me, but only avoid some sub set of the document, i think I have to exclude this block in the search query, not by the text index – Mathias Valencia Rojo Aug 07 '15 at 21:36
  • This kind of sucks. mongo will allow creation of a wildcard index, but since their is a unique index on userId it will not allow such an index. db.users.createIndex( { firstName: "text", "lastName": "text", "userId": "text", "address.zip": "text" }, {default_language: "english" }) – Matthew Payne Sep 22 '16 at 21:04
  • 1
    The annoyance for me is that once the text index is created you have to search the entire bloody thing and are unable to specify the field you wish to query... Also it seems you can only create 1 text index. I'm wondering if its possible to manipulate the results afterward using the weighting system but this feels like a dirty solution –  Oct 16 '16 at 17:17