Elasticsearch是面向檔案(document oriented)的,這意味著它可以存盤整個物件或檔案(document),然而它不僅僅是存盤(store),還會索引(index)每個檔案的內容使之可以被搜索,在Elasticsearch中,你可以對檔案(而非成行成列的資料)進行索引、搜索、排序、過濾,Elasticsearch比較傳統關系型資料庫
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
先安裝docker,如果安裝了寶塔面板可以直接在商店安裝docker

Docker安裝Elasticsearch
拉取鏡像并啟動
#拉取鏡像
docker pull elasticsearch:7.7.0
#啟動鏡像
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0
--name 表示鏡像啟動后的容器名稱
-d: 后臺運行容器,并回傳容器ID;
-e: 指定容器內的環境變數
-p: 指定埠映射,格式為:主機(宿主)埠:容器埠
瀏覽器訪問ip:9200

ElasticSearch面板
推薦瀏覽器安裝Elasticvue插件管理ElasticSearch,Edge插件商店直接能安裝,方便 ,當然安裝ElasticSearch Head也可,
Elasticvue | Edge插件商店

SpringBoot整合
注:如果Linux是自己電腦的虛擬機SpringBoot能直接訪問,但是如果Linux是騰訊云阿里云等云服務器就需要修改ElasticSearch的組態檔才能遠程訪問,不是云服務器請跳過
docker exec -it elasticsearch bash
# docker命令進入elasticsearch
vi config/elasticsearch.yml
#通過vi編輯器打開組態檔
#檔案內容如下
cluster.name: "docker-cluster"
network.host: 0.0.0.0
#添加以下兩行 表示所有ip能訪問elasticsearch
http.cors.enabled: true
http.cors.allow-origin: "*"
#保存檔案 重啟elasticsearch
創建Maven工程 在pom檔案匯入坐標
pom.xml
<?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>cn.appmy</groupId>
<artifactId>appmy-springboot-es</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在cn.appmy下創建啟動類
@SpringBootApplication
public class ESApplication {
public static void main(String[] args) {
SpringApplication.run(ESApplication.class, args);
}
}
創建yml檔案
server:
port: 8080
spring:
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
#如果是云服務器 填服務器ip 記得防火墻
cluster-name: elasticsearch
創建pojo 添加注解進行映射
/**
* @Document:放置到類上
* indexName = "blog1":表示索引的名稱,(小寫)
* type = "article":表示型別
* @Id:放置到欄位id上
* 表示該欄位的值存放到索引庫的_id欄位上,表示主鍵
* @Field:放置到欄位上
* store = true:表示該欄位的值存盤到索引庫
* index = true:表示該欄位的值要建立索參考于搜索
* analyzer = "ik_smart":建立索引的時候使用什么分詞器
* searchAnalyzer = "ik_smart":資料搜索的時候使用什么分詞器(可以不寫)
* type = FieldType.Text:存放欄位的資料型別
*/
@Document(indexName = "blog03",type = "article")
public class Article implements Serializable {
@Id
private Long id;
@Field(index = true,searchAnalyzer = "ik_smart",analyzer = "ik_smart",store = true,type = FieldType.Text)
private String title;
@Field(index = true,searchAnalyzer = "ik_smart",analyzer = "ik_smart",store = true,type = FieldType.Text)
private String content;
public Article() {
}
public Article(long id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
創建Dao介面繼承ElasticsearchRepository
public interface ArticleDao extends ElasticsearchRepository<Article,Long>{
}
測驗
@SpringBootTest
@RunWith(SpringRunner.class)
public class EsApplicationTest03 {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ArticleDao dao;
//創建索引
//創建映射
@Test
public void createMapping() {
elasticsearchTemplate.createIndex(Article.class);
elasticsearchTemplate.putMapping(Article.class);
}
}
原文博客
https://appmy.cn/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/387827.html
標籤:其他
上一篇:8.2 SpringBoot集成ElasticSearch之新增檔案
下一篇:大資料實訓
