简体   繁体   English

Java 相当于 elasticsearch CURL 查询?

[英]Java equivalent of elasticsearch CURL query?

My project had a new reqiurement of migrating all datas which are currently in postresql to elasticsearch.我的项目有一个新的要求,即将所有当前在 postresql 中的数据迁移到 elasticsearch。 Successfully migrated all my datas, but I am stuck with writing java code to search for some datas in elastic search index.成功迁移了我所有的数据,但我坚持编写 java 代码来搜索弹性搜索索引中的一些数据。

Sample structure of a hit in index is attached in the below image:下图中附加了索引命中的示例结构: 在此处输入图像描述

I need to find average of activity.attentionLevel from the index.我需要从索引中找到activity.attentionLevel的平均值。

I wrote something like below query to find average:我写了类似下面的查询来查找平均值:

GET proktor-activities/_search
{
"aggs" : {
    "avg_attention" : { 
        "avg" : { 
            "script" : "Float.parseFloat(doc['activity.attentionLevel.keyword'].value)" }
         }
    }
}

please help me to find java equivalent code for doing the same.请帮我找到 java 等效代码来做同样的事情。

thanks in advance提前致谢

Using Elastic's RestHighLevel API would be something like this:使用 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

More information here .更多信息在这里

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

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