How do I get a list of search results?

I honestly tried to read the official tutorials. Unfortunately, I got completely bogged down: I find them hard to understand and they don't appear to give me the very specific information I need which is: how do I get a List of search results in Java? At this point I have these create() and delete() methods that look like this

public class ElasticQuestionRepositoryImpl implements ElasticQuestionRepository {
    private final ElasticsearchClient client;

    public CreateResponse create(ElasticQuestion question) {
        try {
            CreateRequest<ElasticQuestion> request = new CreateRequest.Builder<ElasticQuestion>()
            return client.create(request);
        } catch (IOException e) {
            throw new ElasticsearchException("Could not save an elastic entity: " + e.getMessage());

    public DeleteResponse delete(ElasticQuestion question) {
        try {
            String questionId = question.getId().toString();
            DeleteRequest request = new DeleteRequest.Builder()
            return client.delete(request);
        } catch (IOException e) {
            throw new ElasticsearchException("Could not delete an elastic entity: " + e.getMessage());

Now, I somehow need to write a getPage() method. ChatGPT provides code that is obviosuly wrong, but I took it, altered a bit, and now I have this. It's still messy and also incomplete

    public Page<QuestionResponseDto> getPage(PaginationParameters params) {
        int limit = PaginationParametersProcessor.extractMaxResults(params);
        int offset = PaginationParametersProcessor.extractFirstResultIndex(params);
        String orderByCol = PaginationParametersProcessor.extractOrderByColumn(params);
        SortOrder sortOrder = PaginationParametersProcessor.isDescending(params) ?
                SortOrder.DESC : SortOrder.ASC;
        String text = PaginationParametersProcessor.extractText(params).orElse(null); // text may be not provided

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .sort(orderByCol, sortOrder);

        if(text != null && !text.isBlank()) {
                    QueryBuilders.multiMatchQuery(text, "title", "description")

        SearchRequest request = new SearchRequest.Builder() // StringRequest is not generic despite what the chat bot says
                .source(searchSourceBuilder) // it is an invalid argument

// here should go the ElasticQuestion → QuestionResponseDto mapping (I can write a mapper) and returning a List of all matches
@Document(indexName = "questions")
public class ElasticQuestion implements Persistable<Long> {
    private Long id;
    private LocalDateTime createdDate;
    private LocalDateTime modifiedDate;
    private String title;
    private String description;
    private Account owner;

    public boolean isNew() {
        return id == null ||
                (createdDate == null && modifiedDate == null);

Can you help me please? Or at least give me a reference that is comprehensive and helpful? I would be happy to receive it

Would this help you to start with?

Note that this has been written by a human :wink:

In case it's necessary (can't edit the post any more, unfortunately)

public class Page<T> {

    private List<T> dtos;
    private Long count; // btw, I need to get a count of all persisted entities too
public class ElasticConfig {
    public RestClient restClient() {
        return RestClient.builder(
                new HttpHost("localhost", 9200)).build();

    public ElasticsearchTransport elasticsearchTransport() {
        return new RestClientTransport(
                restClient(), new JacksonJsonpMapper());

    public ElasticsearchClient elasticsearchClient() {
        return new ElasticsearchClient(elasticsearchTransport());
# docker_compose.yml, it's for a database too
version: '3.8'
    image: postgres:15
    container_name: stack_overflow_postgres
    restart: always
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres123
      - POSTGRES_DB=stack_overflow_postgres
      - '5411:5432'
# the code below is generated by ChatGPT so it may be a mess too
    container_name: stack_overflow_elasticsearch
    restart: always
      - discovery.type=single-node
      - ELASTIC_PASSWORD=your_password
      - '9200:9200'
      - '9300:9300'

      build: .
        - stack_overflow_postgres
        - stack_overflow_elasticsearch
        - SPRING_DATASOURCE_URL=jdbc:postgresql://stack_overflow_postgres:5432/stack_overflow_postgres
        - SPRING_DATASOURCE_PASSWORD=postgres123
        - SPRING_ELASTICSEARCH_REST_URIS=http://stack_overflow_elasticsearch:9200
        - '8080:8080'

Thank you. But can you describe the steps I should take? Is my code is salvageable? For example, is the logic of first creating a SearchSourceBuilder, then passing it into a SearchRequest, then passing it into a SearchResponse valid? If so, how do I create the SearchRequest instance and, finally, retrieve a result List from SearchResponse?

I guess you can but I prefer to use the functional style with lambdas. :wink:

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