我从elasticsearch获取记录并尝试以列表形式返回。但是从sourceAsMap
请在下面找到我的代码。
SearchHit[] searchHits = searchResponse.getHits().getHits();
List<Product> productList=new ArrayList<Product>();
for (SearchHit hit : searchHits) {
// get each hit as a Map
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
product=new Product();
product.setName(sourceAsMap.get("name").toString());
productList.add(product.setName(sourceAsMap.get("name").toString())); // throwing error in this line
}
return productList;
}
请找我的POJO课程:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Product {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:1)
我相信您收到1/0
。您的The method add(void) is undefined for the type Product
方法返回类型为Product#setName
,因此您实际上是在尝试将void
添加到void
{ {1}}。您应该List
而不是Product
您的代码应如下所示:
productList.add(product)