9

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

4
  • 1
    Note that multi fields are not deprecated, they are well and alive. What was deprecated in version 1.0 was the multi-field type, which can now simply be a string type with an embedded fields structure, i.e. exactly what you have above. Commented Jul 27, 2015 at 2:56
  • ok thanks, do you have a rails example of that? Commented Jul 27, 2015 at 7:25
  • Yes, it's shown in my answer below. Commented Aug 6, 2015 at 11:18
  • See my comment, it throws an exception.... Commented Aug 9, 2015 at 20:20

3 Answers 3

2
+50

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' }
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using Mongoid, and this code throws an exception: "undefined method `attribute' for ArticlesEvent:Class"
0

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

Comments

0

You can define below mapping in your model

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.