Return only needed fields in response without metadata

Can I specify my SearchSourceBuilder or SearchRequest for return in SearchResponse only fields without metadata? Include fields and Exclude don't work. I want to return only code.keyword and id.keyword fields in response. This is my SearchRequest:

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                        .size(size)
                        .from(from);
                String[] includeFields = new String[]{"code.keyword", "id.keyword"};
                String[] excludeFields = new String[]{"index", "id", "type", "score", "totalHits"};
                sourceBuilder.docValueField("code.keyword");
                sourceBuilder.docValueField("id.keyword");
                sourceBuilder.fetchSource(false);
                QueryBuilder query = QueryBuilders.matchAllQuery();
                SearchRequest request = new SearchRequest("indexName");
                sourceBuilder.query(query);
                sourceBuilder.timeout(TimeValue.timeValueMinutes(5));
                request.source(sourceBuilder);

And it's my SearchResponse:

        {
          "took": 40,
          "timed_out": false,
          "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
          },
          "hits": {
            "total": {
              "value": 10000,
              "relation": "gte"
            },
            "max_score": 1,
            "hits": [
              {
                "_index": "core_event_usvc_provided",
                "_type": "_doc",
                "_id": "11a39e86-2b86-430e-9042-d59d58fb16d6",
                "_score": 1,
                "fields": {
                  "id.keyword": [
                    "11a39e86-2b86-430e-9042-d59d58fb16d6"
                  ],
                  "code.keyword": [
                    "1.1.2-1"
                  ]
                }
              },
              {
                "_index": "core_event_usvc_provided",
                "_type": "_doc",
                "_id": "7623a866-3f2e-4373-9b56-f819bac8717f",
                "_score": 1,
                "fields": {
                  "id.keyword": [
                    "7623a866-3f2e-4373-9b56-f819bac8717f"
                  ],
                  "code.keyword": [
                    "3.02.02"
                  ]
                }
              },
              {
                "_index": "core_event_usvc_provided",
                "_type": "_doc",
                "_id": "af6ebc01-8bf8-4e4f-978c-55a6a5ee898b",
                "_score": 1,
                "fields": {
                  "id.keyword": [
                    "af6ebc01-8bf8-4e4f-978c-55a6a5ee898b"
                  ],
                  "code.keyword": [
                    "3.29.01"
                  ]
                }
              },
              {
                "_index": "core_event_usvc_provided",
                "_type": "_doc",
                "_id": "26887750-23c0-4309-8985-e31aecf9460c",
                "_score": 1,
                "fields": {
                  "id.keyword": [
                    "26887750-23c0-4309-8985-e31aecf9460c"
                  ],
                  "code.keyword": [
                    "156.25.15"
                  ]
                }
              },
              {
                "_index": "core_event_usvc_provided",
                "_type": "_doc",
                "_id": "8bdbf877-3f08-439f-ad5a-02a3b1f337ef",
                "_score": 1,
                "fields": {
                  "id.keyword": [
                    "8bdbf877-3f08-439f-ad5a-02a3b1f337ef"
                  ],
                  "code.keyword": [
                    "200.14.1.1"
                  ]
                }
              }
            ]
          }
        }

No you can't exclude that. Why do you want to exclude this BTW?

In the REST API, you can use filter_path. See:

But this is not available in the HighLevel Rest Client.

I know about filter_path and it;s work, but I need to realize this in java. Can I return only needed fields in response using java? Can I realize this using HighLevel Rest Client? or may be exist alternative way

No you can not do that with the HLClient. The only way for doing that is by using the LLClient (Low Level Client). But then you won't be able to get high level response beans. So you will have to parse manually the result.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.