简体   繁体   English

使用AJAX在Elasticsearch中查询动态聚合

[英]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. 如果您需要更多代码,只需询问。

You need to sent your query as POST rather than GET. 您需要以POST而不是GET的形式发送查询。 When you are sending it as GET , though you are hitting the _search API , the query part is not considered. 当您以GET形式发送它时,尽管您点击了_search API,但不考虑查询部分。 Hence you will just get the top N documents out of the index. 因此,您只会从索引中获取前N个文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM