Elasticsearch-rails: Mapping for a multi-field query?

Created on 13 Mar 2017  路  2Comments  路  Source: elastic/elasticsearch-rails

I tried multiple ways to get my mapping

{
   "mappings": {
      "tweet": {
         "properties": {
            "text": {
              "type": "string"
            },
            "location": {
              "type": "geo_point"
            },
            "keywords": {
               "type": "string",
               "fields": {
                  "raw": {
                     "type": "string",
                     "index": "not_analyzed"
                  }
               }
            },
            "hashtags": {
                "type": "string",
                "fields": {
                    "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
         }
      }
   }
}

to work but it kind of is confusing to setup multi field mapping. Could someone please help me in translating this to a mapping block for ActiveRecord models.

Rails 4.2.6
Elastic search 5.2.2
Ruby 2.2.6

Thanks

Most helpful comment

You have to use the indexes method inside the mapping definition in the model -- please have a look at the examples in this repository, and also the example Rails applications which you can generate.

require 'active_record'
require 'oj'

require 'elasticsearch/model'

ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )

ActiveRecord::Schema.define(version: 1) do
  create_table :tweets do |t|
    t.string :content
    t.timestamps
  end
end

class Tweet < ActiveRecord::Base
  include Elasticsearch::Model

  mapping do
    indexes :text, type: 'text'
    indexes :location, type: 'geo_point'
    indexes :keywords, type: 'text' do
      indexes :raw, type: 'keyword'
    end
    indexes :hashtags, type: 'text' do
      indexes :raw, type: 'keyword'
    end
  end
end

puts MultiJson.dump(Tweet.mapping.to_hash, pretty: true)

Output:

{
  "tweet":{
    "properties":{
      "text":{
        "type":"text"
      },
      "location":{
        "type":"geo_point"
      },
      "keywords":{
        "type":"text",
        "fields":{
          "raw":{
            "type":"keyword"
          }
        }
      },
      "hashtags":{
        "type":"text",
        "fields":{
          "raw":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

Does this help?

All 2 comments

You have to use the indexes method inside the mapping definition in the model -- please have a look at the examples in this repository, and also the example Rails applications which you can generate.

require 'active_record'
require 'oj'

require 'elasticsearch/model'

ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )

ActiveRecord::Schema.define(version: 1) do
  create_table :tweets do |t|
    t.string :content
    t.timestamps
  end
end

class Tweet < ActiveRecord::Base
  include Elasticsearch::Model

  mapping do
    indexes :text, type: 'text'
    indexes :location, type: 'geo_point'
    indexes :keywords, type: 'text' do
      indexes :raw, type: 'keyword'
    end
    indexes :hashtags, type: 'text' do
      indexes :raw, type: 'keyword'
    end
  end
end

puts MultiJson.dump(Tweet.mapping.to_hash, pretty: true)

Output:

{
  "tweet":{
    "properties":{
      "text":{
        "type":"text"
      },
      "location":{
        "type":"geo_point"
      },
      "keywords":{
        "type":"text",
        "fields":{
          "raw":{
            "type":"keyword"
          }
        }
      },
      "hashtags":{
        "type":"text",
        "fields":{
          "raw":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

Does this help?

Closing as the previous comment answers the question.

Was this page helpful?
0 / 5 - 0 ratings