Class: LogStash::Filters::Empow::PersistentKeyValueDB

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/elastic-db.rb

Instance Method Summary collapse

Constructor Details

#initialize(hosts, username, password, index) ⇒ PersistentKeyValueDB

include LogStash::Util::Loggable



8
9
10
11
12
13
14
15
16
17
# File 'lib/logstash/filters/elastic-db.rb', line 8

def initialize(hosts, username, password, index)
	#@logger ||= self.logger

	#@logger.debug("opening the local classification db")

	@elastic ||= Elasticsearch::Client.new(:hosts => hosts)
	@index = index

	create_index index
end

Instance Method Details

#closeObject



104
105
106
# File 'lib/logstash/filters/elastic-db.rb', line 104

def close
	#@logger.debug("clsoing the local classification db")
end

#create_index(index) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/logstash/filters/elastic-db.rb', line 19

def create_index(index)
	return if @elastic.indices.exists? index: index

	@elastic.indices.create index: index, body: {
		mappings: {
			_doc: {
				properties: {
					product_type: {
						type: 'keyword'
					},
					product: {
						type: 'keyword'
					},
					term_key: {
						type: 'keyword'
					},
					classification: {
						enabled: false
					}
				}
			}
		}
	}
end

#query(product_type, product, term) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/logstash/filters/elastic-db.rb', line 44

def query(product_type, product, term)
	#@logger.debug("quering local classification db")

	# fix nil product
	if product.nil?
		product = 'nil_safe_product_key'
	end

	response = @elastic.search index: @index, type: '_doc', body: {
		query: {
			bool: {
				must: [
					{ term: { product_type: product_type } },
					{
						bool: {
							should: [
								{
									bool: {
										must: [
											{ term: { term_key: term } },
											{ term: { product: product } }
										]
									}
								},
								{
									bool: {
										must: {
											term: { term_key: term }
										},
										must_not: {
											exists: { field: 'product' }
										}
									}
								}
							]
						}
					}
				]
			}
		}
	}

	mash = Hashie::Mash.new response

	return nil if mash.hits.hits.first.nil?

	return mash.hits.hits.first._source.classification
end

#save(doc_id, product_type, product, term, classification) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/logstash/filters/elastic-db.rb', line 93

def save(doc_id, product_type, product, term, classification)
	#@logger.debug("saving key to local classification db")

	@elastic.index index: @index, type: '_doc', id: doc_id, body: {
		product_type: product_type,
		product: product,
		term_key: term,
		classification: classification
	}
end