File: README.md

package info (click to toggle)
ruby-elasticsearch-rails 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 4,420 kB
  • sloc: ruby: 1,661; makefile: 4
file content (149 lines) | stat: -rw-r--r-- 5,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Elasticsearch::Rails

The `elasticsearch-rails` library is a companion for the
the [`elasticsearch-model`](https://github.com/elastic/elasticsearch-rails/tree/main/elasticsearch-model)
library, providing features suitable for Ruby on Rails applications.

## Compatibility

This library is compatible with Ruby 1.9.3 and higher.

The library version numbers follow the Elasticsearch major versions, and the `main` branch
is compatible with the Elasticsearch `master` branch, therefore, with the next major version.

| Rubygem       |   | Elasticsearch |
|:-------------:|:-:| :-----------: |
| 0.1           | → | 1.x           |
| 2.x           | → | 2.x           |
| 5.x           | → | 5.x           |
| 6.x           | → | 6.x           |
| 7.x           | → | 7.x           |
| main          | → | master        |

## Installation

Install the package from [Rubygems](https://rubygems.org):

    gem install elasticsearch-rails

To use an unreleased version, either add it to your `Gemfile` for [Bundler](http://bundler.io):

    gem 'elasticsearch-rails', git: 'git://github.com/elastic/elasticsearch-rails.git', branch: '5.x'

or install it from a source code checkout:

    git clone https://github.com/elastic/elasticsearch-rails.git
    cd elasticsearch-rails/elasticsearch-rails
    bundle install
    rake install

## Features

### Rake Tasks

To facilitate importing data from your models into Elasticsearch, require the task definition in your application,
eg. in the `lib/tasks/elasticsearch.rake` file:

```ruby
require 'elasticsearch/rails/tasks/import'
```

To import the records from your `Article` model, run:

```bash
$ bundle exec rake environment elasticsearch:import:model CLASS='Article'
```

To limit the imported records to a certain
ActiveRecord [scope](http://guides.rubyonrails.org/active_record_querying.html#scopes),
pass it to the task:

```bash
$ bundle exec rake environment elasticsearch:import:model CLASS='Article' SCOPE='published'
```

Run this command to display usage instructions:

```bash
$ bundle exec rake -D elasticsearch
```

### ActiveSupport Instrumentation

To display information about the search request (duration, search definition) during development,
and to include the information in the Rails log file, require the component in your `application.rb` file:

```ruby
require 'elasticsearch/rails/instrumentation'
```

You should see an output like this in your application log in development environment:

    Article Search (321.3ms) { index: "articles", type: "article", body: { query: ... } }

Also, the total duration of the request to Elasticsearch is displayed in the Rails request breakdown:

    Completed 200 OK in 615ms (Views: 230.9ms | ActiveRecord: 0.0ms | Elasticsearch: 321.3ms)

There's a special component for the [Lograge](https://github.com/roidrage/lograge) logger.
Require the component in your `application.rb` file (and set `config.lograge.enabled`):

```ruby
require 'elasticsearch/rails/lograge'
```

You should see the duration of the request to Elasticsearch as part of each log event:

    method=GET path=/search ... status=200 duration=380.89 view=99.64 db=0.00 es=279.37

### Rails Application Templates

You can generate a fully working example Ruby on Rails application, with an `Article` model and a search form,
to play with (it generates the application skeleton and leaves you with a _Git_ repository to explore the
steps and the code) with the
[`01-basic.rb`](https://github.com/elastic/elasticsearch-rails/blob/main/elasticsearch-rails/lib/rails/templates/01-basic.rb) template:

```bash
rails new searchapp --skip --skip-bundle --template https://raw.github.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/01-basic.rb
```

Run the same command again, in the same folder, with the
[`02-pretty`](https://github.com/elastic/elasticsearch-rails/blob/main/elasticsearch-rails/lib/rails/templates/02-pretty.rb)
template to add features such as a custom `Article.search` method, result highlighting and
[_Bootstrap_](http://getbootstrap.com) integration:

```bash
rails new searchapp --skip --skip-bundle --template https://raw.github.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/02-pretty.rb
```

Run the same command with the [`03-expert.rb`](https://github.com/elastic/elasticsearch-rails/blob/main/elasticsearch-rails/lib/rails/templates/03-expert.rb)
template to refactor the application into a more complex use case,
with couple of hundreds of The New York Times articles as the example content.
The template will extract the Elasticsearch integration into a `Searchable` "concern" module,
define complex mapping, custom serialization, implement faceted navigation and suggestions as a part of
a complex query, and add a _Sidekiq_-based worker for updating the index in the background.

```bash
rails new searchapp --skip --skip-bundle --template https://raw.github.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/03-expert.rb
```

## License

This software is licensed under the Apache 2 license, quoted below.

    Licensed to Elasticsearch B.V. under one or more contributor
    license agreements. See the NOTICE file distributed with
    this work for additional information regarding copyright
    ownership. Elasticsearch B.V. licenses this file to you under
    the Apache License, Version 2.0 (the "License"); you may
    not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
    	http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.