# Basic Java Example Not Working for me

**URL:** <https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569>\
**Category:** Elasticsearch\
**Created:** [July 28, 2019, 2:30pm UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569 "2019-07-28T14:30:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticHT](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@elasticHT](https://discuss.elastic.co/u/elasticHT)\
**Post date:** [July 28, 2019, 2:30pm UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569/1 "2019-07-28T14:30:58Z")

</div>

Using the basic Java example, here: [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html)

And having the following Maven dependencies installed:

```
 <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.2.0</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.2.0</version>
    </dependency>

```

The code from the URL above:

```
SearchResponse response = client. **prepareSearch** ("index1", "index2")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.get();

```

Does not compile on the "perpareSearch" as you see bolded above and there does not seem to be another maven dependency the IDE can find (or me either...).

I am creating the client as instructed to in 7.2, like this:

```
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("localhost", 9200, "http"),
                    new HttpHost("localhost", 9201, "http")));

```

If I use one of the older methods online, then the Client object itself has the compile problem.

I have these imports:

```
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

```

Thus, I've yet to find a totally working simple example.

Thanks for any suggestions what's off here.

---

<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:** [July 28, 2019, 3:51pm UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569/2 "2019-07-28T15:51:09Z")

</div>

For the rest client read [this documentation](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html).  
The example you read is for the deprecated TransportClient.

---

<div class="post-metadata">

**Author:** ![elasticHT](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@elasticHT](https://discuss.elastic.co/u/elasticHT)\
**Post date:** [July 28, 2019, 4:19pm UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569/3 "2019-07-28T16:19:06Z")

</div>

Yep, that's exactly the same area I got the code.

```
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.2.0</version>
</dependency>

```

Is what I'm using and what's posted there.

Do you see/find a _complete_ (small even) Java example? I see lots code snippets, but nothing with a complete example with output.

Thanks very much.

---

<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:** [July 28, 2019, 6:45pm UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569/4 "2019-07-28T18:45:08Z")

</div>

The link you pasted is coming from TransportClient documentation AFAIK.

Anyway here are some samples:

> **[dadoonet/elasticsearch-java-client-demo](https://github.com/dadoonet/elasticsearch-java-client-demo)**
>
> Elasticsearch Client Java Sample project. Contribute to dadoonet/elasticsearch-java-client-demo development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![elasticHT](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@elasticHT](https://discuss.elastic.co/u/elasticHT)\
**Post date:** [July 29, 2019, 10:13am UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569/5 "2019-07-29T10:13:01Z")

</div>

Thanks very much. 🙂 This is great info.

---

<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:** [August 26, 2019, 10:13am UTC](https://discuss.elastic.co/t/basic-java-example-not-working-for-me/192569/6 "2019-08-26T10:13:05Z")

</div>

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