class EsWrapSearch

Very early stages for a nice wrapper

Public Class Methods

new(host='localhost',port=9200,tflog=false) click to toggle source

Initialize

# File lib/eswrapsearch.rb, line 9
def initialize(host='localhost',port=9200,tflog=false)
        @@eclient = Elasticsearch::Client.new log: tflog, host: "http://#{host}:#{port}"

        @lastresult = ''
        @size = 0
        @aggs = {}
        @must_values = []
        @must_not_values = []
        @should_values = []
        @body = {"size" => @size, 
                         "query" => 
                                        {"filtered" => 
                                                {"filter" => 
                                                        {"bool" => 
                                                                {      "must" => @must_values,
                                                                        "should" => @should_values,
                                                                        "must_not" => @must_not_values
                                                                }
                                                        } 
                                                } 
                                        },
                         "aggs" => @aggs
                        }
end

Public Instance Methods

get_aggs() click to toggle source

returns the buckets from the last query

# File lib/eswrapsearch.rb, line 184
def get_aggs
        @lastresult["aggregations"]
end
get_body() click to toggle source

what the current query looks like

# File lib/eswrapsearch.rb, line 90
def get_body
        @body
end
get_size() click to toggle source

Get amount of source documents to be returned

# File lib/eswrapsearch.rb, line 85
def get_size
        @size
end
get_source() click to toggle source

returns source documents

# File lib/eswrapsearch.rb, line 142
def get_source
        @lastresult["hits"]["hits"]
end
last_result() click to toggle source

returns last result set

# File lib/eswrapsearch.rb, line 137
def last_result
        @lastresult
end
must_match(field,value) click to toggle source

Bool query for match

# File lib/eswrapsearch.rb, line 95
def must_match(field,value)
        @must_values << {"query" => {"match" => { field => value}}  }
        self.update
end
must_not_match(field,value) click to toggle source

Bool query for match

# File lib/eswrapsearch.rb, line 101
def must_not_match(field,value)
        @must_not_values << {"query" => {"match" => { field => value}}  }
        self.update
end
must_not_range(field,from_value,to_value) click to toggle source

Bool query for range

# File lib/eswrapsearch.rb, line 119
def must_not_range(field,from_value,to_value)
        @must_not_values << {"range" => {field =>  { "gte" => from_value, "lte" => to_value } } }
        self.update
end
must_range(field,from_value,to_value) click to toggle source

Bool query for range

# File lib/eswrapsearch.rb, line 113
def must_range(field,from_value,to_value)
        @must_values << {"range" => {field =>  { "gte" => from_value, "lte" => to_value } } }
        self.update
end
reset() click to toggle source
# File lib/eswrapsearch.rb, line 39
def reset
        @size = 0
        @aggs = {}
        @must_values = []
        @must_not_values = []
        @should_values = []
        @body = {"size" => @size, 
                         "query" => 
                                        {"filtered" => 
                                                {"filter" => 
                                                        {"bool" => 
                                                                {      "must" => @must_values,
                                                                        "should" => @should_values,
                                                                        "must_not" => @must_not_values
                                                                }
                                                        } 
                                                } 
                                        },
                         "aggs" => @aggs
                        }
end
set_aggs(inaggs) click to toggle source

add aggregation method Takes an array of hashes with field name and the type of aggregation. Very much like a group by in SQL Example: obj.set_aggs( [ {“field” => “datatype”, “type” => “terms” },{“field” => “totalbytes”, “type” => “sum”, “script” => “doc.value/1024/1024/1024”} ] ) 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 If the result needed no scripting, dont include it in the aggs

Example: obj.set_aggs( [ {"field" => "datatype", "type" =>  "terms" },{"field" => "totalbytes", "type" => "sum"} ] )
# File lib/eswrapsearch.rb, line 170
def set_aggs(inaggs)
        if inaggs.size == 1
                @aggs = {inaggs[0]["field"] => {inaggs[0]["type"] => ({"field" => inaggs[0]["script"]} == nil) ? {"field" => inaggs[0]["field"]}:{"script" => inaggs[0]["script"] , "lang" => "expression" } } }
        else
                if inaggs[0]['interval'] == nil              
                        @aggs = {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"]}, "aggs" =>  build_agg(inaggs[1..inaggs.size])  } }
                else
                        @aggs = {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"], "interval" => inaggs[0]['interval'] }, "aggs" =>  build_agg(inaggs[1..inaggs.size])} }
                end  
        end
        self.update
end
set_size(size) click to toggle source

Set amount of source documents to be returned

# File lib/eswrapsearch.rb, line 79
def set_size(size)
        @size = size
        self.update
end
should_match(field,value) click to toggle source

Bool query for match

# File lib/eswrapsearch.rb, line 107
def should_match(field,value)
        @should_values << {"query" => {"match" => { field => value}}  }
        self.update
end
should_range(field,from_value,to_value) click to toggle source

Bool query for range

# File lib/eswrapsearch.rb, line 125
def should_range(field,from_value,to_value)
        @should_values << {"range" => {field =>  { "gte" => from_value, "lte" => to_value } } }
        self.update
end
update() click to toggle source
# File lib/eswrapsearch.rb, line 61
def update
        @body = {"size" => @size, 
                         "query" => 
                                        {"filtered" => 
                                                {"filter" => 
                                                        {"bool" => 
                                                                {      "must" => @must_values,
                                                                        "should" => @should_values,
                                                                        "must_not" => @must_not_values
                                                                }
                                                        } 
                                                } 
                                        },
                         "aggs" => @aggs
                        }
end
version() click to toggle source
# File lib/eswrapsearch.rb, line 34
def version
        "0.0.2"
end

Private Instance Methods

build_agg(inaggs) click to toggle source

private function to recursively build aggregations, will expand in the future

# File lib/eswrapsearch.rb, line 147
def build_agg(inaggs)
        if inaggs.size == 1
                if inaggs[0]['interval'] == nil              
                        {inaggs[0]["field"] => {inaggs[0]["type"] => ({"field" => inaggs[0]["script"]} == nil) ? {"field" => inaggs[0]["field"]}:{"script" => inaggs[0]["script"] , "lang" => "expression" }} }
                else
                        {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"], "interval" => inaggs[0]['interval'] }} }
                end  
        else
                if inaggs[0]['interval'] == nil              
                        {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"]}, "aggs" =>  build_agg(inaggs[1..inaggs.size])  } }
                else
                        {inaggs[0]["field"] => {inaggs[0]["type"] => {"field" => inaggs[0]["field"], "interval" => inaggs[0]['interval'] }, "aggs" =>  build_agg(inaggs[1..inaggs.size])} }
                end  
        end   
end