Skip to content

Conversation

@zoer
Copy link
Contributor

@zoer zoer commented Jul 10, 2014

I got issue with localized fields(mongoid), after migrating from tire gem.

class Book
  include Mongoid::Document
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  field :title, localized: true

  mapping do
    indexes :title, analyzer: "russian_morphology"
  end

  def as_indexed_json(opts={})
    {
      title: title
    }
  end
end

book = Book.create(title: "My book")
book.title = "My book 2"

book.changed_attributes # => {title: {ru: "My book 2"}}
book.as_indexed_json # => {title: "My book 2}

book.save
# Elasticsearch::Transport::Transport::Errors::BadRequest:
#       [400] {"error":"MapperParsingException[failed to parse [title]]; nested: ElasticsearchIllegalArgumentException[unknown property [ru]]; ","status":400}

Can we change this behavior a bit?

@karmi
Copy link
Contributor

karmi commented Jul 14, 2014

@zoer Thanks, looks legit, I'll need some time to look at it and be 100% sure what you're doing here. Ping me if I forget!

@karmi
Copy link
Contributor

karmi commented Jul 30, 2014

@zoer Out of curiosity, where does this: book.changed_attributes # => {title: {ru: "My book 2"}} come from? Ie. what makes the language "embedded" in the title?

@karmi
Copy link
Contributor

karmi commented Jul 30, 2014

@firien Yes, it will ignore alt_name, but that's a separate issue, and I believe issues/patches are already opened in this repository.

@zoer
Copy link
Contributor Author

zoer commented Jul 30, 2014

Sorry, bad example. That what I mean:

book = Book.create(title: "My book")
book.title = "My book 2"

book.save
# Elasticsearch::Transport::Transport::Errors::BadRequest:
#       [400] {"error":"MapperParsingException[failed to parse [title]]; nested: ElasticsearchIllegalArgumentException[unknown property [ru]]; ","status":400}

book.instance_variable_get(:@__changed_attributes) # => {title: {ru: "My book 2"}}
book.as_indexed_json # => {title: "My book 2}

karmi pushed a commit that referenced this pull request Jul 31, 2014
…_indexed_json`

Example of the problem:

    book = Book.create(title: "My book")
    book.title = "My book 2"

    book.save
    # Elasticsearch::Transport::Transport::Errors::BadRequest:
    #       [400] {"error":"MapperParsingException[failed to parse [title]]; nested: ElasticsearchIllegalArgumentException[unknown property [ru]]; ","status":400}

    book.instance_variable_get(:@__changed_attributes) # => {title: {ru: "My book 2"}}
    book.as_indexed_json # => {title: "My book 2}

Related: #166, #140
karmi added a commit that referenced this pull request Jul 31, 2014
…_document`

Basically, flipping the relationship here should be enough: select based on
`changed_attributes` keys from the `as_indexed_json` Hash, not vice versa.

Related: #166
@karmi
Copy link
Contributor

karmi commented Jul 31, 2014

@zoer I've actually found a simple way how to solve it, and that is "flipping the relationship" -- select data from as_indexed_json based on the keys of changed_attributes, not vice versa.

This means we don't need the complicate inject code.The test you've added pass. Can you verify that this works for your use case and we can close the issue?

@zoer
Copy link
Contributor Author

zoer commented Aug 23, 2014

Thx, that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants