摘要:注本文首發(fā)于公眾號(hào),可長(zhǎng)按或掃描下面的小心心來(lái)訂閱實(shí)驗(yàn)環(huán)境版本版本首先當(dāng)然需要安裝好環(huán)境,最好再安裝上可視化插件來(lái)便于我們直觀地查看數(shù)據(jù)。
注: 本文首發(fā)于 My 公眾號(hào) CodeSheep ,可 長(zhǎng)按 或 掃描 下面的 小心心 來(lái)訂閱 ↓ ↓ ↓實(shí)驗(yàn)環(huán)境
ES版本:5.3.0
spring bt版本:1.5.9
首先當(dāng)然需要安裝好elastic search環(huán)境,最好再安裝上可視化插件 elasticsearch-head來(lái)便于我們直觀地查看數(shù)據(jù)。
當(dāng)然這部分可以參考本人的帖子:
《centos7上elastic search安裝填坑記》
https://www.jianshu.com/p/04f...
我的ES安裝在http://113.209.119.170:9200/這個(gè)地址(該地址需要配到springboot項(xiàng)目中去)
Spring工程創(chuàng)建這部分沒(méi)有特殊要交代的,但有幾個(gè)注意點(diǎn)一定要當(dāng)心
注意在新建項(xiàng)目時(shí)記得勾選web和NoSQL中的Elasticsearch依賴(lài),來(lái)張圖說(shuō)明一下吧:
項(xiàng)目自動(dòng)生成以后pom.xml中會(huì)自動(dòng)添加spring-boot-starter-data-elasticsearch的依賴(lài):
org.springframework.boot spring-boot-starter-data-elasticsearch
本項(xiàng)目中我們使用開(kāi)源的基于restful的es java客戶端jest,所以還需要在pom.xml中添加jest依賴(lài):
io.searchbox jest
除此之外還必須添加jna的依賴(lài):
net.java.dev.jna jna
否則啟動(dòng)spring項(xiàng)目的時(shí)候會(huì)報(bào)JNA not found. native methods will be disabled.的錯(cuò)誤:
項(xiàng)目的配置文件application.yml中需要把es服務(wù)器地址配置對(duì)
server: port: 6325 spring: elasticsearch: jest: uris: - http://113.209.119.170:9200 # ES服務(wù)器的地址! read-timeout: 5000代碼組織
我的項(xiàng)目代碼組織如下:
各部分代碼詳解如下,注釋都有:
Entity.java
package com.hansonwang99.springboot_es_demo.entity; import java.io.Serializable; import org.springframework.data.elasticsearch.annotations.Document; public class Entity implements Serializable{ private static final long serialVersionUID = -763638353551774166L; public static final String INDEX_NAME = "index_entity"; public static final String TYPE = "tstype"; private Long id; private String name; public Entity() { super(); } public Entity(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
TestService.java
package com.hansonwang99.springboot_es_demo.service; import com.hansonwang99.springboot_es_demo.entity.Entity; import java.util.List; public interface TestService { void saveEntity(Entity entity); void saveEntity(ListentityList); List searchEntity(String searchContent); }
TestServiceImpl.java
package com.hansonwang99.springboot_es_demo.service.impl; import java.io.IOException; import java.util.List; import com.hansonwang99.springboot_es_demo.entity.Entity; import com.hansonwang99.springboot_es_demo.service.TestService; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import io.searchbox.client.JestClient; import io.searchbox.client.JestResult; import io.searchbox.core.Bulk; import io.searchbox.core.Index; import io.searchbox.core.Search; @Service public class TestServiceImpl implements TestService { private static final Logger LOGGER = LoggerFactory.getLogger(TestServiceImpl.class); @Autowired private JestClient jestClient; @Override public void saveEntity(Entity entity) { Index index = new Index.Builder(entity).index(Entity.INDEX_NAME).type(Entity.TYPE).build(); try { jestClient.execute(index); LOGGER.info("ES 插入完成"); } catch (IOException e) { e.printStackTrace(); LOGGER.error(e.getMessage()); } } /** * 批量保存內(nèi)容到ES */ @Override public void saveEntity(ListentityList) { Bulk.Builder bulk = new Bulk.Builder(); for(Entity entity : entityList) { Index index = new Index.Builder(entity).index(Entity.INDEX_NAME).type(Entity.TYPE).build(); bulk.addAction(index); } try { jestClient.execute(bulk.build()); LOGGER.info("ES 插入完成"); } catch (IOException e) { e.printStackTrace(); LOGGER.error(e.getMessage()); } } /** * 在ES中搜索內(nèi)容 */ @Override public List searchEntity(String searchContent){ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //searchSourceBuilder.query(QueryBuilders.queryStringQuery(searchContent)); //searchSourceBuilder.field("name"); searchSourceBuilder.query(QueryBuilders.matchQuery("name",searchContent)); Search search = new Search.Builder(searchSourceBuilder.toString()) .addIndex(Entity.INDEX_NAME).addType(Entity.TYPE).build(); try { JestResult result = jestClient.execute(search); return result.getSourceAsObjectList(Entity.class); } catch (IOException e) { LOGGER.error(e.getMessage()); e.printStackTrace(); } return null; } }
EntityController.java
package com.hansonwang99.springboot_es_demo.controller; import java.util.ArrayList; import java.util.List; import com.hansonwang99.springboot_es_demo.entity.Entity; import com.hansonwang99.springboot_es_demo.service.TestService; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/entityController") public class EntityController { @Autowired TestService cityESService; @RequestMapping(value="/save", method=RequestMethod.GET) public String save(long id, String name) { System.out.println("save 接口"); if(id>0 && StringUtils.isNotEmpty(name)) { Entity newEntity = new Entity(id,name); List實(shí)際實(shí)驗(yàn)addList = new ArrayList (); addList.add(newEntity); cityESService.saveEntity(addList); return "OK"; }else { return "Bad input value"; } } @RequestMapping(value="/search", method=RequestMethod.GET) public List save(String name) { List entityList = null; if(StringUtils.isNotEmpty(name)) { entityList = cityESService.searchEntity(name); } return entityList; } }
增加幾條數(shù)據(jù),可以使用postman工具,也可以直接在瀏覽器中輸入,如增加以下5條數(shù)據(jù):
http://localhost:6325/entityController/save?id=1&name=南京中山陵 http://localhost:6325/entityController/save?id=2&name=中國(guó)南京師范大學(xué) http://localhost:6325/entityController/save?id=3&name=南京夫子廟 http://localhost:6325/entityController/save?id=4&name=杭州也非常不錯(cuò) http://localhost:6325/entityController/save?id=5&name=中國(guó)南邊好像沒(méi)有叫帶京字的城市了
數(shù)據(jù)插入效果如下(使用可視化插件elasticsearch-head觀看):
我們來(lái)做一下搜索的測(cè)試:例如我要搜索關(guān)鍵字“南京”
我們?cè)跒g覽器中輸入:
http://localhost:6325/entityController/search?name=南京
搜索結(jié)果如下:
剛才插入的5條記錄中包含關(guān)鍵字“南京”的四條記錄均被搜索出來(lái)了!
當(dāng)然這里用的是standard分詞方式,將每個(gè)中文都作為了一個(gè)term,凡是包含“南”、“京”關(guān)鍵字的記錄都被搜索了出來(lái),只是評(píng)分不同而已,當(dāng)然還有其他的一些分詞方式,此時(shí)需要其他分詞插件的支持,此處暫不涉及,后文中再做探索。
后記作者更多的原創(chuàng)文章在此,歡迎觀賞
My Personal Blog
作者更多的SpringBt實(shí)踐文章在此:
Spring Boot應(yīng)用監(jiān)控實(shí)戰(zhàn)
SpringBoot應(yīng)用部署于外置Tomcat容器
ElasticSearch搜索引擎在SpringBt中的實(shí)踐
初探Kotlin+SpringBoot聯(lián)合編程
Spring Boot日志框架實(shí)踐
SpringBoot優(yōu)雅編碼之:Lombok加持
如果有興趣,也可以抽點(diǎn)時(shí)間看看作者一些關(guān)于容器化、微服務(wù)化方面的文章:
利用K8S技術(shù)棧打造個(gè)人私有云 連載文章
從一份配置清單詳解Nginx服務(wù)器配置
Docker容器可視化監(jiān)控中心搭建
利用ELK搭建Docker容器化應(yīng)用日志中心
RPC框架實(shí)踐之:Apache Thrift
RPC框架實(shí)踐之:Google gRPC
微服務(wù)調(diào)用鏈追蹤中心搭建
Docker容器跨主機(jī)通信
Docker Swarm集群初探
高效編寫(xiě)Dockerfile的幾條準(zhǔn)則
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/68215.html
閱讀 1368·2021-11-25 09:43
閱讀 1931·2021-11-12 10:36
閱讀 6132·2021-09-22 15:05
閱讀 3508·2019-08-30 15:55
閱讀 2056·2019-08-26 14:06
閱讀 3672·2019-08-26 12:17
閱讀 537·2019-08-23 17:55
閱讀 2483·2019-08-23 16:23