elasticsearch,curl,kibana,Java,elasticsearch,Curl,Kibana" /> elasticsearch,curl,kibana,Java,elasticsearch,Curl,Kibana" />

Java等价于elasticsearch CURL查询?

Java等价于elasticsearch CURL查询?,java,elasticsearch,curl,kibana,Java,elasticsearch,Curl,Kibana,我的项目有一个新的要求,就是将当前postresql中的所有数据迁移到elasticsearch。成功迁移了我的所有数据,但我一直在编写java代码,以便在弹性搜索索引中搜索一些数据 点击索引的示例结构附在下图中: 我需要从索引中找到活动的平均值。attentionLevel 我写了下面这样的查询来查找平均值: GET proktor-activities/_search { "aggs" : { "avg_attention" : {

我的项目有一个新的要求,就是将当前postresql中的所有数据迁移到elasticsearch。成功迁移了我的所有数据,但我一直在编写java代码,以便在弹性搜索索引中搜索一些数据

点击索引的示例结构附在下图中:

我需要从索引中找到
活动的平均值。attentionLevel

我写了下面这样的查询来查找平均值:

GET proktor-activities/_search
{
"aggs" : {
    "avg_attention" : { 
        "avg" : { 
            "script" : "Float.parseFloat(doc['activity.attentionLevel.keyword'].value)" }
         }
    }
}
请帮助我找到与java等效的代码,以便执行相同的操作


提前感谢

使用Elastic的RestHighLevel API会是这样的:

    // Create the client
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 9200, "http")));

    // Specify the aggregation request
    SearchRequest searchRequest = new SearchRequest("proktor-activities");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.aggregation(AggregationBuilders
        .avg("avg_attention")
        .field("activity.attentionLevel"));

    searchRequest.source(searchSourceBuilder);

    // Execute the aggreagation with Elasticsearch
    SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

    // Read the aggregation result
    Aggregations aggregations = response.getAggregations();
    Avg averageAttention = aggregations.get("avg_attention");
    double result = averageAttention.getValue();

    client.close(); // get done with the client
更多信息