or9ob
or9ob

Reputation: 2392

How to cast Jest SearchResult hits to domain object

I am using Jest to query Elasticsearch and so far it has been great. Jest's documentation says:

Result can be cast to List of domain object;

... and shows this example:

SearchResult result = client.execute(search);
List<SearchResult.Hit<Article, Void>> hits = searchResult.getHits(Article.class);
// or
List<Article> articles = result.getSourceAsObjectList(Article.class);

getSourceAsObjectList is deprecated, and I am using:

List<SearchResult.Hit<ImmutableConceptDocument, Void>> concepts = result.getHits(ImmutableConceptDocument.class);

... Where ImmutableConceptDocument is an immutables generated class - otherwise pretty straight forward POJO, with attributes named as I see under the source of my search results.

However, when I use the above line, I don't get the source properties mapped, I do get other details like score, type, index etc. mapped.

What am I missing? Does the domain class need to have specific Jest annotations or something like that?

I can't see any good examples in the unit tests too. This one maps to Object.class and that does not show me a mapping example.

Here is the immutable class:

@Value.Immutable
public abstract class EsConceptDocument {
    public abstract String term();
    public abstract Category type();
    public abstract List<String> synonyms();
}

... where Category is an enum type.

Upvotes: 2

Views: 1314

Answers (1)

or9ob
or9ob

Reputation: 2392

As Val pointed out in the comments, this was because immutables.io makes the generated class' constructor private (and exposes a builder).

I removed immutable from this class and wrote a constructor and getters and it worked.

Upvotes: 1

Related Questions