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

support embedded documents #9

Open
abhishiv opened this issue Mar 19, 2011 · 7 comments
Open

support embedded documents #9

abhishiv opened this issue Mar 19, 2011 · 7 comments

Comments

@abhishiv
Copy link

I am having an hard time figuring out how to make this gem work with embedded documents. Any pointers?

Thanks a ton & great work!

@jwoodleeln
Copy link

abhishiv - did you ever figure this out? Im having the same dilema..

@cice
Copy link

cice commented Jun 24, 2011

+1 and ready to help out

@basharabdullah
Copy link

I've tried this and it worked to search for documents with embedded certain field values.

Say you have company with embedded departments, and you want to search for companies with certain department names.

searchable do

Your regular index

...

text :company_departments
end

def company_departments
departments.map(&:name).join(" ")
end

Hope that helps.

@berkes
Copy link

berkes commented Oct 12, 2011

@basharabdullah, while that works, It hardly scales. I will create a lot of duplication if your embedded documents have many fields. And needs quite some cruft in cases where embedded documents are optional (a user may have 0 to n jobs).

@cice
Copy link

cice commented Oct 12, 2011

@berkes could you elaborate that, to be honest, I've been doing it like @basharabdullah for a while now, but with considerably few documents and only a handful of users. (this particular application does not need to scale, it's just used internally, but nevertheless i'm interested in a 'good'/scaling solution.

@berkes
Copy link

berkes commented Oct 12, 2011

I took @basharabdullah's concept and made it a little more DRY:

My users can have zero or one drupal_profiles. Profiles should only be indexed (and searchable) once they are available and authorized. They contain many fields, two of which are illustrated below: a string and a list of items (Array).

searchable do
    text :drupal_username do
      drupal_profile_value(:drupal_username) do |field|
        field.to_s
      end
    end
    text :drupal_projects do
      drupal_profile_value(:projects) do |field|
        field.values.join(", ")
      end
    end
end

# ....
private 
  def drupal_profile_value(field)
    if (self.drupal_profile) && self.drupal_profile.authorized?)
      if self.drupal_profile.fields.include?(field.to_s)
        value = yield self.drupal_profile[field]
      end
    end
    value
   end

I am no Rails expert, so this could probably be refactored and cleaned even further, but the idea is that I call drupal_profile_value, and pass it a block that allows me to modify, change or return the value of the embedded field.

IMHO this still is not as ideal as a clean DSL that supports embedded documents, say

    text "drupal_profile.drupal_username"
    text "drupal_profile.drupal_projects" do
      drupal_profile.drupal_projects.values.join(", ")
    end

EDIT: @cice: with scaling I meant scaling towards more fields, more types of embedded documents and so on. I have a model with 7 types of embedded documents, that have a total of 160 fields. This means that I have to define ~160 methods in the parent model to index all the fields. Fields that are defined elsewhere.

@basharabdullah
Copy link

@berkes right you are, but it's the best solution I found. Thanks for providing more solid solution.

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

5 participants