Class: EsTractor::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/es_tractor/client.rb

Overview

Client provides an Elasticsearch::Client with a limited subset of methods and simplified arguments.

Constant Summary collapse

ELASTICSEARCH_INDEX =

Examples:

'my_index'
ENV['ES_TRACTOR_ELASTICSEARCH_INDEX']
ELASTICSEARCH_HOST =

Examples:

'example.com:9200'
ENV['ES_TRACTOR_ELASTICSEARCH_HOST']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = false) ⇒ Client

Returns a new instance of Client.

Parameters:

  • log (true, false) (defaults to: false)

    verbose output of client-server interactions



22
23
24
25
26
27
# File 'lib/es_tractor/client.rb', line 22

def initialize(log = false)
  @client = Elasticsearch::Client.new(
    host: ELASTICSEARCH_HOST,
    log: log,
  )
end

Instance Attribute Details

#clientObject (readonly)



19
20
21
# File 'lib/es_tractor/client.rb', line 19

def client
  @client
end

Instance Method Details

#count(opts = {}) ⇒ Hash

Count documents, filtered by options.

Examples:

opts = {
  match: { topping: 'fudge' },
  exists: ['address', 'phone'],
  term: [
    { flavor: 'vanilla' },
    { scoops: 3 },
  ],
}

Client.new.count(opts) # => { 'count' => 7 }

# Tranforms opts into the following hash, passed to Elasticsearch:
{
  "query": {
    "bool": {
      "filter": [
        { "exists": { "field": ["address", "phone"] } },
        { "term": { "flavor": "vanilla" } },
        { "term":{ "scoops": 3 } }
      ],
      "must": [
        { "match": { "topping": "fudge" } }
      ]
    }
  }
}

Parameters:

  • opts (Hash) (defaults to: {})

    with the following keys:

Options Hash (opts):

  • :exists (String, Array<String>)

    One or more field names, translated into filter boolean.

  • :match (Hash, Array<Hash>)

    One or more field: match pairs, translated into must boolean.

  • :query_string (String)

    Translated into must boolean.

  • :range (Hash<Array>)

    A hash keyed on a field name, containing an array: [min, max], translated into filter boolean.

  • :term (Hash, Array<Hash>)

    One or more field: term pairs, translated into filter boolean.

Returns:

  • (Hash)

    with the result in the ‘count’ key.



71
72
73
74
# File 'lib/es_tractor/client.rb', line 71

def count(opts = {})
  args = { body: body(opts) }
  @client.count(args)
end

#search(opts) ⇒ Hash

Search documents, filtered by options, aggregate on special aggregations keys.

Supported aggregations (avg, cardinality, extended_stats, geo_bounds, geo_centroid, max min, percentiles, stats, sum, value_count) take a field name and are automatically named.

Examples:

opts = {
  query_string: 'flavor:vanilla AND cone:true',
  avg: "scoops",
}

Client.new.search(opts)

# Tranforms opts into the following hash, passed to Elasticsearch:
{
  "query": {
    "bool": {
      "filter":[],
      "must":[
        {
          "query_string": {
            "query":"flavor:vanilla AND cone:true"
          }
        }
      ]
    }
  },
  "aggs": {
    "avg-intelligence": {
      "avg": {
        "field":"scoops"
      }
    }
  }
}

Parameters:

  • opts (Hash)

    with the following keys:

Options Hash (opts):

  • :from (Integer)
  • :size (Integer)
  • :avg (String)

    Field name on which to apply the avg aggregation

  • :cardinality (String)

    Field name on which to apply the cardinality aggregation

  • :extended_stats (String)

    Field name on which to apply the extended_stats aggregation

  • :fields (Array)

    Field names to return

  • :geo_bounds (String)

    Field name on which to apply the geo_bounds aggregation

  • :geo_centroid (String)

    Field name on which to apply the geo_centroid aggregation

  • :max (String)

    Field name on which to apply the max aggregation

  • :min (String)

    Field name on which to apply the min aggregation

  • :percentiles (String)

    Field name on which to apply the percentiles aggregation

  • :sort (Array)

    Contains Hashes keyd on field name

  • :stats (String)

    Field name on which to apply the stats aggregation

  • :sum (String)

    Field name on which to apply the sum aggregation

  • :value_count (String)

    Field name on which to apply the value_count aggregation

  • :exists (String, Array<String>)

    One or more field names, translated into filter boolean.

  • :match (Hash, Array<Hash>)

    One or more field: match pairs, translated into must boolean.

  • :query_string (String)

    Translated into must boolean.

  • :range (Hash<Array>)

    A hash keyed on a field name, containing an array: [min, max], translated into filter boolean.

  • :term (Hash, Array<Hash>)

    One or more field: term pairs, translated into filter boolean.

Returns:

  • (Hash)

    with the actual results in the 'hits'['hits'] key.



146
147
148
149
150
151
152
153
# File 'lib/es_tractor/client.rb', line 146

def search(opts)
  args = {
    from: opts[:from] ? opts[:from] : 0,
    size: opts[:size] ? opts[:size] : 0,
    body: body(opts),
  }
  @client.search(args)
end