New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

elasticsearch-model-globalize

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elasticsearch-model-globalize

0.0.3
Rubygems
Version published
Maintainers
1
Created
Source

Elasticsearch::Model::Globalize

elasticsearch-model-globalize library allows you to use elasticsearch-model [LICENSE] with Globalize [LICENSE].

Installation

Add this line to your application's Gemfile:

gem 'elasticsearch-model-globalize'

And then execute:

$ bundle

Or install it yourself as:

$ gem install elasticsearch-model-globalize

Usage

There are a few ways to use elasticsearch-model with Globalize.

This gem has two modules, one is MultipleFields and the other is OneIndexPerLanguage. You can choose one for each model to fit your needs by simply including a module.

class Item < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::Globalize::MultipleFields
  # Or
  # include Elasticsearch::Model::Globalize::OneIndexPerLanguage
end

MultipleFields

MultipleFields module creates additional fields for each language. For example, you have a model like:

class Item < ActiveRecord::Base
  translates :name, :description
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::Globalize::MultipleFields
end

and you have :en and :ja for available locales.

MultipleFields module creates name_en, name_ja, description_en and description_ja field and stores these fields instead of name and description fields into Elasticsearch.

You can customize the way to localize field names.

# Put this in config/initializers/elasticsearch-model-globalize.rb
Elasticsearch::Model::Globalize::MultipleFields.localized_name do |name, locale|
  "#{locale}_#{name}"
end

The way to define mappings and as_indexed_json is same as elasticsearch-model without this library.

class Item < ActiveRecord::Base
  translates :name, :description
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::Globalize::MultipleFields

  mapping do
    indexes :id,             type: 'integer'
    indexes :name_ja,        analyzer: 'kuromoji'
    indexes :name_en,        analyzer: 'snowball'
    indexes :description_ja, analyzer: 'kuromoji'
    indexes :description_en, analyzer: 'snowball'
  end

  def as_indexed_json(options={})
    { id: id,
      name_ja: name_ja,
      name_en: name_en,
      description_ja: description_ja,
      description_en: description_en }
  end
end

Getters and setters of the derived fields are automatically added by MultipleFields module.

I18n.locale = :en
item = Item.new
item.name_ja = '輪ゴム'
item.name_ja # => "輪ゴム"
item.name # => nil
Globalize.with_locale(:ja) { item.name } # => "輪ゴム"
item.name = 'elastic'
item.name # => "elastic"
item.name_en # => "elastic"

One thing you have to care about is that put translates line before including Elasticsearch::Model::Globalize::MultipleFields, otherwise MultipleFields module are not able to know from which fields to derive localized fields.

OneIndexPerLanguage

OneIndexPerLanguage module creates one index per language. For example, you have a model like:

class Item < ActiveRecord::Base
  translates :name, :description
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::Globalize::OneIndexPerLanguage
end

and you have :en and :ja for available locales,

OneIndexPerLanguage module creates items-en and items-ja indices.

You can customize the way to localize index names and document types.

# Put this in config/initializers/elasticsearch-model-globalize.rb
Elasticsearch::Model::Globalize::OneIndexPerLanguage.localized_name do |name, locale|
  "#{locale}-#{name}"
end

You can use index_name_base and documenty_type_base in addition to index_name and document_type.

You can define mappings using globalized_maping as follows:

class Item < ActiveRecord::Base
  translates :name, :description
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::OneIndexPerLanguage

  globalized_mapping do |locale|
    analyzer = locale == :ja ? 'kuromoji' : 'snowball'

    indexes :id,          type: 'integer'
    indexes :name,        analyzer: analyzer
    indexes :description, analyzer: analyzer
  end
end

Development and Community

For local development, clone the repository and run bundle install.

To run all tests against a test Elasticsearch cluster, use a command like this:

curl -# https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.1.tar.gz | tar xz -C tmp/
SERVER=start TEST_CLUSTER_COMMAND=$PWD/tmp/elasticsearch-1.2.1/bin/elasticsearch bundle exec rake test

Contributing

  • Fork it ( https://github.com/kentaroi/elasticsearch-model-globalize/fork )
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

FAQs

Package last updated on 14 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts