java - 如何使用java查询具有特定需要字段的elasticsearch
问题描述
我是弹性搜索的新手,我的问题是如何使用特定所需字段查询弹性搜索以创建JSON
字符串作为响应。
我正在使用弹性搜索客户端 API,并且我已经使用弹性搜索客户端连接到主机。
public class ElasticSearchClient {
private static final String HOST = EnvConf.getProperty("elastic.host");
private static final int PORT = EnvConf.getAsInteger("elastic.port");
private final RestHighLevelClient restClient;
public ElasticSearchClient() {
RestClientBuilder builder = RestClient.builder(
new HttpHost(HOST, PORT))
.setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(5000)
.setSocketTimeout(10000))
.setMaxRetryTimeoutMillis(30000);
restClient = new RestHighLevelClient(builder);
}
public IndexResponse index(String index , String type , XContentBuilder contentBuilder) throws IOException {
IndexRequest indexRequest = new IndexRequest(index, type)
.source(contentBuilder);
return restClient.index(indexRequest , RequestOptions.DEFAULT);
}
}
and this is the JSON of my elasticsearch:
{
"columns": [
{
"text": "test_name",
"value": "test_name"
},
{
"text": "status",
"value": "status"
},
{
"text": "first_failure",
"value": "first_failure"
},
{
"text": "start_timestamp",
"value": "start_timestamp"
},
{
"text": "duration_sec",
"value": "duration_sec"
},
{
"text": "jira_test_key",
"value": "jira_test_key"
},
{
"text": "job_build_number",
"value": "job_build_number"
},
{
"text": "test_case",
"value": "test_case"
},
{
"text": "link_issue",
"value": "link_issue"
}
],
"fontSize": "100%",
"gridPos": {
"h": 11,
"w": 24,
"x": 0,
"y": 15
},
"id": 4,
"interval": "2h",
"links": [],
"pageSize": null,
"scroll": true,
"showHeader": true,
"sort": {
"col": 0,
"desc": false
},
"styles": [
{
"alias": "Time",
"dateFormat": "YYYY-MM-DD HH:mm:ss",
"pattern": "Time",
"type": "date"
},
{
"alias": "",
"colorMode": null,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"dateFormat": "YYYY-MM-DD HH:mm:ss",
"decimals": 2,
"link": true,
"linkTargetBlank": true,
"linkTooltip": "Jira Test",
"linkUrl": "https://indeni.atlassian.net/browse/${__cell}",
"mappingType": 1,
"pattern": "jira_test_key",
"sanitize": false,
"thresholds": [],
"type": "string",
"unit": "short"
},
{
"alias": "",
"colorMode": null,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"dateFormat": "YYYY-MM-DD HH:mm:ss",
"decimals": 2,
"link": true,
"linkTargetBlank": true,
"linkTooltip": "Jenkins Job",
"linkUrl": "http://jenkins.aws.indeni-ops.com/view/build/job/build_test/${__cell}",
"mappingType": 1,
"pattern": "job_build_number",
"thresholds": [],
"type": "string",
"unit": "short"
},
{
"alias": "",
"colorMode": "value",
"colors": [
"rgba(50, 172, 45, 0.97)",
"rgba(237, 129, 40, 0.89)",
"rgba(245, 54, 54, 0.9)"
],
"dateFormat": "YYYY-MM-DD HH:mm:ss",
"decimals": 2,
"mappingType": 1,
"pattern": "duration_sec",
"preserveFormat": false,
"sanitize": false,
"thresholds": [
"300",
"600"
],
"type": "number",
"unit": "short",
"valueMaps": []
}
],
"targets": [
{
"bucketAggs": [],
"metrics": [
{
"field": "select field",
"id": "1",
"meta": {},
"settings": {
"size": 500
},
"type": "raw_document"
}
],
"query": "indeni_version:0.0.0.develop AND status:FAILURE",
"refId": "B",
"timeField": "start_timestamp"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Failures Table",
"transform": "json",
"transparent": true,
"type": "table"
}
我想添加另一个函数,它将在弹性搜索上执行查询命令并返回响应代码JSON
类型。
想要的领域是test_name
, first_failure
,jira_test_key
解决方案
您是否尝试过官方文档中的示例?
推荐阅读
- reactjs - TypeScript - 如何在可重用的 React 组件中使用泛型
- visual-studio-code - 调试控制台中出现 VSCode 神秘的命令提示符窗口
- mysql - mysql触发器中定义器'root@%'和定义器'root@localhost'之间的区别?
- javascript - 如何在反应中的一组元素中识别单个 div 元素?
- c# - 如何在 WPF 运行时动态获取数据网格中的嵌套行?
- android - 指定的命名范围不存在
- python - 如果前一个服务器抛出错误,如何让 Python 尝试备用服务器
- python - 如何在列之间进行求和和除以更改数据框中现有列的值?
- html - 无法使用 xpath 查询提取值
- makefile - 将 Fortran 模块链接在一起