中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

陜西印象盒子seo整站優(yōu)化吧

陜西印象盒子,seo整站優(yōu)化吧,龍華專業(yè)網(wǎng)站建設(shè),2022河南工程預(yù)算定額文章目錄 前言一、ElasticSearch本地環(huán)境搭建二、SpringBoot整合ElasticSearch1.pom中引入ES依賴2.application.yaml配置elasticsearch3.ElasticSearchClientConnect連接ES客戶端工具類4.ElasticSearchResult封裝響應(yīng)結(jié)果5.Person實(shí)體類6.Person實(shí)體類7.ElasticsearchControlle…

文章目錄

  • 前言
  • 一、ElasticSearch本地環(huán)境搭建
  • 二、SpringBoot整合ElasticSearch
    • 1.pom中引入ES依賴
    • 2.application.yaml配置elasticsearch
    • 3.ElasticSearchClientConnect連接ES客戶端工具類
    • 4.ElasticSearchResult封裝響應(yīng)結(jié)果
    • 5.Person實(shí)體類
    • 6.Person實(shí)體類
    • 7.ElasticsearchController增刪改查控制層
    • 8.ElasticsearchDocumentController文檔操作控制層
  • 三、測(cè)試
    • 1.創(chuàng)建索引,名為my_index
    • 2.創(chuàng)建文檔,給名為my_index的索引創(chuàng)建文檔
  • 四、項(xiàng)目結(jié)構(gòu)及代碼下載
  • 參考資料


前言

被逼無(wú)奈,各行各業(yè)都在卷,給自己充電學(xué)習(xí)了Elasticsearch,在學(xué)完基礎(chǔ)知識(shí)后(其實(shí)就是CRUD😂),就去Springboot中嘗試整合ES。終于抽出時(shí)間,將學(xué)習(xí)的東西整理分享在此,歡迎大家批評(píng)指正哈~


一、ElasticSearch本地環(huán)境搭建

詳細(xì)安裝步驟參考此資料windows 安裝 Elasticsearch

二、SpringBoot整合ElasticSearch

我這里Spring Boot版本2.7.5,ElasticSearch版本7.17.3。版本一定要對(duì)應(yīng)上,否則會(huì)報(bào)一堆錯(cuò)誤問(wèn)題。
SpringBoot官方推薦對(duì)應(yīng)Elasticsearch版本。
在這里插入圖片描述
先啟動(dòng)ES本地服務(wù)(雙擊elasticsearch.bat啟動(dòng)服務(wù)),然后在elasticsearch-head可視化插件目錄中執(zhí)行npm start run啟動(dòng)可視化插件。
在這里插入圖片描述
在這里插入圖片描述

1.pom中引入ES依賴

<!-- ElasticSearch --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><exclusions><exclusion><artifactId>elasticsearch-java</artifactId><groupId>co.elastic.clients</groupId></exclusion></exclusions></dependency><dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>8.0.1</version><exclusions><exclusion><artifactId>jakarta.json-api</artifactId><groupId>jakarta.json</groupId></exclusion></exclusions></dependency><dependency><groupId>jakarta.json</groupId><artifactId>jakarta.json-api</artifactId><version>2.0.1</version></dependency>

2.application.yaml配置elasticsearch

elasticsearch:port: 9200ip: 127.0.0.1username: elasticpassword: 123456

3.ElasticSearchClientConnect連接ES客戶端工具類

@Configuration
public class ElasticSearchClientConnect {@Value("${elasticsearch.port}")private int port;@Value("${elasticsearch.ip}")private String ip;@Value("${elasticsearch.username}")private String username;@Value("${elasticsearch.password}")private String password;/*** 創(chuàng)建rest客戶端*/@Beanpublic ElasticSearchResult restClient(){RestClient restClient = RestClient.builder(new HttpHost(ip, port,"http")).build();ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());ElasticsearchClient client = new ElasticsearchClient(transport);return new ElasticSearchResult(restClient,transport,client);}
}

4.ElasticSearchResult封裝響應(yīng)結(jié)果

@Data
public class ElasticSearchResult {private RestClient restClient;private ElasticsearchTransport elasticsearchTransport;private ElasticsearchClient elasticsearchClient;public ElasticSearchResult(RestClient restClient, ElasticsearchTransport elasticsearchTransport, ElasticsearchClient elasticsearchClient) {this.restClient = restClient;this.elasticsearchTransport = elasticsearchTransport;this.elasticsearchClient = elasticsearchClient;}
}

