karmi / retire

A rich Ruby API and DSL for the Elasticsearch search engine
http://karmi.github.com/retire/
MIT License
1.87k stars 533 forks source link

issue with multiple shards and parent / child #883

Open apneadiving opened 10 years ago

apneadiving commented 10 years ago

Hi and thanks for your gem!

I'm running across an issue using parent/children and multiple shards.

The error I get is:

{"error"=>"RoutingMissingException[routing is required for [users]/[timeline_post]/[1]]", "status"=>500}

The logged query is:

curl -X POST "http://foo.qbox.io/users/timeline_post/1?parent=23" -d '{"id":1,"body":"foo"}'

It works fine locally but I haven't several shards on my dev environment.

Here is my code:

In my initializer:

Tire::Configuration.client.put( "#{Tire::Configuration.url}/users/timeline_post/_mapping",
  TimelinePost.mapping_to_hash.to_json)

The child, timeline_post:

class TimelinePost < ActiveRecord::Base
  include Tire::Model::Search

  after_save     { |model| model.update_tire_index }
  after_destroy { |model| model.remove_from_tire_index }

  tire do
    index_name    "users"
    document_type "timeline_post"

    # Tire elastic search mappings
    mapping _parent: { type: 'user' } do
      indexes :id,         type: :integer, index: :not_analyzed
      indexes :body,       type: :string,  analyzer: :snowball
    end
  end

  def update_tire_index
    return unless user_id
    index.store self, parent: user_id
  end

  def remove_from_tire_index
    return unless user_id
    index.remove self, parent: user_id
  end
end

The parent, user:

class User < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire do
    index_name      "users"
    document_type "user"

    # Tire elastic search mappings
    mapping do
      indexes :id,               type: :integer, index: :not_analyzed
      indexes :email,          type: :string, analyzer: :keyword
      indexes :name,          type: :string, analyzer: :snowball
    end
  end
end

What should I do to solve the routing issue? would it affect my search?

Thanks in advance,

Ben

apneadiving commented 10 years ago

Mmm, their was an issue on qbox side + it seems POST isn't legit in this case, tire should do a PUT.

I will dig this.

apneadiving commented 10 years ago

So ES doc states:

parents & children A child document can be indexed by specifying it’s parent when indexing. For example:

$ curl -XPUT localhost:9200/blogs/blog_tag/1122?parent=1111 -d '{ "tag" : "something" }'

so its a put not a post.

Should I send a pull request?

apneadiving commented 10 years ago

Ok so I did this and it now works like a charm:

https://github.com/apneadiving/retire/commit/a8a06e90014649f81a7cd846df0b7b68be28f6e8

Should I do a PR?

apneadiving commented 10 years ago

Anyone?

Seems like something useful for parent handling, too bad it's not shared

gottfrois commented 10 years ago

+1