### Start a testing cluster with specific configuration Source: https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md Examples for starting a Platinum cluster or using the version defined in the pipeline configuration. ```bash rake docker:start[8.x-SNAPSHOT,platinum] ``` ```bash rake es:up ``` -------------------------------- ### Run docker-compose with STACK_VERSION Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/examples/apm/README.md Commands to start the example stack. Set STACK_VERSION to the desired Elastic stack version before running docker-compose up. ```bash $ cd docs/examples/apm $ STACK_VERSION=7.9.1 docker-compose up ``` -------------------------------- ### Install from source checkout Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/transport.md Clone the repository and install the gem from a source code checkout. ```bash git clone https://github.com/elastic/elastic-transport-ruby.git cd elastic-transport bundle install rake install ``` -------------------------------- ### Install from source checkout Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/api.md Installs the unreleased version from a source code checkout. ```bash git clone https://github.com/elasticsearch/elasticsearch-ruby.git cd elasticsearch-ruby/elasticsearch-api bundle install rake install ``` -------------------------------- ### Install ecs-logging gem from command line Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/ecs.md Installs the ecs-logging gem from the command line using gem install. ```ruby $ gem install ecs-logging ``` -------------------------------- ### Clone and bootstrap the project Source: https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md Commands to clone the repository and install dependencies across all subprojects. ```bash $ git clone https://github.com/elasticsearch/elasticsearch-ruby.git $ cd elasticsearch-ruby/ $ bundle exec rake bundle ``` -------------------------------- ### TaskGroup example YAML Source: https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/api-spec-testing/README.md Example of a task group with a 'do' action and match verifications. ```yaml - do: index: index: test-index id: 1 body: { foo: bar } - match: { _index: test-index } - match: { _id: "1"} - match: { _version: 1} ``` -------------------------------- ### Initialize and use the Elasticsearch client Source: https://github.com/elastic/elasticsearch-ruby/blob/main/README.md Basic example showing how to require the library, initialize a client with a host, and perform simple operations like pinging the server and searching. ```ruby require 'elasticsearch' client = Elasticsearch::Client.new(host: 'https://my-elasticsearch-host.example') client.ping client.search(q: 'test') ``` -------------------------------- ### Run benchmark with bundle install and rake run Source: https://github.com/elastic/elasticsearch-ruby/blob/main/scripts/benchmark-vectors/README.md Commands to install dependencies and run the benchmark script. The script connects to http://localhost:9200 with security disabled and produces results.json. ```bash bundle install bundle exec rake run ``` -------------------------------- ### Install elasticsearch-api gem Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/api.md Installs the elasticsearch-api gem from Rubygems. ```bash gem install elasticsearch-api ``` -------------------------------- ### Install elastic-transport gem Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/transport.md Install the elastic-transport gem from Rubygems. ```bash gem install elastic-transport ``` -------------------------------- ### Example output of openssl x509 fingerprint Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/connecting.md Example output of the openssl x509 command, showing the SHA256 fingerprint format. ```sh SHA256 Fingerprint=A5:2D:D9:35:11:E8:C6:04:5E:21:F1:66:54:B7:7C:9E:E0:F3:4A:EA:26:D9:F4:03:20:B5:31:C4:74:67:62:28 ``` -------------------------------- ### Basic client usage Source: https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch/README.md Basic usage example showing how to require the elasticsearch gem, create a client with logging enabled, and call cluster.health, reload_connections!, and search methods. ```ruby require 'elasticsearch' client = Elasticsearch::Client.new(log: true) client.cluster.health client.transport.reload_connections! client.search(q: 'test') # etc. ``` -------------------------------- ### Basic usage of the Ruby client Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/connecting.md Example of using the Ruby client to perform cluster health, indexing, refresh, and search operations. Enable logging with `log: true` to see requests. ```ruby require 'elasticsearch' client = Elasticsearch::Client.new log: true client.cluster.health client.index(index: 'my-index', id: 1, body: { title: 'Test' }) client.indices.refresh(index: 'my-index') client.search(index: 'my-index', body: { query: { match: { title: 'test' } } }) ``` -------------------------------- ### ES|QL query API with Ruby client Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/esql.md Use the ES|QL query API to run an ES|QL query and get a Hash response with columns and values. The example shows a query that calculates duration_ms from event.duration and prints the response. ```ruby query = <[ {"name"=>"@timestamp", "type"=>"date"}, {"name"=>"client.ip", "type"=>"ip"}, {"name"=>"event.duration", "type"=>"long"}, {"name"=>"message", "type"=>"keyword"}, {"name"=>"duration_ms", "type"=>"double"} ], "values"=>[ ["2023-10-23T12:15:03.360Z", "172.21.2.162", 3450233, "Connected to 10.1.0.3", 3.5], ["2023-10-23T12:27:28.948Z", "172.21.2.113", 2764889, "Connected to 10.1.0.2", 2.8], ["2023-10-23T13:33:34.937Z", "172.21.0.5", 1232382, "Disconnected", 1.2], ["2023-10-23T13:51:54.732Z", "172.21.3.15", 725448, "Connection error", 0.7], ["2023-10-23T13:52:55.015Z", "172.21.3.15", 8268153, "Connection error", 8.3], ["2023-10-23T13:53:55.832Z", "172.21.3.15", 5033755, "Connection error", 5.0], ["2023-10-23T13:55:01.543Z", "172.21.3.15", 1756467, "Connected to 10.1.0.1", 1.8] ]} ``` -------------------------------- ### Start Elasticsearch via Rake Source: https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/README.md Starts an Elasticsearch docker container using the default version specified in the pipeline configuration. ```bash $ rake es:up ``` -------------------------------- ### Run Elasticsearch and Kibana locally Source: https://github.com/elastic/elasticsearch-ruby/blob/main/README.md Use this command to start Elasticsearch at http://localhost:9200 and Kibana at http://localhost:5601 using Docker. ```bash curl -fsSL https://elastic.co/start-local | sh ``` -------------------------------- ### Use bundled Curb transport Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/transport.md Requires the 'curb' gem and uses the bundled Curb transport implementation. The example creates a client with the Curb transport class and inspects the underlying connection object. ```ruby require 'curb' require 'elastic/transport/transport/http/curb' client = Elastic::Client.new(transport_class: Elastic::Transport::Transport::HTTP::Curb) client.transport.connections.first.connection # => # ``` -------------------------------- ### Install the latest elasticsearch gem Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/installation.md Use this command to install the latest version of the elasticsearch Ruby gem. ```sh gem install elasticsearch ``` -------------------------------- ### Start a testing cluster with Docker Source: https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md Rake task to start a testing cluster in a Docker container using a specific version. ```bash rake docker:start[VERSION] ``` -------------------------------- ### GCP Cloud Functions example Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/connecting.md Initialize the client outside the function in the global scope to improve performance and enable background functionality like sniffing. This example uses the `functions_framework` gem and a cloud_id for authentication. ```ruby require 'functions_framework' require 'elasticsearch' client = Elasticsearch::Client.new( cloud_id: "elasic-cloud-id", user: "elastic", password: "password", log: true ) FunctionsFramework.http "hello_world" do |request| client.search( index: 'stack-overflow', body: { query: { match: { title: { query: 'phone application' } } } } ) end ``` -------------------------------- ### Start Specific Elasticsearch Version via Rake Source: https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/README.md Starts a specific version of Elasticsearch with a chosen security suite, such as 'free' or 'platinum'. ```bash $ rake es:start[version,suite] # e.g. rake es:start[9.0.0-SNAPSHOT, free] ``` -------------------------------- ### Use authentication with reloading hosts Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/advanced-config.md When using reloading hosts with authentication, pass the scheme, user, and password in the http options. This example enables both reload_connections and reload_on_failure. ```ruby Elasticsearch::Client.new( host: 'localhost:9200', http: { scheme: 'https', user: 'U', password: 'P' }, reload_connections: true, reload_on_failure: true ) ``` -------------------------------- ### Example JSON file with nested data Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/Helpers.md Shows a JSON file structure where the array of data to ingest is nested under the 'data' and 'items' keys. ```json { "field": "value", "status": 200, "data": { "items": [ { "name": "Item 1", (...) }, { (...) ] } } ``` -------------------------------- ### Elasticsearch startup output with security credentials Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/connecting.md Shows the output block from Elasticsearch when it starts for the first time, containing the elastic user password and the HTTP CA certificate SHA-256 fingerprint. Note down these values for the next sections; they are stored in the variables ELASTIC_PASSWORD and CERT_FINGERPRINT. ```sh ---------------------------------------------------------------- -> Elasticsearch security features have been automatically configured! -> Authentication is enabled and cluster connections are encrypted. -> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`): lhQpLELkjkrawaBoaz0Q -> HTTP CA certificate SHA-256 fingerprint: a52dd93511e8c6045e21f16654b77c9ee0f34aea26d9f40320b531c474676228 ... ---------------------------------------------------------------- ``` -------------------------------- ### YAML example of `warnings` directive in a `do` block Source: https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/api-spec-testing/README.md Use this YAML structure in a REST API test to expect specific Warning headers. The `warnings` argument must be a list, even for a single warning, and the warnings must match exactly. The example shows a `get` request with `index`, `type`, and `id` parameters. ```yaml - do: warnings: - '[index] is deprecated' - quotes are not required because yaml - but this argument is always a list, never a single string - no matter how many warnings you expect get: index: test type: test id: 1 ``` -------------------------------- ### Use Patron adapter with client Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/transport.md Create a client and verify the Patron adapter is used, then fetch node stats. ```rb client = Elastic::Transport::Client.new client.transport.connections.first.connection.builder.adapter # => Faraday::Adapter::Patron 10.times do client.nodes.stats(metric: 'http')['nodes'].values.each do |n| puts "#{n['name']} : #{n['http']['total_opened']}" end end # => Stiletoo : 24 # => Stiletoo : 24 # => Stiletoo : 24 # => ... ``` -------------------------------- ### Run the interactive Ruby console Source: https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md Commands to launch the console and initialize the Elasticsearch client. ```bash $ rake console # calls ./elasticsearch/bin/elastic_ruby_console [1] elastic_ruby(main)> client = Elasticsearch::Client.new(host: 'http://elastic:changeme@localhost:9200', log: true) [2] elastic_ruby(main)> client.info ``` -------------------------------- ### Run Elasticsearch locally with start-local Source: https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md Command to download and run Elasticsearch locally for development purposes. ```bash curl -fsSL https://elastic.co/start-local | sh -s -- -esonly -v 9.0.0 ``` -------------------------------- ### Connect using a URL with authentication Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/advanced-config.md Creates a client using a URL that includes scheme, authentication credentials, and port. The client automatically handles these URL parts. ```ruby Elasticsearch::Client.new(url: 'https://username:password@api.server.org:4430/search') ``` -------------------------------- ### Install a specific version of elasticsearch gem Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/installation.md Use this command to install a specific version (e.g., 7.0.0) of the elasticsearch Ruby gem. ```sh gem install elasticsearch -v 7.0.0 ``` -------------------------------- ### Create a default repository with Elasticsearch::Persistence::Repository Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/persistence.md Creates a default, 'dumb' repository by including the Elasticsearch::Persistence::Repository module in a custom class and instantiating it. ```ruby require 'elasticsearch/persistence' class MyRepository; include Elasticsearch::Persistence::Repository; end repository = MyRepository.new ``` -------------------------------- ### AWS Lambda example Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/connecting.md Initialize the client outside the handler using a memoized method to reuse the client across invocations. This example uses a cloud_id for authentication. ```ruby require 'elasticsearch' def client @client ||= Elasticsearch::Client.new( cloud_id: "elastic-cloud-id", user: "elastic", password: "password", log: true ) end def lambda_handler(event:, context:) client.search( index: 'stack-overflow', body: { query: { match: { title: { query: 'phone application' } } } } ) end ``` -------------------------------- ### Create an index Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/getting-started.md Creates the index named 'my_index'. ```rb client.indices.create(index: 'my_index') ``` -------------------------------- ### Connect using multiple URLs Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/advanced-config.md Creates a client using multiple URLs separated by a comma. The client will use a round-robin algorithm across the hosts. ```ruby Elasticsearch::Client.new(urls: 'http://localhost:9200,http://localhost:9201') ``` -------------------------------- ### Run Serverless test suite Source: https://github.com/elastic/elasticsearch-ruby/blob/main/CONTRIBUTING.md Execute the Serverless-specific test suite by providing the required environment variables for the API key and server endpoint. ```bash TEST_SUITE=serverless \ ES_API_KEY=YOUR_SERVERLESS_API_KEY \ TEST_ES_SERVER='ELASTICSEARCH_SERVERLESS_ENDPOINT' \ rake test:yaml ``` -------------------------------- ### Get a document Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/getting-started.md Retrieves a document by its ID from 'my_index'. ```rb client.get(index: 'my_index', id: id) ``` -------------------------------- ### Get a document by ID with client.get Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/examples.md Retrieves a document by its ID from the 'books' index. The ID is stored in a variable and passed to client.get. ```ruby id = 'l7LRjXgB2_ry9btNEv_V' client.get(index: 'books', id: id) ``` -------------------------------- ### Use third-party logging library Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/advanced-config.md Pass a conforming logger implementation, such as the logging gem, to the client. ```ruby require 'logging' # https://github.com/TwP/logging/ log = Logging.logger['elasticsearch'] log.add_appenders Logging.appenders.stdout log.level = :info client = Elasticsearch::Client.new(logger: log) ``` -------------------------------- ### Faraday adapter registration error Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/troubleshoot.md Example of the error message encountered when an adapter is not registered on Faraday, typically during migration to Faraday v2. ```text :adapter is not registered on Faraday::Adapter (Faraday::Error) ``` -------------------------------- ### Connect to a single host Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/advanced-config.md Creates a client connected to a specific host. The host is specified as a string. ```ruby Elasticsearch::Client.new(host: 'search.myserver.com') ``` -------------------------------- ### Require adapter before client instantiation Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/transport.md For Faraday 2, require the adapter before instantiating the client to avoid the error. ```rb > client = Elasticsearch::Client.new(adapter: :net_http_persistent) Faraday::Error: :net_http_persistent is not registered on Faraday::Adapter > require 'faraday/net_http_persistent' => true > client = Elasticsearch::Client.new(adapter: :net_http_persistent) => # 1, "text"=>"Test"}> ``` -------------------------------- ### Connect to multiple hosts Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/advanced-config.md Creates a client connected to multiple hosts. The hosts are specified as an array of strings. ```ruby Elasticsearch::Client.new(hosts: ['myhost1', 'myhost2']) ``` -------------------------------- ### Configure Faraday instance with block Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/transport.md Use a block to configure the Faraday instance directly, e.g., adding a logger and adapter. ```rb require 'patron' client = Elastic::Client.new(host: 'localhost', port: '9200') do |f| f.response :logger f.adapter :patron end ``` -------------------------------- ### Define a plain Ruby object (Note) for persistence Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/persistence.md Defines a simple PORO (Plain Old Ruby Object) with attributes and a to_hash method, used as the domain object for the repository examples. ```ruby class Note attr_reader :attributes def initialize(attributes={}) @attributes = attributes end def to_hash @attributes end end ``` -------------------------------- ### Basic authentication with URL format Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/connecting.md Use the common URL format to pass credentials directly in the URL. This is a concise alternative to the host configuration hash. ```ruby client = Elasticsearch::Client.new(url: 'https://username:password@localhost:9200') ``` -------------------------------- ### Run single test file Source: https://github.com/elastic/elasticsearch-ruby/blob/main/elasticsearch-api/api-spec-testing/README.md Runs a single test file using SINGLE_TEST and TEST_ES_SERVER. ```bash $ cd elasticsearch-api && SINGLE_TEST=indices.resolve_index/10_basic_resolve_index.yml TEST_ES_SERVER='http://localhost:9200' be rake test:rest_api ``` -------------------------------- ### Using JSON Builders with JBuilder Source: https://github.com/elastic/elasticsearch-ruby/blob/main/docs/reference/api.md Pass a JSON string built with JBuilder as the :body argument instead of a Ruby Hash. The example shows a search query and the resulting HTTP request and response. ```ruby require 'jbuilder' query = Jbuilder.encode do |json| json.query do json.match do json.title do json.query 'test 1' json.operator 'and' end end end end client.search(index: 'myindex', body: query) # 2013-06-25 09:56:05 +0200: GET http://localhost:9200/myindex/_search [status:200, request:0.015s, query:0.011s] # 2013-06-25 09:56:05 +0200: > {"query":{"match":{"title":{"query":"test 1","operator":"and"}}}} # ... # => {"took"=>21, ..., "hits"=>{"total"=>1, "hits"=>[{ "_source"=>{"title"=>"Test 1", ...}}]}} ```