Question on indexing nested documents

Hi there, I have an index configured as:

{
  "analyzer": "lucene.standard",
  "searchAnalyzer": "lucene.standard",
  "mappings": {
    "dynamic": false,
    "fields": {
      "alternate_names": {
        "type": "string"
      },
      "platform": {
        "dynamic": false,
        "fields": {
          "_id": {
            "representation": "int32",
            "type": "number"
          },
          "alias": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        },
        "type": "document"
      },
      "reviewed": {
        "type": "boolean"
      },
      "title": {
        "type": "string"
      }
    }
  }
}

The index was working as expected before the addition of the platform nested document.

And here’s a sample document from the collection:

{
  "_id": 1868,
  "coop": "No",
  "platform": {
    "_id": 1,
    "alias": "PC",
    "name": "PC"
  },
  "publishers": [
    {
      "_id": 113,
      "keyword": "Accolade, Inc."
    }
  ],
  "title": "Test Drive II: The Duel",
  "reviewed": true,
  "references": 1,
  "slug": "pc/test-drive-ii-the-duel",

}

Running a sample search to filter by platform is not working however:

[
  {
    $search: {
      index: "name_idx",
      equals: {
        path: "platform._id",
        value: 1
      }
    }
  }
]

Not sure if I understand why this query is not working.

On the same subject, when creating the pipeline to search for the platform._id do I need to add one must entry for each platform selected by the user, or is there a in like operator?

Thank you

Hey @Vinicius_Carvalho1,

Welcome to the MongoDB Community Forums! :leaves:

When I tried to reproduce your mappings on my end, I got an error saying

Your index could not be built: 
"mappings.fields.platform.fields._id.representation" must be one of [double, int64].

ie. it does not accept int32 as a value. Hence, I would suggest you, either skip this field or edit the value.

I edited the value to test this and my mapping looked like this:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "platform": {
        "fields": {
          "_id": {
            "representation": "int64",
            "type": "number"
          },
          "alias": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        },
        "type": "document"
      },
      "reviewed": {
        "type": "boolean"
      },
      "title": {
        "type": "string"
      }
    }
  }
}

Used the sample search you provided

{
  index: 'default',
  equals: {
    value: 1,
    path: 'platform._id'
  }

and I got the sample document as the output.

I suggest exploring compound operators with equals to try and achieve this.

Please let us know if there’s anything else you need help with. Please feel free to reach out for anything else as well.

Regards,
Satyam

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.