retire
retire copied to clipboard
index profile model, how to add attributes from different model to the elasticsearch index?
Digged the docs to no avail so far.
My problem is I have a Profile.rb model and use Tire with elasticsearch to index all fields. Now I also have a match model that belongs to Profile model and I would like to add some attributes from match model into my tire/elasticsearch searchable index. How to add those match attributes so that Tire/ Elastic will index them?
This fails also, who knows how to do this?
Profile.rb:
accepts_nested_attributes_for :match
delegate :looking_for_skillset, :to => :match
mapping do
indexes :id, type: "integer"
indexes :looking_for_skillset, type: "string"
end
@rubytastic Have you looked here http://stackoverflow.com/questions/11692560/elasticsearch-tire-and-nested-queries-associations-with-activerecord/11711477#11711477 ? There's info and full code for indexing associations.
edit: Thank you Karmi for your fast reply. I tried those suggestions but seem to have still some problems. With Elasticsearch-head Browsing the contents I can see the fields are now listed but not with skillset value.
My added tire code for profile and match models is below, do you see my mistake? Followed instructions but stil fail :(
Profile.rb
has_one :match
after_touch() { tire.update_index }
self.include_root_in_json = false
include Tire::Model::Search
include Tire::Model::Callbacks
def to_indexed_json
to_json(include: {match: {only: [:text]}})
end
mapping do
indexes :id, type: "integer"
indexes :created_at, type: 'date'
indexes :match do
indexes :looking_for_skillset, type: 'string'
end
end
Match.rb
belongs_to :profile, touch: true
validates :profile_id, :uniqueness => true
With elasticsearch-head index browser html Im able to see the fields are added like "profile.match.looking_for_skillset" however there is not created an index and the search results return nothing ( I set 1 database column for skillset to value "student") this is not indexed however.
Using rails 3 latest as of now. Any thoughts on why this fails or perhaps what I did wrong? Kind regards
Hmm, this looks OK. Can you please pastie the output of Profile.to_indexed_json
? Also, instead of head, just look at the results directly, http://localhost:9200/products/_search?pretty.
however there is not created an index and the search results return nothing
Can you elaborate on this? I don't understand.
The output of p.to_indexed_json from rails console:
2.0.0p0 :009 > p.to_indexed_json
Match Load (0.3ms) SELECT `matches`.* FROM `matches` WHERE `matches`.`profile_id` = 1 LIMIT 1
=> "{\"state\":\"3\",\"status\":\"student\",\"step\":0,\"updated_at\":\"2013-06-08T14:46:21+02:00\",\"match\":{}}"
2.0.0p0 :010 >
The output of localhost:9200/profiles/_search?pretty is:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "profiles",
"_type" : "profile",
"_id" : "1",
"_score" : 1.0, "_source" : {"state":"3","status":"student","step":0,"updated_at":"2013-06-08T14:46:21+02:00","match":{}}
}, {
"_index" : "profiles",
"_type" : "profile",
"_id" : "2",
"_score" : 1.0, "_source" : {"state":"5","status":"teacher","step":0,"updated_at":"2013-06-04T00:10:37+02:00","match":{}}
} ]
}
}
The skillset column seems not to be indexed. Thank you for your help.
Have not been able to find the related issue on this, anyone any idea what might cause this?
What about your to_indexed_json
? to_json(include: {match: {only: [:text]}})
?