The following example creates a client object with a custom URL and the `log` option set to `true`. It sets the `retry_on_failure` parameter to retry a failed request five times rather than the default three times. Finally, it increases the timeout by setting the `request_timeout` parameter to 120 seconds. It then returns the basic cluster health information:
```ruby
client = OpenSearch::Client.new(
url: "http://localhost:9200",
retry_on_failure: 5,
request_timeout: 120,
log: true
)
client.cluster.health
```
The output is as follows:
```bash
2022-08-25 14:24:52 -0400: GET http://localhost:9200/ [status:200, request:0.048s, query:n/a]
You don't need to create an index explicitly in OpenSearch. Once you upload a document into an index that does not exist, OpenSearch creates the index automatically. Alternatively, you can create an index explicitly to specify settings like the number of primary and replica shards. To create an index with non-default settings, create an index body hash with those settings:
```ruby
index_body = {
'settings': {
'index': {
'number_of_shards': 1,
'number_of_replicas': 2
}
}
}
client.indices.create(
index: 'students',
body: index_body
)
```
## Mappings
OpenSearch uses dynamic mapping to infer field types of the documents that are indexed. However, to have more control over the schema of your document, you can pass an explicit mapping to OpenSearch. You can define data types for some or all fields of your document in this mapping. To create a mapping for an index, use the `put_mapping` method:
```ruby
client.indices.put_mapping(
index: 'students',
body: {
properties: {
first_name: { type: 'keyword' },
last_name: { type: 'keyword' }
}
}
)
```
By default, string fields are mapped as `text`, but in the mapping above, the `first_name` and `last_name` fields are mapped as `keyword`. This mapping signals to OpenSearch that these fields should not be analyzed and should support only full case-sensitive matches.
You can verify the index's mappings using the `get_mapping` method:
If you know the mapping of your documents in advance and want to avoid mapping errors (for example, misspellings of a field name), you can set the `dynamic` parameter to `strict`:
```ruby
client.indices.put_mapping(
index: 'students',
body: {
dynamic: 'strict',
properties: {
first_name: { type: 'keyword' },
last_name: { type: 'keyword' },
gpa: { type: 'float'},
grad_year: { type: 'integer'}
}
}
)
```
With strict mapping, you can index a document with a missing field, but you cannot index a document with a new field. For example, indexing the following document with a misspelled `grad_yea` field fails:
```ruby
document = {
first_name: 'Connor',
last_name: 'James',
gpa: 3.93,
grad_yea: 2021
}
client.index(
index: 'students',
body: document,
id: 100,
refresh: true
)
```
OpenSearch returns a mapping error:
```bash
{"error":{"root_cause":[{"type":"strict_dynamic_mapping_exception","reason":"mapping set to strict, dynamic introduction of [grad_yea] within [_doc] is not allowed"}],"type":"strict_dynamic_mapping_exception","reason":"mapping set to strict, dynamic introduction of [grad_yea] within [_doc] is not allowed"},"status":400}
```
## Indexing one document
To index one document, use the `index` method:
```ruby
document = {
first_name: 'Connor',
last_name: 'James',
gpa: 3.93,
grad_year: 2021
}
client.index(
index: 'students',
body: document,
id: 100,
refresh: true
)
```
## Updating a document
To update a document, use the `update` method:
```ruby
client.update(index: 'students',
id: 100,
body: { doc: { gpa: 3.25 } },
refresh: true)
```
## Deleting a document
To delete a document, use the `delete` method:
```ruby
client.delete(
index: 'students',
id: 100,
refresh: true
)
```
## Bulk operations
You can perform several operations at the same time by using the `bulk` method. The operations may be of the same type or of different types.
You can index multiple documents using the `bulk` method:
In the above example, you pass the data and the header together and you denote the data with the `data:` key.
## Searching for a document
To search for a document, use the `search` method. The following example searches for a student whose first or last name is "James." It uses a `multi_match` query to search for two fields (`first_name` and `last_name`), and it is boosting the `last_name` field in relevance with a caret notation (`last_name^2`).
```ruby
q = 'James'
query = {
'size': 5,
'query': {
'multi_match': {
'query': q,
'fields': ['first_name', 'last_name^2']
}
}
}
response = client.search(
body: query,
index: 'students'
)
```
If you omit the request body in the `search` method, your query becomes a `match_all` query and returns all documents in the index:
```ruby
client.search(index: 'students')
```
## Boolean query
The Ruby client exposes full OpenSearch query capability. In addition to simple searches that use the match query, you can create a more complex Boolean query to search for students who graduated in 2022 and sort them by last name. In the example below, search is limited to 10 documents.
You can bulk several queries together and perform a multi-search using the `msearch` method. The following code searches for students whose GPAs are outside the 3.1–3.9 range:
First, you issue a search query, specifying the `scroll` and `size` parameters. The `scroll` parameter tells OpenSearch how long to keep the search context. In this case, it is set to two minutes. The `size` parameter specifies how many documents you want to return in each request.
The response to the initial search query contains a `_scroll_id` that you can use to get the next set of documents. To do this, you use the `scroll` method, again specifying the `scroll` parameter and passing the `_scroll_id` in the body. You don't need to specify the query or index to the `scroll` method. The `scroll` method returns the next set of documents and the `_scroll_id`. It's important to use the latest `_scroll_id` when requesting the next batch of documents because `_scroll_id` can change between requests.
## Deleting an index
You can delete the index using the `delete` method:
The following is a complete sample program that illustrates all of the concepts described in the preceding sections. The Ruby client's methods return responses as Ruby hashes, which are hard to read. To display JSON responses in a pretty format, the sample program uses the `MultiJson.dump` method.
The [opensearch-aws-sigv4](https://github.com/opensearch-project/opensearch-ruby/tree/main/opensearch-aws-sigv4) gem provides the `OpenSearch::Aws::Sigv4Client` class, which has all features of `OpenSearch::Client`. The only difference between these two clients is that `OpenSearch::Aws::Sigv4Client` requires an instance of `Aws::Sigv4::Signer` during instantiation to authenticate with AWS: