# 8.1.0 Java Client - SearchRequest example

**URL:** <https://discuss.elastic.co/t/8-1-0-java-client-searchrequest-example/299640>\
**Category:** Elasticsearch\
**Created:** [March 14, 2022, 3:52pm UTC](https://discuss.elastic.co/t/8-1-0-java-client-searchrequest-example/299640 "2022-03-14T15:52:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/valh/32/103015_2.png) [@Valh](https://discuss.elastic.co/u/Valh)\
**Post date:** [March 14, 2022, 3:52pm UTC](https://discuss.elastic.co/t/8-1-0-java-client-searchrequest-example/299640/1 "2022-03-14T15:52:32Z")

</div>

Hi, I'm struggling to understand how to do convert my code from the HLRC to the new Elasticsearch Java API Client.

Could someone please give me an example? The documentation is very unclear on this subject.

Fonctional HLRC code:

```java
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = response.getHits();

// Count the total of hits to determine terms frequency
LinkedHashMap<Integer, Integer> mapFrequency = new LinkedHashMap<Integer, Integer>();
for (SearchHit hit : searchHits.getHits()) {
    Integer id = Integer.valueOf(hit.getId());
    Integer termsFrequency = 0;

    HighlightField fieldFR = hit.getHighlightFields().get(FIELD_CV_FR);
    HighlightField fieldEN = hit.getHighlightFields().get(FIELD_CV_EN);

    Integer hlFR = (fieldFR != null) ? fieldFR.getFragments().length : 0;
    Integer hlEN = (fieldEN != null) ? fieldEN.getFragments().length : 0;
    termsFrequency = (hlFR >= hlEN) ? hlFR : hlEN;

    mapFrequency.put(id, termsFrequency);
}

```

What I've tried so far:

```auto
try {
SearchResponse<CVDocument> response = this.client.search(b -> b
    .index(DOCUMENTS_INDEX)
    .highlight(highlightBuilder.build())
    .source(sourceBuilder),
    CVDocument.class
);

// Count the total of hits to determine terms frequency
LinkedHashMap<Integer, Integer> mapFrequency = new LinkedHashMap<Integer, Integer>();
for (CVDocument hit : response.hits()) {
    // foreach not applicable to type 'co.elastic.clients.elasticsearch.core.search.HitsMetadata<com.intl.service.CVDocument>'
}

```

My object representing my index :

```auto
@Data
class CVDocument {
    @JsonProperty("_id")
    private Integer id;

    @JsonProperty("cvFR")
    private Integer cvFR;

    @JsonProperty("cvEN")
    private Integer cvEN;
}

```

Can someone help me translate my code and give me a concrete example please?

---

<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:** [March 14, 2022, 4:38pm UTC](https://discuss.elastic.co/t/8-1-0-java-client-searchrequest-example/299640/2 "2022-03-14T16:38:17Z")

</div>

You probably need to write:

```auto
for (Hit<CVDocument> hit : response.hits().hits) {
  
}

```

---

<div class="post-metadata">

**Author:** ![Valh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/valh/32/103015_2.png) [@Valh](https://discuss.elastic.co/u/Valh)\
**Post date:** [March 14, 2022, 6:08pm UTC](https://discuss.elastic.co/t/8-1-0-java-client-searchrequest-example/299640/3 "2022-03-14T18:08:40Z")

</div>

Excellent! Looks like it works! Thank you @dadoonet

---

<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:** [April 11, 2022, 11:05pm UTC](https://discuss.elastic.co/t/8-1-0-java-client-searchrequest-example/299640/5 "2022-04-11T23:05:58Z")

</div>

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