Giter Site home page Giter Site logo

elasticsearch-model-globalize's Introduction

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|
  "#{name}_#{locale}"
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_mapping as follows:

class Item < ActiveRecord::Base
  translates :name, :description
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::Globalize::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

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

elasticsearch-model-globalize's People

Contributors

kentaroi avatar iuriandreazza avatar florianeck avatar lionyu avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.