Get all documents from an index using spring-data-elasticsearch -


i trying connect external elasticsearch server spring boot.

if curl command line, expected results.

curl "http://ipaddr:9200/indexname/type/_search?pretty=true" 

but getting error when try access via spring boot.

<html><body><h1>whitelabel error page</h1><p>this application has no explicit mapping /error, seeing fallback.</p><div id='created'>mon sep 11 12:39:15 ist 2017</div><div>there unexpected error (type=internal server error, status=500).</div><div>could not write json: (was java.lang.nullpointerexception); nested exception com.fasterxml.jackson.databind.jsonmappingexception: (was java.lang.nullpointerexception) (through reference chain: java.util.arraylist[0]-&gt;org.springframework.data.elasticsearch.core.aggregation.impl.aggregatedpageimpl[&quot;facets&quot;])</div></body></html> 

not sure why nullpointerexception , aggregartion.impl

here spring application:

controller:

@restcontroller public class pojocontroller {      @autowired     pojoservice pojoservice;      @requestmapping(value = "/", method=requestmethod.get)     public @responsebody string index() {         return new string("welcome:)");     }      @requestmapping(value = "/all", method = requestmethod.get,             produces = { mediatype.application_json_value })     @responsebody list<pojo> findall() {         try {             list<pojo> pojoobj = pojoservice.findall();             return pojoobj;         } catch (exception exp) {             exp.printstacktrace();             return null;         }     } } 

repository:

@repository public interface pojorepository extends elasticsearchrepository<pojo, integer> {      list<pojo> findall();  } 

service:

@service public class pojoserviceimpl implements pojoservice{      private pojorepository pojorepository;      private elasticsearchtemplate elasticsearchtemplate;      @autowired     public void setpojorepository(pojorepository pojorepository) {         this.pojorepository = pojorepository;     }      public pojo findone(string id) {         return pojorepository.findone(id);     }      public list<pojo> findall() {         return (list<pojo>) pojorepository.findall();     }  } 

pojo class:

@document(indexname = "index", type = "type") public class pojo {       @id      private integer id;      private string name;       public pojo(){          // empty      }        public pojo(integerid, string name) {         super();         this.id = id;         this.name = name;     }      // getters , setters } 

i should able query documents in index. later on, try , use filters etc.

any appreciated. :)

it looks jackson has problem handling pojo (probably related issue: dataes-274) - problematic part casting in repository iterable collection list.

update

in case of repositories, spring-data-elasticsearch behaves bit different expect. taking example:

@repository public interface pojorepository extends elasticsearchrepository<pojo, integer> {     list<pojo> findall(); } 

and after calling in rest controller:

list<pojo> pojoobj = pojoservice.findall(); 

in debugger see this:

enter image description here

you expect pojoobj list contains objects of pojo class. , here comes surprise - pojoobj arraylist contains 1 object of aggregatedpageimpl type , content field right list contains pojo objects. reason why get:

could not write json: ... java.util.arraylist[0]->org.springframework.data.elasticsearch.core.aggregation.impl.aggregatedpageimpl[\"facets\"]) 

as wrote before, jackson cannot handle while serializing pojo objects.

solution 1

let repositories return iterable collection (by default).

@repository public interface pojorepository extends elasticsearchrepository<pojo, integer> {  } 

move conversion part service use utility method (here guava) in order have this:

import com.google.common.collect.lists;  public list<pojo> findall() {     return lists.newarraylist(pojorepository.findall()); } 

solution 2

use page in repository (here simplified version without parameters):

@repository public interface pojorepository extends elasticsearchrepository<pojo, integer> {     page<testdto> findall(); } 

if still want operate on list - content page in service:

public list<pojo> findall() {     return testdtorepository.findall().getcontent(); } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -