I have created an index (house) with a type "apartments" that contains 20 documents. I uploaded the Json as a binary file into elasticsearch using postman. I have a Spring Boot project that has the following classes:
EsConfig.java - I have configured the clustername which is the default name in the application.properties file.
@Configuration @EnableElasticsearchRepositories(basePackages = "com.search.repository") public class EsConfig { @Value("${elasticsearch.clustername}") private String EsClusterName; @Bean public Client esClient() throws UnknownHostException { Settings esSettings = Settings.builder() .put("cluster.name", EsClusterName) .put("client.transport.sniff", true) .put("client.transport.ignore_cluster_name", false) .build(); TransportClient client = new PreBuiltTransportClient(esSettings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); return client; } @Bean public ElasticsearchOperations elasticsearchTemplate() throws Exception{ return new ElasticsearchTemplate(esClient()); } }
Apartments.java - This is my data model. The documents have the below fields in elasticsearch.
@Document(indexName = "house", type = "apartments") @JsonIgnoreProperties(ignoreUnknown=true) public class Apartments { @Id private String id; @JsonProperty("Apartment_Name") private String apartmentName; @JsonProperty("Apartment_ID") private String apartmentId; @JsonProperty("Area_Name") private String areaName; //constructors along with getters and setters }
ApartmentSearchRepository.java - This is an interface that extends the ElasticsearchRepository interface to perform crud operations.
public interface ApartmentSearchRepository extends ElasticsearchRepository<Apartments, String> { List<Apartments> findByApartmentName(String apartmentName); }
EsApartmentService.java -
@Service public class EsApartmentService { @Autowired ApartmentSearchRepository apartmentSearchRepository; public List<Apartments> getApartmentByName(String apartmentName) { return apartmentSearchRepository.findByApartmentName(apartmentName); } }
ApartmentController.java - I have created an endpoint that should give back those 20 documents from elasticsearch. (Also, Apartment is a POJO in my project and Apartments is the data model.)
@Autowired EsApartmentService esApartmentService; @GetMapping(path = "/search",produces = "application/json") public Set<Apartment> searchApartmentByName( @RequestParam(value = "apartmentName", defaultValue = "") String apartmentName) throws IOException { List<Apartment> apartments= new ArrayList<>(); esApartmentService.getApartmentByName(apartmentName).forEach(apartment-> { apartments.add(new Apartment(apartment.getApartmentName(), apartment.getApartmentId(), apartment.getAreaName())); }); return apartments.stream() .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Apartment::getApartmentId)))); }
This code gives back a status of 200 but with an empty response. I tried debugging but it seems that it is unable to read those documents from elasticsearch. I went through a couple of solutions but most of them have set the document data from within the code itself.
I am unable to retrieve those documents by hitting the endpoint I specified in the controller. Can someone let me know what I could be missing out on? Thanks! :)
Edit: The screenshot below shows the query and response in Postman.