查询动态聚合在elasticsearch与AJAX动态、elasticsearch、AJAX

2023-09-10 20:24:29 作者:じ我們像糖‘甜到悲傷┗

我试图建立动态的基础上一组字段的查询使用映射的集合。 这里的code。

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?

如果你需要更多的code,只问。

If you need more code, just ask.

推荐答案

您需要发送的查询作为POST而不是GET。 当您发送它为GET,但是你打了_search API,不考虑查询的一部分。因此,你只会得到的前N个文件列的索引。

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.