5.Person實(shí)體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {private String name;private String sex;private Integer age;
}

6.Person實(shí)體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {private String name;private String sex;private Integer age;
}

7.ElasticsearchController增刪改查控制層

/*** ES增刪改查*/
@RestController
public class ElasticsearchController {@Autowiredprivate ElasticSearchClientConnect elasticSearchClientConfig;/*** 新建my_index索引* @return* @throws IOException*/@GetMapping("/createIndex")public Boolean createIndex() throws IOException {CreateIndexResponse createIndexResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().indices().create(c -> c.index("my_index"));// 打印結(jié)果System.out.println(createIndexResponse.acknowledged());// 關(guān)閉連接elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();return createIndexResponse.acknowledged();}/*** 查詢索引* @throws IOException*/@GetMapping("/selectIndex")public void selectIndex() throws IOException {GetIndexResponse getIndexResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().indices().get(e -> e.index("jing_index"));// 打印結(jié)果System.out.println("getIndexResponse.result() = " + getIndexResponse.result());System.out.println("getIndexResponse.result().keySet() = " + getIndexResponse.result().keySet());// 關(guān)閉連接elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 刪除索引* @return* @throws IOException*/@GetMapping("/deleteIndex")public Boolean deleteIndex() throws IOException {// 刪除索引DeleteIndexResponse deleteIndexResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().indices().delete(e -> e.index("jing_index"));System.out.println("刪除操作 = " + deleteIndexResponse.acknowledged());// 關(guān)閉連接elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();return deleteIndexResponse.acknowledged();}}

8.ElasticsearchDocumentController文檔操作控制層

/*** ES文檔操作*/
@RestController
public class ElasticsearchDocumentController {@Autowiredprivate ElasticSearchClientConnect elasticSearchClientConfig;/*** 添加document* @throws IOException*/@GetMapping("/addDocument")public void addDocument() throws IOException {// 向Person對(duì)象中添加數(shù)據(jù)Person Person = new Person("java客戶端", "男", 18);// 向索引中添加數(shù)據(jù)CreateResponse createResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().create(e -> e.index("jing_index").id("1001").document(Person));System.out.println("createResponse.result() = " + createResponse.result());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 查詢document* @throws IOException*/@GetMapping("/queryDocument")public void queryDocument() throws IOException {// 構(gòu)建請(qǐng)求GetResponse<Person> getResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().get(e -> e.index("jing_index").id("1001"), Person.class);System.out.println("getResponse.source().toString() = " + getResponse.source().toString());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 修改document* @throws IOException*/@GetMapping("/modifyDocument")public void modifyDocument() throws IOException {// 使用map集合封裝需要修改的內(nèi)容Map<String, Object> map = new HashMap<>();map.put("name", "java客戶端aaa");// 構(gòu)建請(qǐng)求UpdateResponse<Person> updateResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().update(e -> e.index("jing_index").id("1001").doc(map), Person.class);System.out.println("updateResponse.result() = " + updateResponse.result());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 刪除document* @throws IOException*/@GetMapping("/removeDocument")public void removeDocument() throws  IOException {// 構(gòu)建請(qǐng)求DeleteResponse deleteResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().delete(e -> e.index("jing_index").id("1001"));System.out.println("deleteResponse.result() = " + deleteResponse.result());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 批量添加document* @throws IOException*/@GetMapping("/batchAddDocument")public void batchAddDocument() throws IOException {// 構(gòu)建一個(gè)批量數(shù)據(jù)集合List<BulkOperation> list = new ArrayList<>();list.add(new BulkOperation.Builder().create(d -> d.document(new Person("test2", "男", 19)).id("1002").index("Person_test")).build());list.add(new BulkOperation.Builder().create(d -> d.document(new Person("test3", "男", 20)).id("1003").index("Person_test")).build());list.add(new BulkOperation.Builder().create(d -> d.document(new Person("test4", "女", 21)).id("1004").index("Person_test")).build());// 調(diào)用bulk方法執(zhí)行批量插入操作BulkResponse bulkResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().bulk(e -> e.index("Person_test").operations(list));System.out.println("bulkResponse.items() = " + bulkResponse.items());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 批量刪除document* @throws IOException*/@GetMapping("/batchDeleteDocument")public void batchDeleteDocument() throws IOException {// 構(gòu)建一個(gè)批量數(shù)據(jù)集合List<BulkOperation> list = new ArrayList<>();list.add(new BulkOperation.Builder().delete(d -> d.id("1002").index("Person_test")).build());list.add(new BulkOperation.Builder().delete(d -> d.id("1003").index("Person_test")).build());// 調(diào)用bulk方法執(zhí)行批量插入操作BulkResponse bulkResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().bulk(e -> e.index("Person_test").operations(list));System.out.println("bulkResponse.items() = " + bulkResponse.items());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 全量查詢document* @throws IOException*/@GetMapping("/queryAllDocument")public void queryAllDocument() throws IOException {// 全量查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(e -> e.index("Person_test").query(q -> q.matchAll(m -> m)), Person.class);HitsMetadata<Person> hits = searchResponse.hits();for (Hit<Person> hit : hits.hits()) {System.out.println("Person = " + hit.source().toString());}System.out.println("searchResponse.hits().total().value() = " + searchResponse.hits().total().value());elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 分頁(yè)查詢document* @throws IOException*/@GetMapping("/pagingQueryDocument")public void pagingQueryDocument() throws IOException {// 分頁(yè)查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.matchAll(m -> m)).from(0).size(2), Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 排序查詢document* @throws IOException*/@GetMapping("/sortQueryDocument")public void sortQueryDocument() throws IOException {// 排序查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.matchAll(m -> m)).sort(o -> o.field(f -> f.field("age").order(SortOrder.Asc))), Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 條件查詢document* @throws IOException*/@GetMapping("/conditionQueryDocument")public void conditionQueryDocument() throws IOException {// 條件查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.matchAll(m -> m)).sort(o -> o.field(f -> f.field("age").order(SortOrder.Asc))).source(r -> r.filter(f -> f.includes("name", "age").excludes(""))), Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 組合查詢  must是必須滿足所有條件,should只要滿足一個(gè)就行* @throws IOException*/@GetMapping("/combinationQueryDocument")public void combinationQueryDocument() throws IOException {// 組合查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.bool(b -> b.must(m -> m.match(u -> u.field("age").query(21))).must(m -> m.match(u -> u.field("sex").query("男"))).mustNot(m -> m.match(u -> u.field("sex").query("女"))))), Person.class);//SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(//                s -> s.index("Person_test").query(q -> q.bool(b -> b//                        .should(h -> h.match(u -> u.field("age").query(19)))//                        .should(h -> h.match(u -> u.field("sex").query("男")))//                ))//                , Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 范圍查詢* @throws IOException*/@GetMapping("/scopeQueryDocument2")public void scopeQueryDocument2() throws IOException {// 范圍查詢,gte()表示取大于等于,gt()表示大于,lte()表示小于等于SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.range(r -> r.field("age").gte(JsonData.of(20)).lt(JsonData.of(21)))), Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 模糊查詢* @throws IOException*/@GetMapping("/fuzzyQueryDocument2")public void fuzzyQueryDocument2() throws IOException {// 模糊查詢,fuzziness表示差幾個(gè)可以查詢出來(lái)SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.fuzzy(f -> f.field("name").value("tst").fuzziness("2"))), Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 高亮查詢* @throws IOException*/@GetMapping("/highlightQueryDocument2")public void highlightQueryDocument2() throws IOException {// 高亮查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").query(q -> q.term(t -> t.field("name").value("test4"))).highlight(h -> h.fields("name", f -> f.preTags("<font color='red'>").postTags("</font>"))), Person.class);searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 聚合查詢* @throws IOException*/@GetMapping("/aggregateQueryDocument2")public void aggregateQueryDocument2() throws IOException {// 聚合查詢,取最大年齡SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").aggregations("maxAge", a ->a.max(m -> m.field("age"))), Person.class);searchResponse.aggregations().entrySet().forEach(f -> System.out.println(f.getKey() + ":" + f.getValue().max().value()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}/*** 分組查詢* @throws IOException*/@GetMapping("/groupQueryDocument2")public void groupQueryDocument2() throws IOException {// 分組查詢SearchResponse<Person> searchResponse = elasticSearchClientConfig.restClient().getElasticsearchClient().search(s -> s.index("Person_test").aggregations("ageGroup", a ->a.terms(t -> t.field("age"))), Person.class);searchResponse.aggregations().get("ageGroup").lterms().buckets().array().forEach(f -> System.out.println(f.key() + ":" + f.docCount()));elasticSearchClientConfig.restClient().getElasticsearchTransport().close();elasticSearchClientConfig.restClient().getRestClient().close();}
}

三、測(cè)試

1.創(chuàng)建索引,名為my_index

在這里插入圖片描述

在這里插入圖片描述
查詢結(jié)果顯示創(chuàng)建成功
在這里插入圖片描述

2.創(chuàng)建文檔,給名為my_index的索引創(chuàng)建文檔

在這里插入圖片描述
在這里插入圖片描述
查詢結(jié)果顯示創(chuàng)建文檔成功
在這里插入圖片描述

四、項(xiàng)目結(jié)構(gòu)及代碼下載

在這里插入圖片描述
源碼下載地址,歡迎Star哦~~
springboot-cacheable


參考資料

SpringBoot官方推薦對(duì)應(yīng)Elasticsearch版本
SpringBoot集成elasticsearch 7.17.3及常規(guī)應(yīng)用
Elasticsearch8.x版本中RestHighLevelClient被棄用
SpringBoot 整合ElasticSearch實(shí)現(xiàn)模糊查詢,批量CRUD,排序,分頁(yè),高亮
windows 安裝 Elasticsearch

http://www.risenshineclean.com/news/3852.html

相關(guān)文章:

