matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
436 stars 68 forks source link

I just stumbled upon this package: is it meant for joint collection-search or only for searching 1 collection? #517

Closed thebarty closed 7 years ago

thebarty commented 8 years ago

Hi guys,

I just stumbled upon this package and the api looks really promising.

I'd be really thankful about a quick tip, if this package would support my usecase.

My UseCase can be compared to a simple Post / Comments / Author relationship: A Post is related to multiple comments, plus has an author. Basically I'd like to show a list of posts and give the user the option to search by the following:

Can you quickly tell me if this package supports searching?

This is how the collections are hooked up (using SimpleSchema). Plus I also attached my current publication where I join the data for the client.

collections

export const Posts = new Mongo.Collection('posts')
export const Comments = new Mongo.Collection('comments')
export const Authors =  new Mongo.Collection('authors')

Authors.attachSchema(new SimpleSchema({
  author: {
    type: String,
  },
}))
Comments.attachSchema(new SimpleSchema({
  comment: {
    type: String,
  },
  postId: {
    type: String,
    optional: true,
  },
}))
Posts.attachSchema(new SimpleSchema({
  post: {
    type: String,
  },
  authorId: {
    type: String,
    optional: true,
  },
  commentIds: {
    type: [String],
    optional: true,
  },
}))

publication

// peerlibrary:meteor-reactive-publish
// it is NOT working as expected!
Meteor.publish('all-posts-with-comments-and-authors', function () {
  this.autorun(function (computation) {
    const posts = Posts.find({})

    // aggregate authors and comments
    const authorIds = []
    let commentIds = []
    posts.forEach(function (post) {
      authorIds.push(post.authorId)
      commentIds = _.union(commentIds, post.commentIds)
    })
    const authors = Authors.find({ _id: { $in: authorIds } })
    const comments = Comments.find({ _id: { $in: commentIds } })

    // return multiple cursors
    return [posts, authors, comments]
  })
})
matteodem commented 8 years ago

This question has already come up quite a few times and I might add some documentation on how to structure searchable data. Basically the idea is that you have your data denormalized, so that there's no need for joining anymore.

matteodem commented 7 years ago

The first recipe should be of help.