EsTractor: Simplified data extracton from Elasticsearch

home

rubygems.org/gems/es_tractor

dev

github.com/utilum/es_tractor/tree/dev

bugs

github.com/utilum/es_tractor/issues

rdoc

www.rubydoc.info/gems/es_tractor

DESCRIPTION:

Minimal, simple, DRY DSL for searching Elasticsearch.

Takes one shallow hash argument and translates it to an elaborate one passed on to elasticsearch-api. The price: narrower options. The gain: succinctness. For example, a root :range is always a boolean filter and always includes the edges:

tractor = Client.new
opts = { range: { timestamp: ['now-5m', 'now'] } }

tractor.search(opts) # => sends the following to Ealsticsearch:
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "timestamp": {
              "gte":"now-5m",
              "lte":"now"
            }
          }
        }
      ],
      "must": [],
    }
  }
}

FEATURES/PROBLEMS:

  • Subset of Search APIs: count, fields, search, sort, aggregations.

  • Supports most metrics aggregations without optional parameters.

  • Query builder translates a simplified Hash argument into boolean filters and aggregations.

  • TODO:

    • Aggregations:

      • Bucket

      • Nesting

      • Optional parameters

    • Add Search APIs:

      • Scroll

    • Extraction: Aggregation to flat records (separate project?).

SYNOPSIS:

include EsTractor
tractor = Client.new

tractor.count
  # => (Integer) number of documents on the server

tractor.count(term: { my_field: 'my precise term' })
  # => (Integer) number of documents where my_field == 'my precise term'

tractor = Client.new true
tractor.search(size: 1, term: { my_field: 'my precise term' }) # =>
{
  "took"=>29,
  "timed_out"=>false,
  "_shards"=>{"total"=>542, "successful"=>542, "failed"=>0},
  "hits"=> {
    "total"=>279271,
    "max_score"=>0.0,
    "hits"=> [
      {
        "_index"=>"my_index",
        "_id"=>"bc596ff2-955b-11e7-a7d2-001ed3f963a9",
        "_score"=>0.0,
        "_source"=> {
          "my_field"=>"my precise term",
          "timestamp"=>"2017-09-09 12:38:09.263",
        },
      },
    ],
  },
}

REQUIREMENTS:

  • Ruby 2.0+

Some environment variables are expected:

  • ES_TRACTOR_ELASTICSEARCH_HOST

  • ES_TRACTOR_ELASTICSEARCH_INDEX

INSTALL:

$ gem install es_tractor

Or in your Gemfile:

gem 'es_tractor'

DEVELOPERS:

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests, and generate the RDoc.

LICENSE:

(The MIT License)

Copyright © 2017 Oz Shelach

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.