  • php做網(wǎng)站開源項(xiàng)目東莞網(wǎng)站設(shè)計(jì)
  • 高中學(xué)校網(wǎng)站模板如何制作一個(gè)網(wǎng)站
  • 網(wǎng)站抄襲別人的做可以嗎網(wǎng)站入口百度
  • 常熟建設(shè)銀行 招聘網(wǎng)站seddog站長(zhǎng)之家
  • 公司網(wǎng)站維護(hù)內(nèi)容重慶seo公司
  • wordpress插件直播長(zhǎng)沙seo工作室
  • 專門做面條菜譜的網(wǎng)站輿情監(jiān)測(cè)軟件免費(fèi)版
  • 網(wǎng)站建設(shè)的費(fèi)用是多少錢購(gòu)物網(wǎng)站如何推廣
  • 做燈箱的網(wǎng)站百度收錄網(wǎng)站提交入口
  • 網(wǎng)站規(guī)劃與建設(shè)大作業(yè)b站網(wǎng)站推廣mmm
  • 做棋牌網(wǎng)站合法熱狗seo顧問(wèn)
  • 黃頁(yè)網(wǎng)站是什么指數(shù)
  • 網(wǎng)站建設(shè) app百度怎么優(yōu)化排名
  • 網(wǎng)站背景跟著鼠標(biāo)動(dòng)的圖怎么做sem優(yōu)化
  • 東湖南昌網(wǎng)站建設(shè)公司杭州網(wǎng)站優(yōu)化效果
  • 網(wǎng)站的形成網(wǎng)站整站優(yōu)化推廣方案
  • 做海報(bào)文案的參考網(wǎng)站什么是網(wǎng)絡(luò)整合營(yíng)銷
  • wordpress站點(diǎn)描述濮陽(yáng)市網(wǎng)站建設(shè)
  • 幫做3d模型的網(wǎng)站南寧seo怎么做優(yōu)化團(tuán)隊(duì)
  • python庫(kù)之web網(wǎng)站開發(fā)PPT今天剛剛發(fā)生的重大新聞
  • 綿陽(yáng)市建設(shè)局網(wǎng)站全網(wǎng)推廣怎么做
  • 中國(guó)工程局人才招聘網(wǎng)臨沂seo推廣外包
  • 開公司可以在哪些網(wǎng)站做推廣鄭州網(wǎng)站建設(shè)公司
  • 網(wǎng)站建設(shè)圖片素材信息流推廣
  • 有沒有做任務(wù)能兌換現(xiàn)金的網(wǎng)站百度提交網(wǎng)址入口
  • 不讓在建設(shè)門戶網(wǎng)站aso優(yōu)化工具
  • 一個(gè)可以做行程的網(wǎng)站最新新聞熱點(diǎn)事件
  • wordpress 后臺(tái)拿shell正規(guī)網(wǎng)絡(luò)公司關(guān)鍵詞排名優(yōu)化
  • 專門 做鞋子團(tuán)購(gòu)的網(wǎng)站快速收錄網(wǎng)
  • wordpress和帝國(guó)東莞網(wǎng)絡(luò)營(yíng)銷優(yōu)化