Search code examples
javaelasticsearchelasticsearch-jest

Jest ElasticSearch Search API Hits mapping


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


Solution

  • 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?