jbdemonte / mongoose-elasticsearch-xp

A mongoose plugin that indexes models into Elasticsearch 2 / 5 / 6 and 7
93 stars 34 forks source link

Indexing _id field of nested objects #57

Open botoxparty opened 5 years ago

botoxparty commented 5 years ago

Hi,

I'm having trouble mapping the _id field of nested objects to elastic search.

For example.

This will map the _id field for assets

const personSchema = new mongoose.Schema({
  name: String,
  age: Number,
  assets: Object
});

Where as this will not...


const assetsSchema = new mongoose.Schema({
  cash: Number,
  property: Number
});

const person = new mongoose.Schema({
  name: String,
  age: Number,
  assets: assetsSchema
});

I'm stumped!

jbdemonte commented 5 years ago

If you don't want mongoose to add an id to the sub schema, you have to explicitly ask for it with:

const assetsSchema = new mongoose.Schema({
  _id: false,
  cash: Number,
  property: Number
});

doc on https://mongoosejs.com/docs/guide.html#id

If you don't get it, I supposed you may have forced it in a way (default config?) btw, this is not a mexp issue, you should ask for it on mongoose tracker

botoxparty commented 5 years ago

I'm not sure why you closed this. I would say it is an issue with mexp as the exact same setup with mongoosastic does infact add the _id field.

The _id field is always written to the database, but it's not synced to elastic search if it's a nested document using mexp. mongoosastic works fine.

jbdemonte commented 5 years ago

My bad, I though you were talking about the _id fields in the mongo db

I do not remember if non explicit ids in sub object were handle automatically, maybe @nodkz may answer