1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, that worked. Thank you. Is there no way of requesting via GET like I do with curl?
Hello, I have similar problem. Do you know to help me? stackoverflow.com/questions/40757922/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.