I have a problem with spring-data-elasticsearch installing. I tried many ways to fix this but they didnt work. I'm studying course from udemy its 2016-2017 year and i'm using new version of dependencies of spring-data-elasticsearch, spring-data-jpa. Please help)
ElasticSearchConfig.java
@Configuration
@EnableElasticsearchRepositories("info.***.resume")
@ComponentScan(basePackages = {"info.***.resume.search"})
public class ElasticSearchConfig {
@Value("${elasticsearch.home}")//C:/Users/*username*/IdeaProjects/resume/elastic-search-data
private String elasticSearchHome;
@Bean
public Client client() throws UnknownHostException {
Settings settings = Settings.builder()
.put("client.transport.sniff", true)
.put("path.home", elasticSearchHome)
.put("cluster.name", "elasticsearch").build();
// return new PreBuiltTransportClient(settings)
// .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(client());
}
}
ProfileSearchRepository.java
public interface ProfileSearchRepository extends ElasticsearchRepository<Profile, Long> {
Page<Profile> findByObjectiveLikeOrSummaryLike(String objective, String summary, Pageable pageable);
}
ElasticSearchIndexingService.java
@Service
@Transactional
public class ElasticSearchIndexingService {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchIndexingService.class);
@Value("${index.all.during.startup}")
private boolean indexAllDuringStartup;
private final ProfileSearchRepository profileSearchRepository;
private final ElasticsearchOperations elasticsearchOperations;
private final FindProfileService findProfileService;
@Autowired
public ElasticSearchIndexingService(ProfileSearchRepository profileSearchRepository, ElasticsearchOperations elasticsearchOperations, FindProfileService findProfileService) {
this.profileSearchRepository = profileSearchRepository;
this.elasticsearchOperations = elasticsearchOperations;
this.findProfileService = findProfileService;
}
@PostConstruct
private void postConstruct() {
if (indexAllDuringStartup) {
LOGGER.info("Detected index ALL command");
LOGGER.info("Clear old index");
Line38 elasticsearchOperations.deleteIndex(Profile.class);
LOGGER.info("Start index of profiles");
for (Profile p : findProfileService.findAllForIndexing()) {
profileSearchRepository.save(p);
LOGGER.info("Successful indexing of profile:{}", p.getUid());
}
LOGGER.info("Finish index of profiles");
} else {
LOGGER.info("IndexAllDuringStartup is disabled");
}
}
}
Profile.java
@Entity
@Table(name = "profile")
@Document(indexName = "profile")
public class Profile {
@Id
@Column(unique = true, nullable = false)
private Long id;
@Column(nullable = false)
private String uid;
@Column(name = "first_name", nullable = false, length = 50)
private String firstName;
@Column(name = "last_name", nullable = false, length = 50)
private String lastName;
@NotNull
@Size(min = 8)
@JsonIgnore
private String password;
@Column(name = "objective")
private String objective;
@Column
private String summary;
@Column(name = "birth_day")
private Date birthDay;
@Column
private String city;
@Column
private String country;
@Column
@JsonIgnore
private String email;
@Column
@JsonIgnore
private String phone;
@Column
private String info;
@Column(name = "large_photo")
@JsonIgnore
private String largePhoto;
@Column(name = "small_photo")
private String smallPhoto;
@Column(nullable = false)
@JsonIgnore
private boolean completed;
@JsonIgnore
@Column(insertable = false)
private Timestamp created;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<Certificate> certificates;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@OrderBy("finishYear DESC, beginYear DESC, id DESC")
@JsonIgnore
private List<Education> educations;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@OrderBy("name ASC")
@JsonIgnore
private List<Hobby> hobbies;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<Language> languages;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@OrderBy("finishDate DESC ")
private List<Practice> practices;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@OrderBy("id ASC ")
private List<Skill> skills;
@OneToMany(mappedBy = "profile", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@OrderBy("finishDate DESC ")
private List<Course> courses;
@Embedded
@JsonIgnore
private Contacts contacts;
LOG
ERROR ContextLoader Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticSearchIndexingService': Invocation of init method failed; nested exception is NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{OqtopIzTQmOjjLBzr2G_JA}{127.0.0.1}{127.0.0.1:9300}]]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4688)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5151)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1728)
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:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
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:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at java.security.AccessController.doPrivileged(Native Method)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
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:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{OqtopIzTQmOjjLBzr2G_JA}{127.0.0.1}{127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:352)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:248)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:388)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:403)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:391)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1262)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.exists(AbstractClient.java:1286)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.indexExists(ElasticsearchTemplate.java:715)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.deleteIndex(ElasticsearchTemplate.java:732)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.deleteIndex(ElasticsearchTemplate.java:726)
at info.windigital.resume.search.ElasticSearchIndexingService.postConstruct(ElasticSearchIndexingService.java:38)
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:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
... 61 more
27-Apr-2020 13:52:09.140 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
27-Apr-2020 13:52:09.141 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [/resume] startup failed due to previous errors
27-Apr-2020 13:52:09.206 WARNING [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [resume] registered the JDBC driver [org.postgresql.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
[2020-04-27 01:52:09,227] Artifact resume:war: Error during artifact deployment. See server log for details.
You are trying to create a deprecated transport client to interact with Elasticsearch, while Elasticsearch officially released high-level java rest-client which is a preferred method for clients to connect with Elasticsearch.
Transport client is used by nodes in the cluster for communication which will be totally removed in 8.X as mentioned in the same doc:
Deprecated in 7.0.0. The TransportClient is deprecated in favour of the Java High Level REST Client and will be removed in Elasticsearch 8.0. The migration guide describes all the steps needed to migrate.
You should create a preferred and official high-level java client as shown below:
import org.springframework.beans.factory.annotation.Autowired;@Configuration
static class Config {
@Bean
RestHighLevelClient client() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200", "localhost:9201")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
Once created you can simply autowired this client
and use it for all the operations of Elasticsearch.