Contents of this chapter
-
-
- ES and x-pack download and install
-
-
-
- Kibana and x-pack download and install
-
-
-
- Spring Boot integrates ES
-
-
-
- Spring Boot operates ES
-
Reading time: 5 minutes
Excerpt: Those who intend to start with extraordinary and write a masterpiece, often cannot insist on completing the first chapter
Original source: spring4all.com
1. Download and install ES and x-pack
A preliminary exploration of the ElasticSearch architecture of spring-data-elasticsearch, please see my other article in detail, "A preliminary exploration of the ElasticSearch architecture of spring-data-elasticsearch (1)" http://www.spring4all.com/article/330
Three elements of ES:
-
Document (Document) Document, in the object-oriented concept is an object. In ES, it's a big JSON object, the lowest or root object that specifies a unique ID. The location of a document is uniquely identified by _index, _type, and _id.
-
Index An index is used to distinguish document groups, that is, a collection of documents grouped into a group. An index for storing documents and making them searchable. For example, projects are stored in the index project, transactions are stored in the index sales, and so on.
-
Type The type used to distinguish documents in the index, that is, to logically partition data in the index. For example, the project data of the index project is distinguished according to the project type, ui project, illustration project, etc.
An analogy with the relational database MySQL:
- Document is similar to Record
- Type is similar to Table
- Index is similar to Database
The installation steps are as follows:
1. Download ES 5.5.3
Download address: https://www.elastic.co/downloads/past-releases/elasticsearch-5-5-3
2. Install the ES plugin x-pack
unpack - then install the plugin x-pack
tar -xzf elasticsearch-5.3.0.tar.gz
cd elasticsearch-5.3.0/
// 安装 X-Pack
bin/elasticsearch-plugin install x-pack
3. Configure ES and start
Set Xpack security validation to false:
vim config/elasticsearch.yml
and add the following configuration:
xpack.security.enabled: false
and start ES:
./bin/elasticsearch
或者后台启动
./bin/elasticsearch -d
2. Kibana and x-pack download and install
1. Download Kibana 5.5.3
Download address: https://www.elastic.co/downloads/past-releases/kibana-5-5-3
2. Install the Kibana plugin x-pack
unpack - then install the plugin x-pack
tar -zxvf kibana-5.5.3-darwin-x86_64.tar.gz
cd kibana-5.5.3-darwin-x86_64/
// 安装 X-Pack
bin/kibana-plugin install x-pack
3. Configure Kibana and start
Set Xpack security validation to false:
vim config/kibana.yml
and add the following configuration:
xpack.security.enabled: false
and start Kibana:
./bin/kibanah
或者后台启动
nohup ./bin/kibana &
Must pay attention: start ES first, then start Kibana.
If the background is started, the shutdown command is as follows:
ps aux | grep 'elastic'
kill -9 pid
After the startup is successful, open the webpage and visit 127.0.0.1:5601. The default account is elasti and the password is changeme.
3. Spring Boot integrates ES
1. pom.xml dependencies
code show as below:
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring4all</groupId>
<artifactId>bysocket</artifactId>
<version>1.0.0</version>
<description>bysocket :: AI For All</description>
<properties>
<lombok.version>1.16.18</lombok.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M7</version>
</parent>
<dependencies>
<!-- web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 日志 log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 容器 undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- 简化 lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- ES -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Spring Data
To understand what spring-data-elasticsearch is, first understand what Spring Data is. Spring Data provides a similar and consistent programming model for data access based on Spring and preserves the underlying data store.
Spring Data Elasticsearch
spring-data-elasticsearch is one of Spring Data's Community modules and is Spring Data's implementation of the Elasticsearch engine. Elasticsearch provides access in the form of a lightweight HTTP Restful interface by default. Relatively speaking, using HTTP Client calls is also simple. But spring-data-elasticsearch can be built on Spring applications faster, such as configuring ES node information and spring-boot-starter-data-elasticsearch dependencies in application.properties, and using them directly on Spring Boot applications.
The spring-boot-starter-data-elasticsearch version depends on here is 2.0, and the corresponding spring-data-elasticsearch version is 5.5.3.RELEASE. For ES, try to install the same version as 5.5.3.
2. application.properties configure the ES address
application.properties is configured as follows:
# ES
spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-name: elasticsearch
cluster-nodes: 120.132.29.37:9300
The default 9300 is the port for the Java client. 9200 is the interface that supports Restful HTTP. More configuration:
- spring.data.elasticsearch.cluster-name Elasticsearch cluster name. (default: elasticsearch)
- spring.data.elasticsearch.cluster-nodes A comma-separated list of cluster node addresses. If not specified, starts a client node.
- spring.data.elasticsearch.propertie is used to configure additional properties on the client side.
- spring.data.elasticsearch.repositories.enabled enables the Elasticsearch repository. (Default: true.)
3. ES data manipulation layer
The article entity class code is as follows:
/**
* 文章
*/
@Document(indexName = "elasticsearch", type = "article")
@Data
public class ArticleEntity implements Serializable {
// 作者信息
private String writer;
// 文章信息
@Id
private Long id;
private String title;
private String content;
// 归属信息
private Long admin;
}
- CamelCase is not supported for City property names.
- The indexName configuration must be all lowercase, otherwise an exception will occur. org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [provinceIndex], must be lowercase
The ES data operation layer implementation code is as follows:
@Repository
public interface ArticleRepository extends ElasticsearchRepository<ArticleEntity, Long> {
}
The interface only needs to inherit the ElasticsearchRepository interface class, and the specific methods of this interface are used:
<S extends T> S save(S var1);
<S extends T> Iterable<S> saveAll(Iterable<S> var1);
Optional<T> findById(ID var1);
boolean existsById(ID var1);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> var1);
long count();
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable<? extends T> var1);
void deleteAll();
<S extends T> S index(S var1);
Iterable<T> search(QueryBuilder var1);
Page<T> search(QueryBuilder var1, Pageable var2);
Page<T> search(SearchQuery var1);
Page<T> searchSimilar(T var1, String[] var2, Pageable var3);
void refresh();
Class<T> getEntityClass();
Add, delete, modify, search, and search are all there, and we don't need to implement it specifically, we just need to pass in the corresponding parameters.
4. Article search ES business logic implementation class
The interface for batch saving to ES is implemented. The code is as follows:
@Service
@Primary
@AllArgsConstructor
@Log4j2
public class ArticleServiceImpl implements ArticleService {
private final ArticleRepository articleRepository;
@Override
public boolean saveArticles(List<ArticleBean> articleBeanList) {
List articleEntityList = transferToArticleEntityList(articleBeanList);
articleRepository.saveAll(articleEntityList);
return true;
}
... 省略
}
4. Spring Boot operates ES
Add articles with Postman tool:
POST /api/articles HTTP/1.1
Host: 127.0.0.1:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: d6288a82-b98f-1c1e-6dad-15b0223102ab
[
{
"id":2,
"title":"文章题目",
"writer":"温岭",
"content":"温岭是个沿海城市",
"admin":1
},
{
"id":3,
"title":"文章题目文章题目文章题目",
"writer":"温1岭",
"content":"温2岭是个沿海城市",
"admin":1
}
]
Then set the indexName to "elasticsearch" in Kibana, and open the Discover tab, you can see the data, and you can search and query, as shown in the figure:
More ES articles:
- 《Spring Boot 整合 Elasticsearch》
- "Introduction to the ElasticSearch Architecture of Spring-data-elasticsearch (1)"
- "In-depth spring-data-elasticsearch series - overview and introduction (2)"
- "In-depth explanation of spring-data-elasticsearch - detailed explanation of basic cases (3)"
- "Explaining in simple terms spring-data-elasticsearch - detailed explanation of practical cases (4)"
Follow to get a series of tutorial articles!