"Fossies" - the Fresh Open Source Software Archive

Member "elasticsearch-6.8.23/docs/ruby/model.asciidoc" (29 Dec 2021, 2039 Bytes) of package /linux/www/elasticsearch-6.8.23-src.tar.gz:

As a special service "Fossies" has tried to format the requested source page into HTML format (assuming AsciiDoc format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

ActiveModel / ActiveRecord

The elasticsearch-model Rubygem provides integration with Ruby domain objects ("models"), commonly found e.g. in Ruby on Rails applications.

It uses the elasticsearch Rubygem as the client communicating with the Elasticsearch cluster.

Features at a Glance

  • ActiveModel integration with adapters for ActiveRecord and Mongoid

  • Enumerable-based wrapper for search results

  • ActiveRecord::Relation-based wrapper for returning search results as records

  • Convenience model methods such as search, mapping, import, etc

  • Support for Kaminari and WillPaginate pagination

  • Extension implemented via proxy object to shield model namespace from collisions

  • Convenience methods for (re)creating the index, setting up mappings, indexing documents, …​


Add the library to your Gemfile:

gem 'elasticsearch-rails'

Include the extension module in your model class:

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

Import some data and perform a search:


response = Article.search 'fox dog'
# => 3

It is possible to either return results as model instances, or decorated documents from Elasticsearch, with the records and results methods, respectively:

# Article Load (0.4ms)  SELECT "articles".* FROM "articles"  WHERE ...
=> #<Article id: 3, title: "Foo " ...>

# => 0.02250402

# => "Quick brown fox"

Please see the full documentation for more information.