简体   繁体   中英

How to set up routing in mapping with elasticsearch-rails

I'm trying to translate this mapping for an Item model using the tire gem to its equivalent with the elasticsearch-rails gem:

tire do
  mapping(
    _parent: { type: 'player' },
    _routing: { required: true, path: :user }
  ) do

  indexes :user, type: "string", index: :not_analyzed

end

My mapping in elasticsearch-rails is:

mapping(_parent: {type: 'player', require: 'true', _routing: {path: 'user', required: 'true'}}) do
  indexes :user, type: "string", index: not_analyzed
end

This seems okay until I try to import some players:

transform_function = lambda do |item|
{
  index: {
    _id: item.id,
    _parent: item.player_id,
    _routing: item.user_id,
    data: item.__elasticsearch__.as_indexed_json
  }
}

Item.__elasticsearch__.import \
    index: 'my_index', 
    batch_size: 100, 
    transform: transform_function

I get an error when I try to import stuff:

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] 
  {
    "error" : "MapperParsingException[The provided routing value [1] doesn't match the routing key stored in the document: [null]]",
    "status":400
  }

I don't know what that error means and I can't seem to find an applicable explanation online. Other people who experienced this error seem to be doing something different...

I think this is just an issue with how I'm declaring the mapping in elasticsearch-rails . Unfortunately, there isn't that much documentation around how this should look... Anyone have ideas?

I don't know who downvoted this question, but I think it's a totally useful question.

Turns out, when you don't need the path for this. You can actually just set the routing part up like this:

_routing: {type: 'user', required: 'true'})

I hope this helps someone.

To the person who downvoted this question: maybe instead of making this community unwelcoming without reason, add a comment to explain why the question is bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM