ElasticSearch怎么在Spring boot中使用
作者
这期内容当中小编将会给大家带来有关ElasticSearch怎么在Spring boot中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
1.安装ElasticSearch
解压,到bin目录,双击elasticsearch.bat
1.1安装elasticsearch-head
https://github.com/mobz/elasticsearch-head
elasticsearch-head是一个网页查看elasticsearch状态,操作的第三方东西
gitclonegit://github.com/mobz/elasticsearch-head.git cdelasticsearch-head npminstall npmrunstart openhttp://localhost:9100/
要先安装node.js
config/elasticsearch.yml 文件增加
http.cors.enabled: truehttp.cors.allow-origin: “*”
elasticsearch-head/Gruntfile.js
connect:{ server:{ options:{ hostname:'0.0.0.0', port:9100, base:'.', keepalive:true } } }
8082改成你自己的端口
http://127.0.0.1:8082/swagger-ui.html#/
0是第一页
application.properties
#===esstart=== spring.data.elasticsearch.repositories.enabled=true spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 #===esend===
2.porm
<?xmlversion="1.0"encoding="UTF-8"?> <projectxmlns="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.0http://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.1.1.RELEASE</version> <relativePath/><!--lookupparentfromrepository--> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>DemoprojectforSpringBoot</description> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--Swagger-UIAPI文档生产工具--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <!--Swagger-UIAPI文档生产工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
主要添加spring-boot-starter-data-elasticsearch,注意spring-boot-starter-parent的版本号
3. GoodsInfo
packagecom.example.demo.domain; importorg.springframework.data.elasticsearch.annotations.Document; importjava.io.Serializable; @Document(indexName="testgoods",type="goods") publicclassGoodsInfoimplementsSerializable{ privateLongid; privateStringname; privateStringdescription; publicLonggetId(){ returnid; } publicvoidsetId(Longid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetDescription(){ returndescription; } publicvoidsetDescription(Stringdescription){ this.description=description; } publicGoodsInfo(Longid,Stringname,Stringdescription){ this.id=id; this.name=name; this.description=description; } publicGoodsInfo(){ } }
indexName 类似数据库名称,type类似表名字
4. GoodsRepository
packagecom.example.demo.repository; importcom.example.demo.domain.GoodsInfo; importorg.springframework.data.elasticsearch.repository.ElasticsearchRepository; importorg.springframework.stereotype.Component; @Component publicinterfaceGoodsRepositoryextendsElasticsearchRepository<GoodsInfo,Long>{ }
这里会帮你封装了很多了
5. HelloWorldController
packagecom.example.demo.controller; importcom.example.demo.domain.GoodsInfo; importcom.example.demo.repository.GoodsRepository; importio.swagger.annotations.Api; importio.swagger.annotations.ApiImplicitParam; importio.swagger.annotations.ApiOperation; importorg.elasticsearch.index.query.QueryBuilder; importorg.elasticsearch.index.query.QueryBuilders; importorg.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.data.domain.Page; importorg.springframework.data.domain.PageRequest; importorg.springframework.data.domain.Pageable; importorg.springframework.data.elasticsearch.core.query.NativeSearchQuery; importorg.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; importorg.springframework.data.elasticsearch.core.query.SearchQuery; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.*; importjava.util.List; importjava.util.Optional; importstaticorg.elasticsearch.index.query.QueryBuilders.queryStringQuery; @Controller @Api(value="HelloWorldController|一个用来测试swagger注解的控制器",tags="HelloWorldController",description="HelloWorldController") publicclassHelloWorldController{ @Autowired privateGoodsRepositorygoodsRepository; @ResponseBody @RequestMapping(value="/hello",method=RequestMethod.GET) @ApiOperation(value="根据用户编号获取用户姓名",notes="test:仅1和2有正确返回") @ApiImplicitParam(paramType="query",name="userNumber",value="用户编号",required=true,dataType="Integer") publicStringindex(@RequestParamIntegeruserNumber){ if(userNumber==1){ return"小白"; } elseif(userNumber==2){ return"小红"; } else{ return"未知"; } } @ResponseBody @RequestMapping(value="/save",method=RequestMethod.POST) @ApiOperation(value="新增商品") publicStringsave(){ GoodsInfogoodsInfo=newGoodsInfo(System.currentTimeMillis(),"商品"+System.currentTimeMillis(),"这是一个测试商品"); goodsRepository.save(goodsInfo); return"success"; } @ResponseBody @RequestMapping(value="/delete",method=RequestMethod.POST) @ApiOperation(value="删除商品") @ApiImplicitParam(paramType="query",name="id",value="商品id",required=true,dataType="long") publicStringdelete(@RequestParam(required=true)longid){ goodsRepository.deleteById(id); return"success"; } @ResponseBody @RequestMapping(value="/update",method=RequestMethod.POST) @ApiOperation(value="更新商品") @ApiImplicitParam(paramType="query",name="id",value="商品id",required=true,dataType="long") publicStringupdate(@RequestParam(required=true)longid, @RequestParam(required=false)Stringname, @RequestParam(required=false)Stringdescription){ Optional<GoodsInfo>goodsInfo=goodsRepository.findById(id); if(goodsInfo.isPresent()){ if(name!=null){ goodsInfo.get().setName(name); } if(description!=null){ goodsInfo.get().setDescription(description); } goodsRepository.save(goodsInfo.get()); return"success"; }else{ return"nofind"; } } @ResponseBody @RequestMapping(value="/getOne",method=RequestMethod.GET) @ApiOperation(value="得到一个商品") @ApiImplicitParam(paramType="query",name="id",value="商品id",required=true,dataType="long") publicOptional<GoodsInfo>getOne(@RequestParam(required=true)longid){ Optional<GoodsInfo>goodsInfo=goodsRepository.findById(id); returngoodsInfo; } privateSearchQuerygetEntitySearchQuery(intpageNumber,StringsearchContent){ Pageablepageable=PageRequest.of(pageNumber,20); SearchQuerysearchQuery=newNativeSearchQueryBuilder().withQuery(queryStringQuery(searchContent)).withPageable(pageable).build(); returnsearchQuery; } @ResponseBody @RequestMapping(value="/search",method=RequestMethod.GET) @ApiOperation(value="搜索商品") publicList<GoodsInfo>search(@RequestParam(required=true)IntegerpageNumber,@RequestParam(required=true)Stringquery){ SearchQuerysearchQuery=getEntitySearchQuery(pageNumber,query); Page<GoodsInfo>goodsPage=goodsRepository.search(searchQuery); returngoodsPage.getContent(); } }
上述就是小编为大家分享的ElasticSearch怎么在Spring boot中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注恰卡编程网行业资讯频道。
目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~