I tried to map elastic data to list. since getSourceAsObjectList() method is deprecated, I made code like this.
class activityObj {
private String name;
private String oid;
public String getName()
{
return name;
}
public void setName( String name)
{
this.name = name;
}
public String getOid()
{
return oid;
}
public void setOid( String oid)
{
this.oid = oid;
}
}
List< Hit<activityObj, Void>> hits = result.getHits(activityObj.class);
List< activityObj> list = new ArrayList<activityObj>();
for (SearchResult.Hit<activityObj, Void> hit: hits) {
activityObj objectSource = hit.source; // null point error
list.add(objectSource);
logger.info( "hits : " + objectSource.getName());
}
It doesn't work as I expect and looks messy ( it's using two list value to map result). Am I doing wrong?
Michael's answer helped me and is a nice way to do it. Here's what I came up with including all the parts I used to make the query and get the result.
In my case I wanted to get a list of user ids collected from the source of each document in the result.
class DocSource {
private Integer userId;
public Integer getUserId() {
return userId;
}
}
String query = "{\n"
+ " \"_source\": [\n"
+ " \"userId\"\n"
+ " ],\n"
+ " \"query\": {\n"
+ " <some query config here>"
+ " }\n"
+ " }\n"
+ "}";
Search search = new Search.Builder(query)
.addIndex("my-index")
.build();
SearchResult result = jestClient.execute(search);
List<SearchResult.Hit<DocSource, Void>> hits = result.getHits(DocSource.class);
List<Integer> listOfIds = hits.stream()
.map(h -> h.source.getUserId())
.collect(Collectors.toList());