3

I have made an application using Springboot and running Elasticsearch in the background, everything works fine and when I am saving data by passing some json data it returns the right result(the number of inserted data) but when I am trying to fetch the datas it is returning empty array symbols.

Not getting what's happening can anybody please help.

And I have attached the screenshots of my postman requests.

For Saving data For Saving data

For Fetching data For Fetching data

My api class:-

@SpringBootApplication
@RestController
public class SpringBootElasticserachExampleApplication {

@Autowired
private HeroRepository repository;

@PostMapping("/saveHero")
public int saveCustomer(@RequestBody List<Hero> heros) {        

    repository.saveAll(heros);
    return heros.size();
}

@GetMapping("/findAll")
public Iterable<Hero> findAllHeros() {

    return repository.findAll();
}
}

Model class:

@Document(indexName="movie",type="hero",shards=2)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hero {

@Id
private String id;
private String firstname;
private String lastname;
private int age;

}

Repository:

public interface HeroRepository extends ElasticsearchRepository<Hero, String>{

List<Hero> findByFirstname(String firstName);

}
5
  • Please include the code and screenshots in the question. stackoverflow.com/help/mcve Commented Jan 4, 2019 at 13:06
  • @Nikhil Added the codes Commented Jan 4, 2019 at 13:45
  • 1
    heros.size() would return the size whether the data is stored or not Commented Jan 4, 2019 at 14:08
  • Iterable<Hero> savedHeroes = repository.saveAll(heros); should get you what has been saved and the size. Commented Jan 4, 2019 at 14:19
  • @eosimosu Yes I know that. But I have started with a new cluster and index too when I wants to save 4 json objects, it returns me the count as 4. And when I call the findAll() it shows 4 empty array symbols.Then after if I try to save one json object and by calling findAll() it shows 5 empty array symbols. Commented Jan 5, 2019 at 14:01

3 Answers 3

2

It is a Bean creation issue, Lombok Did not properly work here.

I solved this by adding Manually Getter Setter.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Are you sure your data is saved in the DB? Did you query the DB directly and can see the data there? Like others wrote before me, your current code simply returns the number of input items. not necessarily the real amount of items that were saved.

  2. Seems like your findAllHeros is calling repository.findAll(); (that you inherit from parent repositoy). But in your repository you have created a different method : List<Hero> findByFirstname(String firstName); Is it just a typo?

Also, I would expect the Controller with findAllHeros to return a list and not Iterable

2 Comments

I think findByFirstname(String firstname) is fine. See: docs.spring.io/spring-data/elasticsearch/docs/current/reference/…
@rioio findByFirstname(String firstName ) this is a different method which I have created because the doesn't have this one inbuit. But the Elasticsearch Repo has the findAll() method in it. and I am not saving the data into DB just working with Elastic only.
0

I think your code is adding the JSON object to ElasticSearch, but data is not saved. So you need to save the data with return type as List, don't return size only.

@PostMapping("/saveCustomer")
public List<Customer> saveCustomer(@RequestBody List<Customer> customers) {
  List<Customer> savedCustomer= repository.save(customers);
  //return customers.size();
  return savedCustomer;
}

In repository write

List<Customer> save(List<Customer> customers);
Iterable<Customer> findAll();

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.