Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review and update documentation #773

Merged
merged 38 commits into from
Mar 22, 2021
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6e60c0f
Add AR versions to compatibility matrix table
Vitalina-Vakulchyk Mar 15, 2021
5b1af1e
Change view of AR versions compatibility
Vitalina-Vakulchyk Mar 15, 2021
e85b4e1
Add explanation of Elasticsearch client options
Vitalina-Vakulchyk Mar 15, 2021
a4fc782
Update CLient.new instruction view
Vitalina-Vakulchyk Mar 15, 2021
dd872c8
Add link to search connection request section
Vitalina-Vakulchyk Mar 15, 2021
c95bb9c
Add to ES request connection section - link to code source in Readme
Vitalina-Vakulchyk Mar 16, 2021
00643fb
Restruct Readme Usage#ClientSettings section to be sure gem will work…
Vitalina-Vakulchyk Mar 16, 2021
107b684
Add GettingStarted section to Readme
Vitalina-Vakulchyk Mar 16, 2021
9ef1b05
Add example of data request to Readme
Vitalina-Vakulchyk Mar 16, 2021
eb10db1
Use data according db fields in Readme request example section
Vitalina-Vakulchyk Mar 16, 2021
ebbdd10
Try without json code block definition in Readme example of request s…
Vitalina-Vakulchyk Mar 16, 2021
81e3bfe
Add comma to code example of example of request section
Vitalina-Vakulchyk Mar 16, 2021
1b2c983
Use dotts in code block in example of request section in Readme
Vitalina-Vakulchyk Mar 16, 2021
e0ef5ea
Correct gramma
Vitalina-Vakulchyk Mar 16, 2021
34d464e
Correct text in example of request in Readme
Vitalina-Vakulchyk Mar 16, 2021
cf21ced
Merge branch 'master' into review-and-update-documentation
Vitalina-Vakulchyk Mar 16, 2021
23f4e42
Update CHANGELOG according to Readme changes
Vitalina-Vakulchyk Mar 16, 2021
852ee81
Correct word order
Vitalina-Vakulchyk Mar 16, 2021
8b62fe9
Change view of link in Elasticsearch client options section
Vitalina-Vakulchyk Mar 16, 2021
d499f71
Set actual PR link to CHANGELOG file
Vitalina-Vakulchyk Mar 16, 2021
543df2e
Change text of link in Elasticsearch client options section
Vitalina-Vakulchyk Mar 16, 2021
1db4cc9
Update CHANGELOG file with AR versions information
Vitalina-Vakulchyk Mar 16, 2021
155be11
Add snippet of the definition of UsersIndex
Vitalina-Vakulchyk Mar 16, 2021
72c3293
Correct example field names for query
Vitalina-Vakulchyk Mar 16, 2021
0259121
Update text for Example of data request section
Vitalina-Vakulchyk Mar 16, 2021
6503d4e
Remove updates from CHANGELOG
Vitalina-Vakulchyk Mar 17, 2021
da5968f
Adjust wording
Vitalina-Vakulchyk Mar 17, 2021
fdc8cc4
Add json code formatting to Example of data request section
Vitalina-Vakulchyk Mar 17, 2021
313ad80
Update Getting Started and content
Vitalina-Vakulchyk Mar 17, 2021
097e715
Replace Index item position in content
Vitalina-Vakulchyk Mar 17, 2021
9cf2dbe
Make Getting Started as section
Vitalina-Vakulchyk Mar 17, 2021
cdc6195
Add Ruby code style to Getting Started section
Vitalina-Vakulchyk Mar 17, 2021
369eaa4
Reorder content
Vitalina-Vakulchyk Mar 17, 2021
de08419
Restart CI
Vitalina-Vakulchyk Mar 18, 2021
97dc6f9
Update Getting Started section introductional text
Vitalina-Vakulchyk Mar 18, 2021
4eb7efa
Add shell code identifying
Vitalina-Vakulchyk Mar 18, 2021
f84b3d0
Remove Ruby code identification
Vitalina-Vakulchyk Mar 18, 2021
fa49552
Update Docker section
Vitalina-Vakulchyk Mar 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 160 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elas

