lundi 17 août 2015

Ruby hash keeps overwriting existing hash rather than adding other hashes to it

I have a builder type method that can be called to build up a json. Basically the end result should look like this:

{
    "aggs": {
        "buzzsumo_status_type_group": {
            "terms": {
                "field": "buzzsumo_status_type"
            }
        },
        "buzzsumo_group": {
            "aggs": {
                "interactions": {
                    "avg": {
                        "field": "total_interactions"
                    }
                },
                "shares": {
                    "avg": {
                        "field": "num_shares"
                    }
                },
                "comments": {
                    "avg": {
                        "field": "num_comments"
                    }
                },
                "likes": {
                    "avg": {
                        "field": "num_likes"
                    }
                }
            }
        }
    }
}

The problem is the method seems to continue to overwrite existing aggs hash and the buggy result looks like below:

{
    "aggs": {
        "buzzsumo_status_type_group": {
            "aggs": {
                "interactions": {
                    "avg": {
                        "field": "total_interactions"
                    }
                }
            }
        }
    }
}

The builder type ruby code that does this is below:

    query_builder.aggregation("buzzsumo_status_type").nested.avg("total_interactions")
    .avg("num_shares").avg("num_comments").avg("num_likes")

The query_builder class definition is below:

  class QueryBuilder

        attr_reader :query_hash, :context, :condition_hash, :filter_hash

        def initialize
            @query_hash = {}
            @aggs_hash = {}
            @main_hash = {"query" => { "filtered" => { "query"=> { "bool" => @query_hash}}}}

        end

        def serialize

            if(!@aggs_hash.empty?)
                @main_hash["aggs"] = @aggs_hash
            end

            return @main_hash
        end


        def aggregation(field_name)
                @context = "aggregation"+"_"+field_name
                @aggs_hash = { field_name+"_group" => { "terms"=> {"field" => field_name}}}
            return self

        end

        def nested
            if( @context.include?("aggregation") )
                @context.slice!("aggregation_")
                @aggs_hash[@context+"_group"] = {}
                @context = @context+"_nested"
                @context = "aggregation_"+@context
            end
            return self
        end

        def avg(field_name)
            category = field_name.split("_")[-1]
            if( @context.include?("aggregation") && @context.include?("nested") )
                @context.slice!("aggregation_")
                @context.slice!("_nested")
                stats_hash = {"avg" => {"field" => field_name}}
                group_by_field = @context

                if(@aggs_hash.has_key?(group_by_field+"_group"))
                    if(@aggs_hash[group_by_field+"_group"].has_key?("aggs"))
                        @aggs_hash[group_by_field+"_group"]["aggs"].merge!({category => stats_hash})
                    else
                        @aggs_hash[group_by_field+"_group"] = {"aggs" => {category => {"avg" => {"field"=> field_name}}}}
                    end
                end
            end
            return self
        end
    end

Aucun commentaire:

Enregistrer un commentaire