I have an Activity model and ActivityOccurrence Model where Activity has_many :activity_occurrences
Activity
: This model will have all the meta data required by ActivityOccurrence
AcitvityOccurrence
: attrs - occurrence(datetime), completed.
Now we have new requirement where we have to show all occurrences of activity in search results when user searches for activities in a particular range.
Previously we used to show only one record in case of repeating activities.
So as per new requirement we have decided to move search from Activity
to ActivityOccurrence
.
Now, I don't want to index the Meta information of Activity
in each of my ActivityOccurrence as my activity has 10 fields more than ActivityOccurrence
,
eg:
if I have Activity
with 1000 AcitivityOccurrence
then I will be indexing all my activity informations in 1000 AcitivityOccurrence
records.
This will take huge space as app grows if we index this way
Hence, my major concern is the amount of indexing I have to do.
So I am thinking to avoid activity indexes in ActivityOccurrence
.
So is there a way to search Activity based on its filters first and then search ActivityOccurrence
in the range based on the results from activities?
Note: Also we have never ending occurrences.
Any ideas?
Thanks in advance.
Unless you're dealing with millions of Activities/Occurrences, this may be a premature optimization - space is cheap, and SOLR is fast. Looking at this the other way around, have you considered just indexing a list of the activity occurrences that pertain to each activity (using callbacks to ensure that it gets updated)?It's hard to really optimization without more info about your data access patterns, but I'm never a fan of doing more round-trips than necessary.
That said, while I'm not sure how to write a pure SOLR query to do this, you can do it with Sunspot pretty easily:
Make sure that ActivityOccurence
is searchable by Activity
easily (i.e. by Activity ID).
Search Activity
for the metadata that you want, and use this to extract the ID's that are relevant:
search = Activity.solr_search {<some block that does what you want>}
activity_ids = search.hits.map { |hit| hit.primary_key.to_i }
Now you can just add a with parameter to your ActivityOccurence
search block:
with(:activity_id, activity_ids)
This will limit the search to the occurrences for those activities. Note that you are trading off search-time performance for index efficiency with this.