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
@Repository
@RequiredArgsConstructor
public class ElasticQuestionRepositoryImpl implements ElasticQuestionRepository {
private final ElasticsearchClient client;
@Override
public CreateResponse create(ElasticQuestion question) {
try {
CreateRequest<ElasticQuestion> request = new CreateRequest.Builder<ElasticQuestion>()
.document(question)
.build();
return client.create(request);
} catch (IOException e) {
throw new ElasticsearchException("Could not save an elastic entity: " + e.getMessage());
}
}
@Override
public DeleteResponse delete(ElasticQuestion question) {
try {
String questionId = question.getId().toString();
DeleteRequest request = new DeleteRequest.Builder()
.id(questionId)
.build();
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
@Override
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()
.from(offset)
.size(limit)
.sort(orderByCol, sortOrder);
if(text != null && !text.isBlank()) {
searchSourceBuilder.query(
QueryBuilders.multiMatchQuery(text, "title", "description")
);
}
SearchRequest request = new SearchRequest.Builder() // StringRequest is not generic despite what the chat bot says
.index("questions")
.source(searchSourceBuilder) // it is an invalid argument
.build();
}
// here should go the ElasticQuestion → QuestionResponseDto mapping (I can write a mapper) and returning a List of all matches
@Document(indexName = "questions")
@NoArgsConstructor
@Getter
public class ElasticQuestion implements Persistable<Long> {
@Id
private Long id;
@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime modifiedDate;
private String title;
private String description;
private Account owner;
@Override
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