* [Why Chewy?](#why-chewy)
* [Installation](#installation)
* [Usage](#usage)
* [Compatibility](#compatibility)
* [Ruby](#ruby)
* [Elasticsearch compatibility matrix](#elasticsearch-compatibility-matrix)
* [Active Record](#active-record)
* [Getting Started](#getting-started)
* [Minimal client setting](*minimal-client-setting)
* [Elasticsearch](#elasticsearch)
* [Index](#index)
* [Model](#model)
* [Example of data request](#example-of-data-request)
* [Usage and configuration](#usage)
* [Client settings](#client-settings)
* [AWS ElasticSearch configuration](#aws-elastic-search)
* [Index definition](#index-definition)
Expand All @@ -30,6 +40,7 @@ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elas
* [Non-block notation](#non-block-notation)
* [Designing your own strategies](#designing-your-own-strategies)
* [Rails application strategies integration](#rails-application-strategies-integration)
* [Elasticsearch client options](#elasticsearch-client-options)
* [ActiveSupport::Notifications support](#activesupportnotifications-support)
* [NewRelic integration](#newrelic-integration)
* [Search requests](#search-requests)
Expand Down Expand Up @@ -101,23 +112,157 @@ Chewy is compatible with MRI 2.5-3.0¹.

See [Migration guide](migration_guide.md).

## Usage
### Active Record

### Client settings
5.2, 6.0, 6.1 Active Record versions are supported by all Chewy versions.

## Getting Started

Chewy provides functionality for Elasticsearch index handling, documents import mappings, index update strategies and chainable query DSL.

### Minimal client setting

Create `config/initializers/chewy.rb` with this line:

```ruby
Chewy.settings = {host: 'localhost:9250'}
```

And run `rails g chewy:install` to generate `chewy.yml`:

```yaml
# config/chewy.yml
# separate environment configs
test:
host: 'localhost:9250'
prefix: 'test'
development:
host: 'localhost:9200'
```

### Elasticsearch

Make sure you have Elasticsearch up and running. You can [install](https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html) it locally, but the easiest way is to use [Docker](https://www.docker.com/get-started):

```shell
$ docker run --rm --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.11.1
```

### Index

Create `app/chewy/user_index.rb` with User Index:

```ruby
class UsersIndex < Chewy::Index
settings analysis: {
analyzer: {
email: {
tokenizer: 'keyword',
filter: ['lowercase']
}
}
}

define_type User do
field :first_name
field :last_name
field :email, analyzer: 'email'
end
end
```

### Model

There are two ways to configure the Chewy client:
Add User model, table and migrate it:

* via the hash `Chewy.settings`
* via the configuration file `chewy.yml`
```shell
$ bundle exec rails g model User first_name last_name email
$ bundle exec rails db:migrate
```

Add `update_index` to app/models/user.rb:

```ruby
class User < ApplicationRecord
update_index('users#user') { self }
end
```

### Example of data request

1. Once a record is created (could be done via the Rails console), it creates User index too:

```
User.create(
first_name: "test1",
last_name: "test1",
email: '[email protected]',
# other fields
)
# UsersIndex::User Import (355.3ms) {:index=>1}
# => #<User id: 1, first_name: "test1", last_name: "test1", email: "[email protected]", # other fields>
```

You can create `chewy.yml` manually or run `rails g chewy:install` to
generate it.
2. A query could be exposed at a given `UsersController`:

```ruby
def search
@users = UsersIndex.query(query_string: { fields: [:first_name, :last_name, :email, ...], query: search_params[:query], default_operator: 'and' })
render json: @users.to_json, status: :ok
end

private

def search_params
params.permit(:query, :page, :per)
end
```

3. So a request against `http://localhost:3000/users/[email protected]` issuing a response like:

```json
[
{
"attributes":{
"id":"1",
"first_name":"test1",
"last_name":"test1",
"email":"[email protected]",
...
"_score":0.9808291,
"_explanation":null
},
"_data":{
"_index":"users",
"_type":"_doc",
"_id":"1",
"_score":0.9808291,
"_source":{
"first_name":"test1",
"last_name":"test1",
"email":"[email protected]",
...
}
}
}
]
```

## Usage and configuration

### Client settings

To configure the Chewy client you need to add `chewy.rb` file with `Chewy.settings` hash:

```ruby
# config/initializers/chewy.rb
Chewy.settings = {host: 'localhost:9250'} # do not use environments
```

And add `chewy.yml` configuration file.

You can create `chewy.yml` manually or run `rails g chewy:install` to generate it:

```yaml
# config/chewy.yml
# separate environment configs
Expand Down Expand Up @@ -167,7 +312,7 @@ Chewy.settings = {
}
```

### Index definition
#### Index definition

1. Create `/app/chewy/users_index.rb`

Expand Down Expand Up @@ -788,6 +933,12 @@ RSpec.configure do |config|
end
```

### Elasticsearch client options

All connection options, except the `:prefix`, are passed to the `Elasticseach::Client.new` ([chewy/lib/chewy.rb](https://github.com/toptal/chewy/blob/f5bad9f83c21416ac10590f6f34009c645062e89/lib/chewy.rb#L153-L160)):

Here's the relevant Elasticsearch documentation on the subject: https://rubydoc.info/gems/elasticsearch-transport#setting-hosts

### `ActiveSupport::Notifications` support

Chewy has notifying the following events:
Expand Down