1. Introduction of ElasticSearch
Elasticsearch is a distributed search and analysis engine at the heart of the Elastic Stack. Logstash and Beats help collect, aggregate, and enrich your data and store it in Elasticsearch. With Kibana, you can interactively explore, visualize and share insights into data, and manage and monitor stacks.
Maven introduces dependencies
The pom.xml configuration is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<groupId>com.james</groupId>
<artifactId>elasticsearch_demo</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>elasticsearch_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>Also 5.4.7</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
3.ElasticSearch connection configuration
(1) application. The properties configuration
elasticsearch.address=localhost:9200
elasticsearch.username=elastic
elasticsearch.password=elastic
Copy the code
(2) Java connection configuration class
@Configuration
public class ElasticsearchConfig {
@Value("${elasticsearch.address}")
private String address;
@Value("${elasticsearch.username}")
private String userName;
@Value("${elasticsearch.password}")
private String password;
@Bean(destroyMethod = "close")
public RestHighLevelClient restClient(a) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
// Split the address
List<HttpHost> hostLists = new ArrayList<>();
String[] hostList = address.split(",");
for (String addr : hostList) {
String host = addr.split(":") [0];
String port = addr.split(":") [1];
hostLists.add(new HttpHost(host, Integer.parseInt(port), "http"));
}
// Convert to HttpHost array
HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{});
RestClientBuilder builder = RestClient.builder(httpHost).
setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setMaxConnTotal(100)
.setMaxConnPerRoute(100)
.setDefaultCredentialsProvider(credentialsProvider)).
setRequestConfigCallback(requestConfigBuilder ->
requestConfigBuilder.setConnectTimeout(10000)
.setSocketTimeout(60000));
RestHighLevelClient client = new RestHighLevelClient(builder);
returnclient; }}Copy the code
4. Java code examples
@Service
@Slf4j
public class ProfileService {
private RestHighLevelClient restClient;
private ObjectMapper objectMapper;
@Autowired
public ProfileService(RestHighLevelClient restClient, ObjectMapper objectMapper) {
this.restClient = restClient;
this.objectMapper = objectMapper;
}
public String createProfileDocument(ProfileDocument document) throws Exception {
UUID uuid = UUID.randomUUID();
document.setId(uuid.toString());
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.getId())
.source(convertProfileDocumentToMap(document));
IndexResponse indexResponse = restClient.index(indexRequest, RequestOptions.DEFAULT);
return indexResponse.getResult().name();
}
public List<ProfileDocument> findAll(a) throws Exception {
SearchRequest searchRequest = buildSearchRequest(INDEX, TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse =
restClient.search(searchRequest, RequestOptions.DEFAULT);
return getSearchResult(searchResponse);
}
private SearchRequest buildSearchRequest(String index, String type) {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(index);
searchRequest.types(type);
return searchRequest;
}
private List<ProfileDocument> getSearchResult(SearchResponse response) {
SearchHit[] searchHit = response.getHits().getHits();
List<ProfileDocument> profileDocuments = new ArrayList<>();
for (SearchHit hit : searchHit) {
profileDocuments
.add(objectMapper
.convertValue(hit
.getSourceAsMap(), ProfileDocument.class));
}
return profileDocuments;
}
private Map<String, Object> convertProfileDocumentToMap(ProfileDocument profileDocument) {
return objectMapper.convertValue(profileDocument, Map.class);
}
private ProfileDocument convertMapToProfileDocument(Map<String, Object> map) {
returnobjectMapper.convertValue(map, ProfileDocument.class); }}Copy the code