Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: tags_with_weight for subsets #18

Open
airy opened this issue Jun 2, 2011 · 5 comments
Open

Feature Request: tags_with_weight for subsets #18

airy opened this issue Jun 2, 2011 · 5 comments

Comments

@airy
Copy link

airy commented Jun 2, 2011

Post.limit(10).tags_with_weight and Post.tags_with_weight returns the same results. Having filtered tags_with_weight would be realy nice.

thanks

@wilkerlucio
Copy link
Owner

Hi airy,

Yes, they return the same... You can't limit or sort or do any of these things for this method, because it uses a low level collection for this data, in fact, after each save this data is cached on an internal collection for that.

If you can send a good implementation that covers that and keeps performance, I will be happy to accept :)

@airy
Copy link
Author

airy commented Jun 2, 2011

thanks for your confidence but i am not good enough on mongodb to write implementation :)

i found some usefull information here : http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Overview

there is a query option as well as limit and scope.

@wilkerlucio
Copy link
Owner

In fact, the cached table data is created using the MapReduce, you can take a look here: https://github.com/wilkerlucio/mongoid_taggable/blob/master/lib/mongoid/taggable.rb#L78

It should not be so hard to finish the implementation of query methods to suporte the context, but need to dig a little on MongoId source to see how internally you can use the current search context.

Currently Im most dedicated on other projects, but if you have some time, I mean read the MongoId source and do this implementation will be a good learning experience :)

@hayksaakian
Copy link

any progress on this front?

scoped/contextual tags would be really convenient

+1

@loicginoux
Copy link

For now you can use the aggregate method from mongoid. This works for me:

# you define here your query
criteria = YourModel.where(my_attribute: "a value")
# and you count the tags
result = YourModel.collection.aggregate([
    { "$match" => criteria.selector},
    { "$project" => YourModel.only(:tags_array).options.fields},
    { "$unwind" => "$tags_array"},
    { "$group" =>  { "_id" => "$tags_array", 
      "count" => { "$sum" => 1 }
    }}
  ])

p results # print [{"_id"=>"bee", "count"=>1},  {"_id"=>"ant", "count"=>1},  {"_id"=>"food", "count"=>2}]

and you can add a sorting at the end of the pipeline if you want them by counts:

{ "$sort" => {"count" => -1,"_id" => 1}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants