0

I'm extremely new to elastic search

I'm trying to understanding the below code :

List<Hit<Talk, Void>> hits = result.getHits(Talk.class);
        for (Hit<Talk, Void> hit: hits) {
            Talk talk = hit.source;
            log.info(talk.getTitle());
        }

This is directly taken from : https://www.elastic.co/blog/found-java-clients-for-elasticsearch

My question is , how does Java know what field to map to what variable.

Essentially , how does Java know , say to match the property "title" to the member variable "title" of the Talk class.

Cheers

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Hormigas
  • 1,429
  • 5
  • 24
  • 45

1 Answers1

0

In the page it mentions that the Talk class is a Bean. Beans implement Serializable. When result.getHits is called, notice how Talk.class object is passed into the method. That means you are basically deserializing the hits into Talk instances. With how the JEST Hit class is structured, that is how you access the Talk instances after they are deserialized from the Elastisearch response.

For more information on Beans, see here: What is a JavaBean exactly?

ryanlutgen
  • 2,951
  • 1
  • 21
  • 31
  • so is it necessary to use a class which is a bean ? Could I instead just make my own class that implements Serializable ? Thank you for the answer! Really appreciate your help! – Hormigas Jun 18 '17 at 23:31
  • 1
    A bean is just a set of criteria that a class needs to meet to be considered such. So if you meet those criteria with any class you write, it is by definition a bean. In other words, yes you can write your own class that implements Serializable, and if you meet the criteria in the question I linked, it should work. – ryanlutgen Jun 19 '17 at 16:38