{"id":19063242,"url":"https://github.com/delner/elasticsearch-resources","last_synced_at":"2026-05-17T03:04:45.374Z","repository":{"id":59153186,"uuid":"78970849","full_name":"delner/elasticsearch-resources","owner":"delner","description":"Access well-known Elasticsearch indexes like resources.","archived":false,"fork":false,"pushed_at":"2017-01-19T20:56:12.000Z","size":125,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T16:09:02.140Z","etag":null,"topics":["elasticsearch","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/delner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-14T22:36:17.000Z","updated_at":"2017-05-14T02:19:51.000Z","dependencies_parsed_at":"2022-09-13T17:50:39.567Z","dependency_job_id":null,"html_url":"https://github.com/delner/elasticsearch-resources","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delner%2Felasticsearch-resources","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delner%2Felasticsearch-resources/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delner%2Felasticsearch-resources/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delner%2Felasticsearch-resources/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/delner","download_url":"https://codeload.github.com/delner/elasticsearch-resources/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240118332,"owners_count":19750470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elasticsearch","ruby"],"created_at":"2024-11-09T00:29:22.478Z","updated_at":"2026-05-14T09:30:16.902Z","avatar_url":"https://github.com/delner.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"Elasticsearch::Resources\n========================\n\n[![Build Status](https://travis-ci.org/delner/elasticsearch-resources.svg?branch=master)](https://travis-ci.org/delner/elasticsearch-resources) [![Gem Version](https://badge.fury.io/rb/elasticsearch-resources.svg)](https://badge.fury.io/rb/elasticsearch-resources)\n###### *For Ruby 2.3+*\n\n`Elasticsearch::Resources` is a wrapper for the [Elasticsearch gem](https://github.com/elastic/elasticsearch-ruby) that provides a strongly typed interface for accessing your indexes, types, and documents.\n\n```ruby\n# Search an index: e.g. search { index: 'film', body: { query: { ... } } }\ndocuments = film.search({ query: { match: { title: 'Tron' } } })\ndocuments # [#\u003cMovie @title='Tron'\u003e, #\u003cDocumentary @title='Making Tron'\u003e]\n\n# Get a specific document: e.g. get { index: 'film', type: 'movies', id: 'tron' }\ndocument = movies.get(id: 'tron') # #\u003cMovie @title='Tron'\u003e\ndocument.id # =\u003e 'tron'\ndocument.attributes # =\u003e { 'title' =\u003e 'Tron' }\ndocument.title # =\u003e 'Tron'\n```\n\n### Installation\n\n##### If you're not using Bundler...\n\nInstall the gem via:\n\n```\ngem install elasticsearch-resources\n```\n\nThen require it into your application with:\n\n```\nrequire 'elasticsearch/resources'\n```\n\n##### If you're using Bundler...\n\nAdd the gem to your Gemfile:\n\n```\ngem 'elasticsearch/resources'\n```\n\nAnd then `bundle install` to install the gem and its dependencies.\n\n### Common use cases\n\n##### Search all indexes in a cluster\n\n```ruby\n# Connects to default of '127.0.0.1:9200'\ncluster = Elasticsearch::Resources::Cluster.new\n\n# Hash can be any valid Elasticsearch query\ndocuments = cluster.search({ query: { match: { title: 'Tron' } } })\n```\n\n##### Search all types in an index\n\n```ruby\n# Connects to default of '127.0.0.1:9200'\ncluster = Elasticsearch::Resources::Cluster.new\ndocuments = cluster.search({ index: 'film', query: { match: { title: 'Tron' } } })\n\n# OR using a defined index\nindex = Elasticsearch::Resources::Index.new(cluster: cluster) do |index|\n  index.name = 'film'\nend\n\ndocuments = index.search({ query: { match: { title: 'Tron' } } })\n```\n\n##### Search specific type in an index\n\n```ruby\n# Connects to default of '127.0.0.1:9200'\ncluster = Elasticsearch::Resources::Cluster.new\ndocuments = cluster.search({ index: 'film', type: 'movies', query: { match: { title: 'Tron' } } })\n\n# OR using a defined index \u0026 type\nindex = Elasticsearch::Resources::Index.new(cluster: cluster) do |index|\n  index.name = 'film'\nend\n\ntype = Elasticsearch::Resources::Type.new(index: index) do |type|\n  type.name = 'movies'\nend\n\ndocuments = type.search({ query: { match: { title: 'Tron' } } })\n```\n\n##### Creating a document\n\n```ruby\n# type: Elasticsearch::Resources::Type object\ndocument = Elasticsearch::Resources::Document.new(type: type, id: 'tron', attributes: { title: 'Tron' })\ndocument.create\n```\n\n##### Fetch a document\n\n```ruby\ndocument = type.get(id: 'tron')\n```\n\n### Resources\n\nThere are four (4) basic resources which you can query with: `Cluster`, `Index`, `Type`, and `Document`.\n\n#### Cluster\n\nAn `Elasticsearch::Resources::Cluster` represents an ElasticSearch cluster that hosts multiple indexes. You will always needs to create one to run queries.\n\n```ruby\n# Create a cluster with default settings (connects to '127.0.0.1:9200')\ncluster = Elasticsearch::Resources::Cluster.new\n\n# Create a cluster with custom host\ncluster = Elasticsearch::Resources::Cluster.new do |cluster|\n  cluster.host = 'davesawesomemovies.com:9200'\nend\n```\n\n##### indexes\n\nReturns a `Hash` of well known indexes. Empty if no well-known indexes are defined (regardless of whether they actually exist in Elasticsearch.) See \"Defining well known resources\" and `define_indexes`.\n\n```ruby\ncluster.indexes\n# =\u003e { film: #\u003cElasticsearch::Resources::Index\u003e }\n```\n\n##### client\n\nReturns the underlying `Elasticsearch::Transport::Client`.\n\n```ruby\ncluster.client\n```\n\n##### search\n\nAccepts `(body, options = {})` and calls [`search`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#search-instance_method) on the `Elasticsearch::Transport::Client`.\n\nReturns `Array[Elasticsearch::Resources::Document]` objects.\n\n```ruby\ncluster.search({ query: { match: { title: 'Tron' } } })\n# =\u003e [#\u003cElasticsearch::Resources::Document\u003e]\n```\n\n##### count\n\nAccepts `(body, options = {})` and calls [`count`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#count-instance_method) on the `Elasticsearch::Transport::Client`.\n\nReturns `Hash` response from `Elasticsearch::Transport::Client`.\n\n```ruby\ncluster.count({ query: { match: { title: 'Tron' } } })\n# =\u003e {\"count\"=\u003e1, \"_shards\"=\u003e{\"total\"=\u003e5, \"successful\"=\u003e5, \"failed\"=\u003e0}}\n```\n\n#### Index\n\nAn `Elasticsearch::Resources::Index` represents an ElasticSearch index that contains multiple types. Requires a `Cluster` (see \"Cluster\" above.)\n\n```ruby\n# Create an index. You must set `name`.\nindex = Elasticsearch::Resources::Index.new(cluster: cluster) do |index|\n  index.name = 'film'\nend\n```\n\n##### name\n\nReturns the name of the index, as it exists in Elasticsearch.\n\n```ruby\nindex.name\n# =\u003e 'film'\n```\n\n##### types\n\nReturns a `Hash` of well known types. Empty if no well-known types are defined (regardless of whether they actually exist in Elasticsearch.) See \"Defining well known resources\" and `define_types`.\n\n```ruby\nindex.types\n# =\u003e { movies: #\u003cElasticsearch::Resources::Type\u003e }\n```\n\n##### client\n\nReturns the underlying `Elasticsearch::Transport::Client`.\n\n```ruby\nindex.client\n```\n\n##### search\n\nAccepts `(body, options = {})` and calls [`search`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#search-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` option to your queries.\n\nReturns `Array[Elasticsearch::Resources::Document]` objects.\n\n```ruby\nindex.search({ query: { match: { title: 'Tron' } } })\n# =\u003e [#\u003cElasticsearch::Resources::Document\u003e]\n```\n\n##### count\n\nAccepts `(body, options = {})` and calls [`count`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#count-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` option to your queries.\n\nReturns `Hash` response from `Elasticsearch::Transport::Client`.\n\n```ruby\nindex.count({ query: { match: { title: 'Tron' } } })\n# =\u003e {\"count\"=\u003e1, \"_shards\"=\u003e{\"total\"=\u003e5, \"successful\"=\u003e5, \"failed\"=\u003e0}}\n```\n\n##### exists?\n\nCalls [`exists?`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Indices/Actions#exists-instance_method) on the `Elasticsearch::API::Indices::IndicesClient`.\n\nReturns `true` or `false`.\n\n```ruby\nindex.exists?\n# =\u003e true\n```\n\n##### create\n\nCalls [`create`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Indices/Actions#create-instance_method) on the `Elasticsearch::API::Indices::IndicesClient`.\n\nThrows error if index already exists.\n\n```ruby\nindex.create\n```\n\n##### put_mapping\n\nAccepts `(options = {})` and calls [`put_mapping`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Indices/Actions#put_mapping-instance_method) on the `Elasticsearch::API::Indices::IndicesClient`.\n\n```ruby\nindex.put_mapping(type: 'movies', body: { })\n```\n\n##### delete\n\nCalls [`delete`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Indices/Actions#delete-instance_method) on the `Elasticsearch::API::Indices::IndicesClient`.\n\nThrows error if index doesn't exist.\n\n```ruby\nindex.delete\n```\n\n#### Type\n\nAn `Elasticsearch::Resources::Type` represents an ElasticSearch type within an index. Requires a `Index` (see \"Index\" above.)\n\n```ruby\n# Create a type. You must set `name`.\ntype = Elasticsearch::Resources::Type.new(index: index) do |type|\n  type.name = 'movie'\nend\n```\n\n##### name\n\nReturns the name of the type, as it exists in Elasticsearch.\n\n```ruby\ntype.name\n# =\u003e 'movie'\n```\n\n##### client\n\nReturns the underlying `Elasticsearch::Transport::Client`.\n\n```ruby\ntype.client\n```\n\n##### search\n\nAccepts `(body, options = {})` and calls [`search`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#search-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\nReturns `Array[Elasticsearch::Resources::Document]` objects.\n\n```ruby\ntype.search({ query: { match: { title: 'Tron' } } })\n# =\u003e [#\u003cElasticsearch::Resources::Document\u003e]\n```\n\n##### count\n\nAccepts `(body, options = {})` and calls [`count`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#count-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\nReturns `Hash` response from `Elasticsearch::Transport::Client`.\n\n```ruby\ntype.count({ query: { match: { title: 'Tron' } } })\n# =\u003e {\"count\"=\u003e1, \"_shards\"=\u003e{\"total\"=\u003e5, \"successful\"=\u003e5, \"failed\"=\u003e0}}\n```\n\n##### exists?\n\nAccepts `(options = {})` and calls [`exists?`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#exists-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\nReturns `true` or `false`.\n\n```ruby\ntype.exists?(id: 'tron')\n# =\u003e true\n```\n\n##### create\n\nAccepts `(options = {})` and calls [`create`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#create-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\nThrows error if document already exists.\n\n```ruby\ntype.create(id: 'tron', body: { })\n```\n\n##### update\n\nAccepts `(options = {})` and calls [`update`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#update-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\n```ruby\ntype.update(id: 'tron', body: { })\n```\n\n##### delete\n\nAccepts `(options = {})` and calls [`delete`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#delete-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\nThrows error if document doesn't exist.\n\n```ruby\ntype.delete(id: 'tron')\n```\n\n##### get\n\nAccepts `(options = {})` and calls [`get`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#get-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index` and `type` option to your queries.\n\nReturns `Elasticsearch::Resources::Document`.\n\n```ruby\ntype.get(id: 'tron')\n# =\u003e #\u003cElasticsearch::Resources::Document\u003e\n```\n\n#### Document\n\nAn `Elasticsearch::Resources::Document` represents an ElasticSearch document within an index. Requires an `id` and `Type` (see \"Type\" above.)\n\n```ruby\n# Create a document\ndocument = Elasticsearch::Resources::Document.new(\n  type: type, # (Required)\n  id: 'tron', # (Required)\n  attributes: { title: 'Tron' } # (Optional)\n)\n```\n\n##### id\n\nReturns a `String` of the Document ID.\n\n```ruby\ndocument.id\n# =\u003e 'tron'\n```\n\n##### attributes\n\nReturns a `Hash` of the Document body.\n\n```ruby\ndocument.attributes\n# =\u003e { 'title' =\u003e 'Tron' }\n```\n\n##### client\n\nReturns the underlying `Elasticsearch::Transport::Client`.\n\n```ruby\ndocument.client\n```\n\n##### exists?\n\nAccepts `(options = {})` and calls [`exists?`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#exists-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index`, `type` and `id` option to your queries.\n\nReturns `true` or `false`.\n\n```ruby\ndocument.exists?\n# =\u003e true\n```\n\n##### create\n\nAccepts `(options = {})` and calls [`create`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#create-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index`, `type`, `id`, and `body` option to your queries.\n\nThrows error if document already exists.\n\n```ruby\ndocument.create\n```\n\n##### update\n\nAccepts `(options = {})` and calls [`update`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#update-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index`, `type`, `id`, and `body` option to your queries.\n\n```ruby\ndocument.update\n```\n\n##### delete\n\nAccepts `(options = {})` and calls [`delete`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#delete-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index`, `type` and `id` option to your queries.\n\nThrows error if document doesn't exist.\n\n```ruby\ndocument.delete\n```\n\n##### get\n\nAccepts `(options = {})` and calls [`get`](http://www.rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions#get-instance_method) on the `Elasticsearch::Transport::Client`. Automatically adds the `index`, `type` and `id` option to your queries.\n\nReturns `Elasticsearch::Resources::Document`.\n\n```ruby\ndocument.get\n# =\u003e #\u003cElasticsearch::Resources::Document\u003e\n```\n\n### Defining well known resources\n\n#### Cluster\n\n```ruby\nclass DavesAwesomeMovies \u003c Elasticsearch::Resources::Cluster\n  # Set any default configuration settings here.\n  define_configuration defaults: -\u003e { |cluster|\n    cluster.host = 'davesawesomemovies.com:9200'\n  }\n\n  # Provide a hash of keys to Index class names (either constants or strings)\n  define_indexes film: 'Film'\nend\n\ncluster = DavesAwesomeMovies.new\ncluster.indexes[:film] # =\u003e #\u003cFilm\u003e\n```\n\n#### Index\n\n```ruby\nclass Film \u003c Elasticsearch::Resources::Index\n  # Set any default configuration settings here.\n  # Probably should set a name.\n  define_configuration defaults: -\u003e { |index|\n    index.name = 'film'\n  }\n\n  # Provide a hash of keys to Type class names (either constants or strings)\n  define_type movie: 'Movie'\nend\n\nfilm = Film.new\nfilm.types[:movies] # =\u003e #\u003cMovies\u003e\n```\n\n#### Type\n\n```ruby\nclass Movies \u003c Elasticsearch::Resources::Type\n  # Set any default configuration settings here.\n  # Probably should set a name.\n  define_configuration defaults: -\u003e { |type|\n    type.name = 'movie'\n  }\n\n  # Provide a Document class name (either constants or strings)\n  # If not called, type will return Elasticsearch::Resources::Document objects instead.\n  define_document 'Movie'\nend\n\nmovies = Movies.new\nmovies.get(id: 'tron') # =\u003e #\u003cMovie\u003e\n```\n\n#### Document\n\n```ruby\nclass Movie \u003c Elasticsearch::Resources::Document\n  # Provide a list of well known attributes\n  define_attributes :title, :year\nend\n\nmovie = Movie.new(id: 'tron', type: movies, attributes: { title: 'Tron', year: 1982 })\nmovie.title # =\u003e 'Tron'\nmovie.year # =\u003e 1982\n```\n\n## Development\n\nInstall dependencies using `bundle install`. Run tests using `bundle exec rspec`\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/delner/elasticsearch-resources.\n\n## License\n\nThe gem is available as open source under the terms of the [Apache 2.0 License](https://raw.githubusercontent.com/delner/elasticsearch-resources/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdelner%2Felasticsearch-resources","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdelner%2Felasticsearch-resources","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdelner%2Felasticsearch-resources/lists"}