Class: Kabutops::Adapters::ElasticSearch

Inherits:
DatabaseAdapter show all
Includes:
Extensions::Parameterable
Defined in:
lib/kabutops/adapters/elastic_search.rb

Instance Attribute Summary

Attributes inherited from DatabaseAdapter

#recipe

Instance Method Summary collapse

Methods included from Extensions::Includable

#append_features, #included

Methods inherited from DatabaseAdapter

#data, #process, #process_one

Methods included from Extensions::CallbackSupport

#callbacks, #manager, #notify

Methods included from Extensions::Logging

#logger

Methods inherited from Base

#debug, #enable_debug, #initialize

Constructor Details

This class inherits a constructor from Kabutops::Adapters::Base

Instance Method Details

#clientObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/kabutops/adapters/elastic_search.rb', line 71

def client
  @client ||= Elasticsearch::Client.new(
    hosts: [
      {
        host: params[:host] || 'localhost',
        port: params[:port] || '9200',
      },
    ],
  )
end

#find(resource) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kabutops/adapters/elastic_search.rb', line 21

def find resource
  result = client.search(
    index: params[:index] || 'default',
    body: {
      query: {
        filtered: {
          filter: {
            or: [
              { term: { id: resource[:id] || resource[:url] } },
              { term: { url: resource[:url] } },
            ]
          },
        },
      },
    },
    size: 5,
  )
  result['hits']['hits'].map{ |hit| hit['_source'] }.first
end

#find_outdated(freshness) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kabutops/adapters/elastic_search.rb', line 41

def find_outdated freshness
  result = client.search(
    index: params[:index] || 'default',
    body: {
      query: {
        filtered: {
          filter: {
            and: [
              {
                or: [
                  { range: { updated_at: { lte: Time.now.to_i - freshness } } },
                  { missing: { field: 'updated_at' } },
                ]
              },
              {
                or: [
                  { range: { scheduled_update_at: { lte: Time.now.to_i - 3600 } } },
                  { missing: { field: 'scheduled_update_at' } },
                ]
              },
            ]
          },
        },
      },
    },
    size: 5,
  )
  result['hits']['hits'].map{ |hit| hit['_source'] }
end

#store(result) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/kabutops/adapters/elastic_search.rb', line 12

def store result
  client.index(
    index: params[:index] || 'default',
    type: params[:type] || 'default',
    id: result[:id],
    body: result.to_hash,
  )
end