0

I have following code for searching in ES:

    Header[] headers = {new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"), new BasicHeader(HttpHeaders.CONNECTION, "keep-alive")};
    RestClientBuilder restClientBuilder =
            RestClient.builder( new HttpHost("my-es-url",
                    443, "https"));
    restClientBuilder.setDefaultHeaders(headers);

    RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.rangeQuery("createdAt").from(startDate).to(endDate));
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(sourceBuilder);

    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    System.out.println(searchResponse.getHits().getHits().length);

This works fine. But I see that I haven't provided the index name here. I want to restrict my search to a given index only.

How to configure it?

1 Answer 1

1

You can do that when creating the SearchRequest:

SearchRequest searchRequest = new SearchRequest("my-index");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.