eswrapsearch 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/eswrapsearch.rb +189 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2aba98d0cffb7739396f23b6ca75904172e9f46b
|
4
|
+
data.tar.gz: 800f26be93a3cde7ec709739301bf170b89f58e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6125c9173a8635bf4a5edb0a8ecf35d6d6839a96ad1632c4fb95d034a47bc171c048b799f7527f5e1e75352eb9c6d5275bf9a279b28293699bf149b3cc78277
|
7
|
+
data.tar.gz: 6f50e20dcfb58c233e44fcb267d9d81005f812a80ef99b1c25d452acf6887a0b4624702617472e534674736c7097e970e6f180024eb527d606b92b44b409f24a
|
data/lib/eswrapsearch.rb
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
require 'elasticsearch'
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
class EsWrapSearch
|
6
|
+
|
7
|
+
|
8
|
+
#Initialize
|
9
|
+
def initialize(host='localhost',port=9200,tflog=false)
|
10
|
+
@@eclient = Elasticsearch::Client.new log: tflog, host: "http://#{host}:#{port}"
|
11
|
+
|
12
|
+
@lastresult = ''
|
13
|
+
@size = 0
|
14
|
+
@aggs = {}
|
15
|
+
@must_values = []
|
16
|
+
@must_not_values = []
|
17
|
+
@should_values = []
|
18
|
+
@body = {"size" => @size,
|
19
|
+
"query" =>
|
20
|
+
{"filtered" =>
|
21
|
+
{"filter" =>
|
22
|
+
{"bool" =>
|
23
|
+
{ "must" => @must_values,
|
24
|
+
"should" => @should_values,
|
25
|
+
"must_not" => @must_not_values
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
},
|
30
|
+
"aggs" => @aggs
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def version
|
35
|
+
"0.0.2"
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def reset
|
40
|
+
@size = 0
|
41
|
+
@aggs = {}
|
42
|
+
@must_values = []
|
43
|
+
@must_not_values = []
|
44
|
+
@should_values = []
|
45
|
+
@body = {"size" => @size,
|
46
|
+
"query" =>
|
47
|
+
{"filtered" =>
|
48
|
+
{"filter" =>
|
49
|
+
{"bool" =>
|
50
|
+
{ "must" => @must_values,
|
51
|
+
"should" => @should_values,
|
52
|
+
"must_not" => @must_not_values
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
},
|
57
|
+
"aggs" => @aggs
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def update
|
62
|
+
@body = {"size" => @size,
|
63
|
+
"query" =>
|
64
|
+
{"filtered" =>
|
65
|
+
{"filter" =>
|
66
|
+
{"bool" =>
|
67
|
+
{ "must" => @must_values,
|
68
|
+
"should" => @should_values,
|
69
|
+
"must_not" => @must_not_values
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
},
|
74
|
+
"aggs" => @aggs
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
#Set amount of source documents to be returned
|
79
|
+
def set_size(size)
|
80
|
+
@size = size
|
81
|
+
self.update
|
82
|
+
end
|
83
|
+
|
84
|
+
#Get amount of source documents to be returned
|
85
|
+
def get_size
|
86
|
+
@size
|
87
|
+
end
|
88
|
+
|
89
|
+
#what the current query looks like
|
90
|
+
def get_body
|
91
|
+
@body
|
92
|
+
end
|
93
|
+
|
94
|
+
#Bool query for match
|
95
|
+
def must_match(field,value)
|
96
|
+
@must_values << {"query" => {"match" => { field => value}} }
|
97
|
+
self.update
|
98
|
+
end
|
99
|
+
|
100
|
+
#Bool query for match
|
101
|
+
def must_not_match(field,value)
|
102
|
+
@must_not_values << {"query" => {"match" => { field => value}} }
|
103
|
+
self.update
|
104
|
+
end
|
105
|
+
|
106
|
+
#Bool query for match
|
107
|
+
def should_match(field,value)
|
108
|
+
@should_values << {"query" => {"match" => { field => value}} }
|
109
|
+
self.update
|
110
|
+
end
|
111
|
+
|
112
|
+
#Bool query for range
|
113
|
+
def must_range(field,from_value,to_value)
|
114
|
+
@must_values << {"range" => {field => { "gte" => from_value, "lte" => to_value } } }
|
115
|
+
self.update
|
116
|
+
end
|
117
|
+
|
118
|
+
#Bool query for range
|
119
|
+
def must_not_range(field,from_value,to_value)
|
120
|
+
@must_not_values << {"range" => {field => { "gte" => from_value, "lte" => to_value } } }
|
121
|
+
self.update
|
122
|
+
end
|
123
|
+
|
124
|
+
#Bool query for range
|
125
|
+
def should_range(field,from_value,to_value)
|
126
|
+
@should_values << {"range" => {field => { "gte" => from_value, "lte" => to_value } } }
|
127
|
+
self.update
|
128
|
+
end
|
129
|
+
|
130
|
+
#Execute search, returns complete results set "ignoring type atm"
|
131
|
+
def search(index,type)
|
132
|
+
@lastresult = @@eclient.search index: index, body: @body
|
133
|
+
@lastresult
|
134
|
+
end
|
135
|
+
|
136
|
+
#returns last result set
|
137
|
+
def last_result
|
138
|
+
@lastresult
|
139
|
+
end
|
140
|
+
|
141
|
+
#returns source documents
|
142
|
+
def get_source
|
143
|
+
@lastresult["hits"]["hits"]
|
144
|
+
end
|
145
|
+
|
146
|
+
#private function to recursively build aggregations, will expand in the future
|
147
|
+
def build_agg(inaggs)
|
148
|
+
if inaggs.size == 1
|
149
|
+
if inaggs[0]['interval'] == nil
|
150
|
+
{inaggs[0]["field"] => {inaggs[0]["type"] => ({"field" => inaggs[0]["script"]} == nil) ? {"field" => inaggs[0]["field"]}:{"script" => inaggs[0]["script"] , "lang" => "expression" }} }
|
151
|
+
else
|
152
|
+
{inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"], "interval" => inaggs[0]['interval'] }} }
|
153
|
+
end
|
154
|
+
else
|
155
|
+
if inaggs[0]['interval'] == nil
|
156
|
+
{inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"]}, "aggs" => build_agg(inaggs[1..inaggs.size]) } }
|
157
|
+
else
|
158
|
+
{inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"], "interval" => inaggs[0]['interval'] }, "aggs" => build_agg(inaggs[1..inaggs.size])} }
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
private :build_agg
|
163
|
+
|
164
|
+
#add aggregation method
|
165
|
+
#Takes an array of hashes with field name and the type of aggregation. Very much like a group by in SQL
|
166
|
+
#Example: obj.set_aggs( [ {"field" => "datatype", "type" => "terms" },{"field" => "totalbytes", "type" => "sum", "script" => "doc['totalbytes'].value/1024/1024/1024"} ] )
|
167
|
+
#This array creates a bucket that will group by all different terms in the field "datatype" and then sub-bucket into a bucket for the field "totalbytes" and sum it with a script that converts it into GB
|
168
|
+
#If the result needed no scripting, dont include it in the aggs
|
169
|
+
# Example: obj.set_aggs( [ {"field" => "datatype", "type" => "terms" },{"field" => "totalbytes", "type" => "sum"} ] )
|
170
|
+
def set_aggs(inaggs)
|
171
|
+
if inaggs.size == 1
|
172
|
+
@aggs = {inaggs[0]["field"] => {inaggs[0]["type"] => ({"field" => inaggs[0]["script"]} == nil) ? {"field" => inaggs[0]["field"]}:{"script" => inaggs[0]["script"] , "lang" => "expression" } } }
|
173
|
+
else
|
174
|
+
if inaggs[0]['interval'] == nil
|
175
|
+
@aggs = {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"]}, "aggs" => build_agg(inaggs[1..inaggs.size]) } }
|
176
|
+
else
|
177
|
+
@aggs = {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"], "interval" => inaggs[0]['interval'] }, "aggs" => build_agg(inaggs[1..inaggs.size])} }
|
178
|
+
end
|
179
|
+
end
|
180
|
+
self.update
|
181
|
+
end
|
182
|
+
|
183
|
+
#returns the buckets from the last query
|
184
|
+
def get_aggs
|
185
|
+
@lastresult["aggregations"]
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eswrapsearch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eugene Kuhn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple wrapper for Elasticsearch
|
14
|
+
email: kruiserx@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/eswrapsearch.rb
|
20
|
+
homepage: http://rubygems.org/gems/eswrapsearch
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.2.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: ElasticSearch wrapper
|
44
|
+
test_files: []
|