3

I'm just getting started with elasticsearch with spring which is both technologies which are completely new to me. I have uploaded data to an elasticseach index with logstash and I can search it successfully using kebana. However when I try to return from an index to a webpage using spring it only returns empty json-objects, but the right amount of empty objects. Did I upload the data incorrectly or is something wrong with my code? I don't understand why this is happening and would appreciate any help I can get. You can find some code below.

Code for type:

@Document(indexName="usmgbg_index", type="usmgbg_type")
public class Usmgbg {

    @Id
    private String ID;
    private String Source, Name, Profession, Country, FileName, LastModified, OwnerID;
}

Repository:

@Repository 
public interface UsmgbgRepository extends ElasticsearchRepository<Usmgbg, String>{}

Controller:

@RestController
public class UsmgbgController {
    @Autowired
    private UsmgbgRepository repository;
    
    @GetMapping("usmgbg/findall")
    public List<Usmgbg> findAllCustomers() {

        List<Usmgbg> items = new ArrayList<>();
        repository.findAll().forEach(items::add);
      
        return items;
    } 
}

The output I'm getting from findAllCustomers looks like:

[{},{},{},{},....]
3
  • maybe your _source is false? Commented Nov 18, 2019 at 17:51
  • Is that an option for the config file, beacuse I'm not using any field called _source, is the default option of _source false? Commented Nov 18, 2019 at 19:41
  • stackoverflow.com/questions/57308752/… Commented Jul 13, 2021 at 9:49

3 Answers 3

3

I realize this is an old question but I had the same issue (maybe for a different reason) and I solved it by adding getters and setters in the model.

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

Comments

1

Iterable is returned from findAll().

If you want to get list you should get content first.

Change

@Repository 
public interface UsmgbgRepository extends 
ElasticsearchRepository<Usmgbg, String>{
     Page<Usmgbg> findAll();
}

And then

repository.findAll().getContent().forEach(items::add);

Or fix your code to iterate over the results.

Another solution is to use search method in ElasticsearchRepository using QueryBuilders API.

  Iterable<Usmgbg>=
  repository.search(QueryBuilders.matchAllQuery);

4 Comments

I tried this and it gave the same result. I guess i also can mention if i print the returning list it looks like this: [com.example.demo.usmgbg.Usmgbg@5025389, com.example.demo.usmgbg.Usmgbg@13dc7f1.....] Thank you!
I have tried both of your sugenstions but they give the same result? Do you have any other idea?
No, I don't get any other result when changing the type of client, I will have to look more at it tomorrow! But thanks for the help!
You can always switch to Java High Level Rest Client medium.com/@sourav.pati09/…
0

Adding my experience

spring-data requires getters to follow the POJO naming, i.e: getSomething()

so it did not work (spring-data did not send any fields to ElasticSearch when saving the @Document, resulting in an empty _source in ES) when having Lombok @Accessors(fluent = true), as it removes the get prefix on getters ...

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.