Hello,
can you tell me where i can find an example with spring data and elasticsearch.
i use elasticsearch 5.1.2 and Spring 4.
i found an example, but when i tried it, exceptions are produced :
Main Classe for tests :
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
public class ESMain {
@Autowired
private UserRepository repository;
@Autowired
private ElasticsearchTemplate template;
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
ESMain app = (ESMain)ctx.getBean("main");
Settings s= app.template.getClient().settings();
Iterable<User> users = app.listAllUsers();
System.out.print("Done");
}
public Iterable<User> listAllUsers(){
return repository.findAll();
}
spring.xml
<context:annotation-config/>
<bean name="main" class="com.es.ESMain"/>
<elasticsearch:repositories base-package="com.es"/>
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300"/>
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>
UserRepository:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface UserRepository extends ElasticsearchRepository<User, String>{
}
User:
package com.es;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "users", type = "user")
public class User {
@Id
private String username;
private String password;
private boolean enabled;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
pom : (dependencies)
junit
junit
3.8.1
test
org.aspectj
aspectjrt
${aspectj.version}
runtime
org.aspectj
aspectjtools
${aspectj.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework.data
spring-data-elasticsearch
2.0.6.RELEASE
org.elasticsearch
elasticsearch
5.1.2
org.elasticsearch.client
transport
5.1.2
org.apache.logging.log4j
log4j-core
2.7
Exception is :
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
.....
at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy17.findAll(Unknown Source)
at com.elasticsearch.esexample.ESMain.main(ESMain.java:26)
thanks.