OpenSearch logo

OpenSearch Ruby Client

Welcome!

opensearch-ruby is a community-driven, open source fork of elasticsearch-ruby licensed under the Apache v2.0 License. For more information, see opensearch.org.

Setup

To add the client to your project, install it using RubyGem

gem install opensearch

or install it from a source code checkout:

git clone https://github.com/opensearch-project/opensearch-ruby.git cd opensearch-ruby/opensearch gem build opensearch.gemspec gem install opensearch-.gem

Import the client as a module:

require 'opensearch'

If you prefer to add the client manually or just want to examine the source code, see opensearch-ruby on GitHub.

Sample code

require 'opensearch'


# If you want to use authentication credentials
client = OpenSearch::Client.new url: 'https://admin:[email protected]:9200', log: true

# If you don't want to use authentication credentials
#client = OpenSearch::Client.new url: 'http://localhost:9200', log: true

# Create an index with non-default settings.
index_name = 'ruby-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}

response = client.indices.create(
  index: index_name,
  body: index_body
)

puts 'Creating index:'
puts response


# Add a document to the index.
document = {
  'title': 'Moneyball',
  'director': 'Bennett Miller',
  'year': '2011'
}
id = '1'

response = client.index(
  index: index_name,
  body: document,
  id: id,
  refresh: true
)

puts 'Adding document:'
puts response



# Search for the document.
q = 'miller'
query = {
  'size': 5,
  'query': {
    'multi_match': {
      'query': q,
      'fields': ['title^2', 'director']
    }
  }
}

response = client.search(
  body: query,
  index: index_name
)
puts 'Search results:'
puts response


# Delete the document.
response = client.delete(
  index: index_name,
  id: id
)

puts 'Deleting document:'
puts response

# Delete the index.
response = client.indices.delete(
  index: index_name
)

puts 'Deleting index:'
puts response

Project Resources

Code of Conduct

This project has adopted the Amazon Open Source Code of Conduct. For more information see the Code of Conduct FAQ, or contact [email protected] with any additional questions or comments.

License

This project is licensed under the Apache v2.0 License.

Copyright OpenSearch Contributors. See NOTICE for details.