溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ElasticSearch怎么在Spring boot中使用

發布時間:2021-03-25 16:49:13 來源:億速云 閱讀:272 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關ElasticSearch怎么在Spring boot中使用,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1.安裝ElasticSearch

ElasticSearch怎么在Spring boot中使用

解壓,到bin目錄,雙擊elasticsearch.bat

1.1安裝elasticsearch-head

https://github.com/mobz/elasticsearch-head

elasticsearch-head是一個網頁查看elasticsearch狀態,操作的第三方東西

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open http://localhost:9100/

要先安裝node.js

config/elasticsearch.yml 文件增加

http.cors.enabled: true
http.cors.allow-origin: “*”

elasticsearch-head/Gruntfile.js

connect: {
   server: {
    options: {
     hostname: '0.0.0.0',
     port: 9100,
     base: '.',
     keepalive: true
    }
   }
  }

ElasticSearch怎么在Spring boot中使用

8082改成你自己的端口

http://127.0.0.1:8082/swagger-ui.html#/

ElasticSearch怎么在Spring boot中使用

0是第一頁

application.properties

#===es start===
spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
#===es end===

2.porm

<?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>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.1.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 <packaging>jar</packaging>
 
 <properties>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <!--Swagger-UI API文檔生產工具-->
  <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-UI API文檔生產工具-->
 
  <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

package com.example.demo.domain;
 
import org.springframework.data.elasticsearch.annotations.Document;
 
import java.io.Serializable;
 
@Document(indexName = "testgoods", type = "goods")
public class GoodsInfo implements Serializable {
 private Long id;
 private String name;
 private String description;
 
 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;
 }
 
 public String getDescription() {
  return description;
 }
 
 public void setDescription(String description) {
  this.description = description;
 }
 
 public GoodsInfo(Long id, String name, String description) {
  this.id = id;
  this.name = name;
  this.description = description;
 }
 
 public GoodsInfo() {
 }
}

indexName 類似數據庫名稱,type類似表名字

4. GoodsRepository

package com.example.demo.repository;
 
import com.example.demo.domain.GoodsInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
 
@Component
public interface GoodsRepository extends ElasticsearchRepository<GoodsInfo, Long> {
}

這里會幫你封裝了很多了

5. HelloWorldController

package com.example.demo.controller;
 
import com.example.demo.domain.GoodsInfo;
import com.example.demo.repository.GoodsRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Optional;
 
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
 
@Controller
@Api(value = "HelloWorldController|一個用來測試swagger注解的控制器",tags = "HelloWorldController", description = "HelloWorldController")
public class HelloWorldController {
 
 @Autowired
 private GoodsRepository goodsRepository;
 
 @ResponseBody
 @RequestMapping(value = "/hello", method = RequestMethod.GET)
 @ApiOperation(value = "根據用戶編號獲取用戶姓名", notes = "test: 僅1和2有正確返回")
 @ApiImplicitParam(paramType="query", name = "userNumber", value = "用戶編號", required = true, dataType = "Integer")
 public String index(@RequestParam Integer userNumber){
  if(userNumber == 1){
   return "小白";
  }
  else if(userNumber == 2){
   return "小紅";
  }
  else{
   return "未知";
  }
 }
 @ResponseBody
 @RequestMapping(value = "/save", method = RequestMethod.POST)
 @ApiOperation(value = "新增商品")
 public String save(){
  GoodsInfo goodsInfo = new GoodsInfo(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")
 public String delete(@RequestParam(required = true) long id){
  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")
 public String update(@RequestParam(required = true) long id,
       @RequestParam(required = false) String name,
       @RequestParam(required = false) String description){
  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 "no find";
  }
 
 }
 
 @ResponseBody
 @RequestMapping(value = "/getOne", method = RequestMethod.GET)
 @ApiOperation(value = "得到一個商品")
 @ApiImplicitParam(paramType="query", name = "id", value = "商品id", required = true, dataType = "long")
 public Optional<GoodsInfo> getOne(@RequestParam(required = true) long id){
  Optional<GoodsInfo> goodsInfo = goodsRepository.findById(id);
  return goodsInfo;
 }
 
 private SearchQuery getEntitySearchQuery(int pageNumber, String searchContent){
  Pageable pageable = PageRequest.of(pageNumber, 20);
  SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQuery(searchContent)).withPageable(pageable).build();
  return searchQuery;
 }
 
 @ResponseBody
 @RequestMapping(value = "/search", method = RequestMethod.GET)
 @ApiOperation(value = "搜索商品")
 public List<GoodsInfo> search(@RequestParam(required = true) Integer pageNumber, @RequestParam(required = true) String query){
  SearchQuery searchQuery = getEntitySearchQuery(pageNumber, query);
  Page<GoodsInfo> goodsPage = goodsRepository.search(searchQuery);
  return goodsPage.getContent();
 }
 
}

ElasticSearch怎么在Spring boot中使用

上述就是小編為大家分享的ElasticSearch怎么在Spring boot中使用了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女