Basic Java Example Not Working for me

Using the basic Java example, here: 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.

For the rest client read this documentation.
The example you read is for the deprecated TransportClient.

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.

The link you pasted is coming from TransportClient documentation AFAIK.

Anyway here are some samples:

Thanks very much. :slight_smile: This is great info.

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