安裝流程: 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>
訪問: http://127.0.0.1:9200/

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);
}
}


轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/282294.html
標籤:其他
上一篇:RISC-V 能打 50 年!不必期待 RISC-VI —— 對話 RISC-V CTO Mark Himelstein
