java - Java等效于elasticsearch CURL查询?
问题描述
我的项目有一个新的要求,将当前在 postresql 中的所有数据迁移到 elasticsearch。成功迁移了我的所有数据,但我坚持编写 java 代码来搜索弹性搜索索引中的一些数据。
我需要activity.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
更多信息在这里。
推荐阅读
- regex - perl grep 多重模式
- mysql - MySQL 在比较 datetime 列时返回 0 个结果
- rust - Rust 函数中的值所有权
- c# - 导航栏不够宽
- html - 限制相对 div 中的链接宽度
- javascript - 使用 nodeJS/AngularJS 在 Windows 上打开文件
- c++ - “在 C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\ 中找不到版本 4.1.1 的包 System.Net.Http。”
- javascript - Aurelia 使用打印机打印标签
- node.js - 如何使用覆盖实现交集类型
- jquery - keydown 没有捕捉到输入键