使用spring data elasticsearch从索引中获取所有文档
我正在尝试使用Spring Boot连接到外部ElasticSearch服务器使用spring data elasticsearch从索引中获取所有文档,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我正在尝试使用Spring Boot连接到外部ElasticSearch服务器 <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Sep 11
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Sep 11 12:39:15 IST 2017</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"])</div></body></html>
如果我从命令行执行curl,就会得到预期的结果
curl "http://ipAddr:9200/indexName/TYPE/_search?pretty=true"
但当我试图通过Spring Boot访问它时,会出现这个错误
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Sep 11 12:39:15 IST 2017</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"])</div></body></html>
存储库:
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
我应该能够查询索引中的所有文档。稍后,我将尝试使用过滤器等
感谢您的帮助。谢谢:)看起来Jackson在处理您的POJO时遇到了问题(可能与此问题有关:)-问题部分正在存储库中从
Iterable
集合转换到列表
更新
对于存储库,SpringDataElasticSearch的行为与您预期的稍有不同。以你为例:
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
正如我之前所写的,Jackson在序列化POJO对象时无法处理这个问题
解决方案1
让存储库返回Iterable
集合(默认情况下)
如果您仍然希望对列表进行操作-从服务中的页面获取内容:
public List<POJO> findAll() {
return testDtoRepository.findAll().getContent();
}
公共列表findAll(){
返回testDtoRepository.findAll().getContent();
}
我将列表更改为Iterable,但仍然得到相同的错误。您是否也将POJOServiceImpl
中的findAll()
方法更改为Lists.newArrayList(pojoRepository.findAll())
(而不是强制转换为列表)?否。即使在那里,我也没有获得列表,而是返回Iterable,感谢您指出Iterable
是从findAll()
返回的,我尝试使用Page
进行分页,令人惊讶的是,我再也没有收到该错误了。太棒了!当我在调试代码的过程中发现错误的确切原因时,我解释了我的答案,以防别人有同样的问题。
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
List<POJO> findAll();
}
List<POJO> pojoObj = pojoService.findAll();
Could not write JSON: ... java.util.ArrayList[0]->org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl[\"facets\"])
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
}
import com.google.common.collect.Lists;
public List<POJO> findAll() {
return Lists.newArrayList(pojoRepository.findAll());
}
@Repository
public interface PojoRepository extends ElasticsearchRepository<POJO, Integer> {
Page<TestDto> findAll();
}
public List<POJO> findAll() {
return testDtoRepository.findAll().getContent();
}