0

Am fetching records from elasticsearch and am trying to return as a list. But am getting error while adding to the list from sourceAsMap

Please find my code below.

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;
}   

Please find my POJO class:

@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;
    }

}
1
  • Do you have a stack trace? Commented May 24, 2018 at 18:13

1 Answer 1

1

I believe you are getting The method add(void) is undefined for the type Product.Your Product#setName methods return type is void, so you are actually trying to add void to List of Product. You should do productList.add(product) instead of productList.add(product.setName(sourceAsMap.get("name").toString()));

Your code should looks like below:

    Map<String, Object> sourceAsMap = hit.getSourceAsMap();
    product=new Product();
    product.setName(sourceAsMap.get("name").toString());
    productList.add(product);
Sign up to request clarification or add additional context in comments.

2 Comments

I have added my POJO class too. to which method i have to change the return type as void to List..?
@Karthikeyan You don't need to change anything only use productList.add(product) instead of productList.add(product.setName(sourceAsMap.get("name").toString())); wil solve your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.