Patrícia Villela
Patrícia Villela

Reputation: 839

Querying dynamic aggregation in elasticsearch with AJAX

I'm trying to build an aggregation dynamically based on a group of fields queried using the mapping. Here's the code.

$(document).ready(function(){                                         

        var query = {                                                               
            aggs:{                                                                  
                sum_of_too_many_tries:{                                                       
                    sum:{                                                           
                        field:"reason.too_many_tries"                                                                                                                   
                    }                                                               
                }                                                                   
            }                                                                       
        };                                                                          
        $.ajax({                                                                    
            url: "http://10.138.16.125:9200/log/blocks/_mapping",                
            type: "get",                                                            
            async: false,                                                           
            crossDomain: true,                                                      
            success: function(data) {                                               
                var fields = Object.keys(data.log.mappings.blocks.properties.reasons.properties);
                    $.ajax({                                                        
                    url: "http://10.138.16.125:9200/log/blocks/_search",         
                    crossDomain: true,                                              
                    async: false,                                                   
                    type: "get",                                                    
                    dataType: "json",                                               
                    data: JSON.stringify(query),                                    
                    success: function(response) {                                   
                        $("#dump").append(var_dump(response));                      
                    }                                                               
                });                                                                 
            }                                                                       
        });                                                                         
    });

When I do the query, it returns the hits normally, without the aggregations. What am I doing wrong?

If you need more code, just ask.

Upvotes: 1

Views: 702

Answers (1)

Vineeth Mohan
Vineeth Mohan

Reputation: 19273

You need to sent your query as POST rather than GET. When you are sending it as GET , though you are hitting the _search API , the query part is not considered. Hence you will just get the top N documents out of the index.

Upvotes: 2

Related Questions