SpringBoot-2.2.6操作Elasticsearch-6.2.4
## Kibana版本 kibana-6.2.4-darwin-x86_64
Kibana安装Elasticsearch安装请自行解决.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
以下方法官方已经不推荐了.
#spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#spring.data.elasticsearch.cluster-name=elasticsearch
所以我们通过java代码配置, 需要注意端口是Es的Tcp端口9300. 而不是Http协议的9200
package com.example.config;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.EntityMapper;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author wajn
* @date 2020/04/25 19:50
*/
@Configuration
public class EsConf extends ElasticsearchConfigurationSupport {
@Bean
public Client elasticsearchClient() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
@Bean(name = "elasticsearchTemplate")
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(elasticsearchClient(), entityMapper());
}
// use the ElasticsearchEntityMapper
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
new DefaultConversionService());
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
}
package com.example.esentity;
import lombok.Data;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;
/**
* @author wajn
* @date 2020/04/25 19:50
*/
@ToString
@Data
//indexName索引库名,个人建议以项目名称命名
//type类型,个人建议以实体类名称命名
@Document(indexName = "log",type = "test")
public class LogEntity {
//代表唯一标识
@Id
private String id;
private String exceptionInformation;
private String logType;
private String operating;
private String userName;
private Date responseTime;
private String ip;
private Date time;
}
package com.example.repository;
import com.example.esentity.LogEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
* @author wajn
* @date 2020/04/25 19:50
*/
@Repository
public interface LogRepository extends ElasticsearchRepository<LogEntity,String> {
}
package com.example.repository;
import com.example.ApplicationTest;
import com.example.esentity.LogEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.Optional;
/**
* @author wajn
* @date 2020/04/25 19:50
*/
public class LogRepositoryTest extends ApplicationTest {
@Autowired
LogRepository logRepository;
@Test
public void find() {
Optional<LogEntity> y8fAsHEBZjLCY8DIlCsl = logRepository.findById("y8fAsHEBZjLCY8DIlCsl");
System.out.println(y8fAsHEBZjLCY8DIlCsl.get());
}
@Test
public void save() {
LogEntity logEntity = new LogEntity();
logEntity.setExceptionInformation("loading...");
logEntity.setLogType("1");
logEntity.setOperating("delete");
logEntity.setUserName("zs");
logEntity.setResponseTime(new Date());
logEntity.setIp("192.168.1.1");
logEntity.setTime(new Date());
LogEntity index = logRepository.save(logEntity);
System.out.println(index);
}
}
package com.example;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author wajn
* @date 2020/04/25 19:50
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsDemoApplication.class)
public class ApplicationTest {
@Before
public void init() {
System.out.println("init ok");
}
}
控制台:
LogEntity(id=zMf-sHEBZjLCY8DIwytV, exceptionInformation=loading…, logType=1, operating=delete, userName=zs, responseTime=Sat Apr 25 19:00:18 CST 2020, ip=192.168.1.1, time=Sat Apr 25 19:00:18 CST 2020)
es数据结果: