Parent child query

35 views
Skip to first unread message

aash dhariya

unread,
Jan 19, 2013, 6:07:21 AM1/19/13
to elasti...@googlegroups.com

Here are my two elastictastic models
Post:

class Post
include Elastictastic::Document

field :content, type: 'string'
has_many comments, class_name: 'Comment'
end

Comment:

class Comment
include Elastictastic::Document

belongs_to :post, class_name: 'Post'

field :content, type: 'string'
end

Next, this is how I save my documents:

p = Post.new
p.content = 'aash'

c.content = 'hello'
c.save

p.save

Now, on querying 

Post.query(has_child: {type: 'comments', query: {term: {content: 'hello'}}})

I get the following error: 
Elastictastic::ServerError::QueryParsingException: [[development_posts] [has_child] No mapping for for type [comments]]; }]

Is there something wrong that I am doing here? If yes, then please give me directions on how to make parent child relationships using elastictastic documents and how to query them


-- 
Thanks, 
Aash

Matthew A. Brown

unread,
Jan 19, 2013, 11:51:37 AM1/19/13
to elasti...@googlegroups.com
I notice that "comments" is not quoted as a string or symbol in your Post definition. Does that reflect what's in your code?

Anywho, Elastictastic uses the singularized, underscored class name for the "type". Thus what you're looking for is {type: 'comment', ...}

Mat


--
 
 

aash dhariya

unread,
Jan 20, 2013, 5:45:39 AM1/20/13
to elasti...@googlegroups.com
1. About  the quotes, it was a typo when I wrote the code in the email
2. Even if I query this:

Post.query(has_child: {type: 'comment', query: {term: {content: 'hello'}}})

I get the same type of error:
Elastictastic::ServerError::QueryParsingException: [[development_posts] [has_child] No mapping for for type [comment]]; }]



--
 
 



--
Thanks, 
Aash

Mat Brown

unread,
Jan 20, 2013, 9:08:57 AM1/20/13
to elasti...@googlegroups.com
Have you run:

    Post.sync_mapping
    Comment.sync_mapping

?
--
 
 

aash dhariya

unread,
Jan 20, 2013, 10:42:10 AM1/20/13
to elasti...@googlegroups.com
Ok, I found the problem. When I execute

Comment.sync_mapping

it throws me the following error:

Elastictastic::ServerError::IndexMissingException: [[development_comments] missing]

As parent and child have the same index, I changed the index name of Comment model same as parent. Now its working correct. 

The elastictastic gem that I use is customized such that depending on the rails environment, the term development, staging or production is appended to the index_name. I suspect, in doing these changes I would have broken the parent child index name creation. I will soon look into the code and fix it

Thanks a lot for your help
 


--
 
 



--
Thanks, 
Aash
Reply all
Reply to author
Forward
0 new messages