Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ElasticSearch model with fields

I'm using elasticsearch-rails and mongoid I have the following simple mapping with fields:

"title": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           }

My model looks like this:

class ArticlesEvent
include Mongoid::Document
include Elasticsearch::Model

field :title, type: String

attr_accessible :title

def as_indexed_json(options={})
  as_json(except: [:id, :_id])
end

Can anyone show an example how to define the rails model with the title.raw field, and how to access that field? Since the multi fields have been deprecated it's hard to find a working example with rails and mongoid.

Thanks

like image 283
Orr Avatar asked Jul 26 '15 21:07

Orr


1 Answers

You should be able to achieve this with the following attribute definition in your model:

attribute :title, String, mapping: { 
    fields: {
       raw:  { type: 'string', index: 'not_analyzed' }
    } 
}
like image 65
Val Avatar answered Sep 21 '22 18:09

Val