我是弹性搜索的新手,我的问题是如何使用特定的必需字段查询弹性搜索以创建一个JSON
字符串作为响应。
我正在使用Elastic Search Client API,并且已经使用Elastic Search Client连接到主机。
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