Orr
Orr

Reputation: 4840

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

Upvotes: 9

Views: 1565

Answers (3)

gaurav makwana
gaurav makwana

Reputation: 680

You can define below mapping in your model

indexes :name_of_field,type: :keyword, fields: {
    raw: {
      type: :text
    }
  }

Upvotes: 0

Orr
Orr

Reputation: 4840

Looks like this is still an open issue with elasticsearch-rails: https://github.com/elastic/elasticsearch-rails/issues/79

Upvotes: 0

Val
Val

Reputation: 217314

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' }
    } 
}

Upvotes: 2

Related Questions