mongoolia icon indicating copy to clipboard operation
mongoolia copied to clipboard

Indexing Referenced Schemas

Open johnernaut opened this issue 6 years ago • 3 comments

I have a mongoose schema that references a 'User' schema. I'd like certain fields to be indexed into Algolia like the users name, and a few other fields, but the problem is that all of the attributes within user are being sent over to Algolia, including things like the hashed password (which I don't want indexed). Is it currently possible to tell Mongolia to not index certain fields within referenced documents?

const EventSchema = new mongoose.Schema({
  name: { type: String, required: true, algoliaIndex: true },
  start_at: { type: Date, required: true, algoliaIndex: true },
  end_at: { type: Date, required: true, algoliaIndex: true },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    index: true,
    algoliaIndex: true
  }
});

johnernaut avatar Apr 10 '18 13:04 johnernaut

Hi @johnernaut have you tried to specify algoliaIndex: false to the attributes you don't want to send to Algolia?

iam4x avatar Apr 10 '18 13:04 iam4x

@iam4x Yes, I put algoliaIndex: false in the user schema, but it still indexes in algolia. I think the issue is that the document object in the post save hook contains all of the properties of the user, and in the code, you're only looking for algoliaIndex on the top level document (where as the user is nested within that document) https://github.com/algolia/mongoolia/blob/master/src/index.js#L41

I think it's worth having functionality that searches through nested documents for that property so that it's not indexed though. I'd be happy to make an attempt at a PR if you agree.

johnernaut avatar Apr 10 '18 13:04 johnernaut

Yes if you are willing to make a PR you can! I'll be happy to help you if have questions 👍

Thank you!

iam4x avatar Apr 11 '18 14:04 iam4x