Robin
Robin

Reputation: 21884

Elasticsearch mapping does not include attributes from Rails key-value store

Model:

class Item
    store :socket_store, accessors: [:sockets, :socket_count, :socket_link_count]
end

Mapping:

mapping do
  indexes :id,                    key: "value", index: :not_analyzed
  indexes :sockets,               type: "object"
  indexes :socket_count,          type: "integer"
  indexes :socket_link_count,     type: "integer"
end

But here's what my actual index looks like:

socket_cout and the othe 2 attributes are not at the root of the mapping, as if they were completely ignored.

I know I could create methods with a different name and add them to the to_indexed_json, but I would have guessed it should work as is.

Upvotes: 1

Views: 290

Answers (1)

Robin
Robin

Reputation: 21884

Ok, I got it.

Since the accessors of the key-value store are not actual attributes on the Item model, they need to be added as methods in the to_indexed_json method:

def to_indexed_json
    to_json(include: [:stats], exclude: [:sockets_store], methods: [:socket_count, :socket_link_count, :sockets])
end

And that's it!

Upvotes: 1

Related Questions