# Return only needed fields in response without metadata

**URL:** <https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639>\
**Category:** Elasticsearch\
**Created:** [October 1, 2020, 10:00am UTC](https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639 "2020-10-01T10:00:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Name](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_name/32/45771_2.png) [@Nick\_Name](https://discuss.elastic.co/u/Nick_Name)\
**Post date:** [October 1, 2020, 10:00am UTC](https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639/1 "2020-10-01T10:00:41Z")

</div>

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:

```auto
    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:

```auto
        {
          "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"
                  ]
                }
              }
            ]
          }
        }

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 1, 2020, 10:40am UTC](https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639/2 "2020-10-01T10:40:01Z")

</div>

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

In the REST API, you can use filter\_path. See:

> **[Common options | Elasticsearch Reference \[7.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#common-options-response-filtering)**

But this is not available in the HighLevel Rest Client.

---

<div class="post-metadata">

**Author:** ![Nick\_Name](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_name/32/45771_2.png) [@Nick\_Name](https://discuss.elastic.co/u/Nick_Name)\
**Post date:** [October 1, 2020, 11:47am UTC](https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639/3 "2020-10-01T11:47:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 1, 2020, 1:24pm UTC](https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639/4 "2020-10-01T13:24:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 29, 2020, 1:24pm UTC](https://discuss.elastic.co/t/return-only-needed-fields-in-response-without-metadata/250639/5 "2020-10-29T13:24:42Z")

</div>

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