首页 > 解决方案 > 如何使用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

标签: javaapielasticsearch

解决方案


您是否尝试过官方文档中的示例?


推荐阅读