# Move from High Level REST client to Java API Client

**URL:** <https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 3, 2023, 10:58am UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304 "2023-10-03T10:58:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt4589](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@matt4589](https://discuss.elastic.co/u/matt4589)\
**Post date:** [October 3, 2023, 10:58am UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304/1 "2023-10-03T10:58:42Z")

</div>

I have to move from High Level REST client to Java API Client  
I would like to show one method I have to replace and learn from that . This is old method:

```auto
public List<Map<String, Object>> getPublicFilters() {

		SearchRequest searchRequest = new SearchRequest("filter-template");

		searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchQuery("private", false)));

		return Arrays.stream(client.get().search(searchRequest, RequestOptions.DEFAULT).getHits().getHits())
				.map(SearchHit::getSourceAsMap).collect(Collectors.toList());
	}

```

I understand it have to be replaced to something like this:

```auto
this.elasticsearchClient.search(searchRequest -> searchRequest
   .index(indexName)
   .query(queryBuilder ->
           queryBuilder.match(matchQBuilder->
                  matchQBuilder.field("route")
                .query(searchText))),Flight.class
);

```

indexName is probably "filter-template"  
and I assume "route" have to be replaced with "private"  
but what about searchText ?  
And Flight.class - is it Object in ma case ?

---

<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 3, 2023, 11:31am UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304/2 "2023-10-03T11:31:04Z")

</div>

Welcome!

> indexName is probably "filter-template"

Yes.

> and I assume "route" have to be replaced with "private"

Yes.

> but what about searchText ?

I believe it should be `false`. But I'd use a `term` query instead of a match query as I guess your field is mapped as a boolean...

> And Flight.class - is it Object in ma case ?

I'd recommend it to be your Java Bean (the business object). That's one of the cool additions of this Java client. You can directly index your java beans and get back your java beans at search time...

---

<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 4, 2023, 9:59am UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304/4 "2023-10-04T09:59:49Z")

</div>

You should not start a new project with that one for sure.  
If you are planning to use 8.x, switch to it. I don't think the HLClient will be able to support it and actually a lot of new APIs are not available in the HLClient.

In general, I'd recommend:

- Upgrade your 7.x or below cluster to 7.17.x
- Switch to the new Java API Client with the 7.17.x version
- Then, when possible, upgrade your client and cluster to 8.x (the latest version)

---

<div class="post-metadata">

**Author:** ![matt4589](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@matt4589](https://discuss.elastic.co/u/matt4589)\
**Post date:** [October 4, 2023, 10:23am UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304/5 "2023-10-04T10:23:48Z")

</div>

But is seems there will be no problem to use HLClient with 8.x , no ?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/e/3e9ef00606b5240c0db70d68456b254cd9682983.png)

---

<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 4, 2023, 12:03pm UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304/6 "2023-10-04T12:03:00Z")

</div>

> [@matt4589](#):
>
> But is seems there will be no problem to use HLClient with 8.x , no ?

I said that you should not. Not that you can't... I'd really avoid it if possible.  
This setting has been made for existing code when people want to upgrade the cluster to 8.x but don't have time to rewrite the code using the new Java API Client.

---

<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:** [November 1, 2023, 12:03pm UTC](https://discuss.elastic.co/t/move-from-high-level-rest-client-to-java-api-client/344304/7 "2023-11-01T12:03:21Z")

</div>

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