问题
I'm using sunspot-solr in ROR and I need help in creating a searchable block using two tables.(join of two tables) The query I want to be executed when the indexes are formed is :
SELECT a.id,a.title
FROM table_one a,table_two b
WHERE a.status=1
AND a.id=b.id
AND b.status=1
I want the "title" field to be searchable(text), only if the id exists in both tables and both have status 1.And I want them to be stored fields(no db hits).
class TableOne
has_many :table_twos
searchable do
text :title, :stored => true
string :status, :stored => true
string :id, :multiple => true, :stored => true do
table_twos.map(&:id)
end
end
When I searched a word, I got 5 results.
But when I delete an entry of one of the results from table_two and searched the same word again.. I still got 5 results when I should get only the other 4.
Any help ?
回答1:
If you delete an associated record that was stored as a solr/sunspot record, you will have no choice but to reindex that record.
回答2:
So to solve the issue I did somthing like without(:id,nil)
in my controller and I got the results as I wanted them.
I'm not sure its the right way to go about it though.
来源:https://stackoverflow.com/questions/31512210/indexing-using-two-tables-in-sunspot-solr-rails-4