我试图通过Spring Data Elasticsearch项目访问Elasticsearch,通过其提供的Repository接口更精确。该应用程序非常简单,到目前为止仅作为POC使用。
我有一家公司类型的Elasticsearch指数公司。相应的实体类:
package spring.elasticsearch.example;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName="companies", type="company")
public class Company {
@Id
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;
}
}
我扩展了ElasticsearchCrudRepository
package spring.elasticsearch.example;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
public interface CompanyRepository extends ElasticsearchCrudRepository<Company, String> {
}
并在Spring的存储库实现中将它自动装入Spring(到目前为止,只有一种方法可以检查现有公司):
package spring.elasticsearch.example;
import org.springframework.beans.factory.annotation.Autowired;
public class CompanyRepositoryImpl {
@Autowired
private CompanyRepository repository;
public boolean existsCompany(String id) {
return repository.exists(id);
}
}
Elasticsearch和bean xml配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<elasticsearch:repositories base-package="spring.elasticsearch.example" />
<elasticsearch:transport-client id="client"
cluster-nodes="deves01:9300" cluster-name="elasticsearch" />
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
<bean id="companyRepositoryImpl" class="spring.elasticsearch.example.CompanyRepositoryImpl"></bean>
</beans>
最后我的应用程序运行它:
package spring.elasticsearch.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/config.xml");
CompanyRepositoryImpl companyRepositoryImpl = (CompanyRepositoryImpl) context.getBean("companyRepositoryImpl");
boolean testCompanyExists = companyRepositoryImpl.existsCompany("123");
if(testCompanyExists) {
System.out.println("company exists.");
} else {
System.out.println("company doesn't exist.");
}
}
}
Elasticsearch节点已启动并正在运行。一切正常,除非我开始使用CompanyRepository的方法 - &gt; ElasticsearchCrudRepository。执行此应用程序会抛出java.lang.IndexOutOfBoundsException和org.elasticsearch.common.util.concurrent.UncategorizedExecutionException。我不清楚它们在这种情况下的含义 - 显然它是一个Elasticsearch问题,而不是Spring Data Repository引起的问题。这是完整的堆栈跟踪:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.elasticsearch.common.util.concurrent.UncategorizedExecutionException: Failed execution
at org.elasticsearch.action.support.AdapterActionFuture.rethrowExecutionException(AdapterActionFuture.java:90)
at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:50)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForObject(ElasticsearchTemplate.java:225)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForObject(ElasticsearchTemplate.java:217)
at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.findOne(AbstractElasticsearchRepository.java:91)
at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.exists(AbstractElasticsearchRepository.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy16.exists(Unknown Source)
at spring.elasticsearch.example.CompanyRepositoryImpl.existsCompany(CompanyRepositoryImpl.java:11)
at spring.elasticsearch.example.Application.main(Application.java:12)
Caused by: java.lang.IndexOutOfBoundsException: Readable byte limit exceeded: 66
at org.elasticsearch.common.netty.buffer.AbstractChannelBuffer.readByte(AbstractChannelBuffer.java:236)
at org.elasticsearch.transport.netty.ChannelBufferStreamInput.readByte(ChannelBufferStreamInput.java:132)
at org.elasticsearch.common.io.stream.AdapterStreamInput.readByte(AdapterStreamInput.java:54)
at org.elasticsearch.common.io.stream.StreamInput.readBoolean(StreamInput.java:317)
at org.elasticsearch.search.fetch.source.FetchSourceContext.optionalReadFromStream(FetchSourceContext.java:102)
at org.elasticsearch.action.get.GetRequest.readFrom(GetRequest.java:268)
at org.elasticsearch.transport.netty.MessageChannelHandler.handleRequest(MessageChannelHandler.java:209)
at org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:109)
at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:296)
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462)
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443)
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)
at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.elasticsearch.common.netty.OpenChannelsHandler.handleUpstream(OpenChannelsHandler.java:74)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)
at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
有人看到错误或我做错了什么吗?谢谢你的帮助!