安装流程: https://blog.csdn.net/qq_37598011/article/details/103140376
Elasticsearch与Solr的比较
- 当单纯的对已有数据进行搜索时,Solr更快。
- 当实时建立索引时, Solr会产生io阻塞,查询性能较差, Elasticsearch具有明显的优势。
- 随着数据量的增加,Solr的搜索效率会变得更低,而Elasticsearch却没有明显的变化。
- 综上所述,Solr的架构不适合实时搜索的应用。
- Solr 利用 Zookeeper 进行分布式管理,而 Elasticsearch 自身带有分布式协调管理功能;
- Solr 支持更多格式的数据,而 Elasticsearch 仅支持json文件格式;
- Solr 官方提供的功能更多,而 Elasticsearch 本身更注重于核心功能,高级功能多有第三方插件提供;
- Solr 在传统的搜索应用中表现好于 Elasticsearch,但在处理实时搜索应用时效率明显低于 Elasticsearch。
- Solr 是传统搜索应用的有力解决方案,但 Elasticsearch 更适用于新兴的实时搜索应用。
ES的主要应用分为两大类:
- 搜索类(带上聚合),考虑事务性,频繁更新,与现有数据库进行同步,通过ES进行查询聚合。
- 日志类,包括日志收集,指标性收集,通过beats等工具收集到kafka等Q中,通过logstash进行转换,输送到ES中,然后通过Kibana进行展示。
1.POM依赖添加/YML配置
<?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 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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zzf</groupId>
<artifactId>es-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es-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-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</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>
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
2.启动类添加@EnableElasticsearchRepositories注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.zzf.es")
public class EsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EsDemoApplication.class, args);
}
}
3.实体类创建
@Field(type=FieldType.Text, analyzer="ik_max_word") 表示该字段是一个文本,并作最大程度拆分,默认建立索引
@Field(type=FieldType.Text,index=false) 表示该字段是一个文本,不建立索引
@Field(type=FieldType.Date) 表示该字段是一个文本,日期类型,默认不建立索引
@Field(type=FieldType.Long) 表示该字段是一个长整型,默认建立索引
@Field(type=FieldType.Keyword) 表示该字段内容是一个文本并作为一个整体不可分,默认建立索引
@Field(type=FieldType.Float) 表示该字段内容是一个浮点类型并作为一个整体不可分,默认建立索引
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Data
@Document(indexName = "my_user", type = "user")
public class UserEntity {
@Id
private String id;
private String name;
private int sex;
private int age;
}
4.Reposiory接口类添加
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface UserReposiory extends ElasticsearchRepository<UserEntity, String> {
}
5.测试
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
public class EsController {
@Autowired
private UserReposiory userReposiory;
@PostMapping("/addUser")
public UserEntity addUser(@RequestBody UserEntity user) {
return userReposiory.save(user);
}
@GetMapping("/findUser")
public Optional<UserEntity> findUser(@RequestParam("id") String id) {
return userReposiory.findById(id);
}
}