欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring boot中使用ElasticSearch的方法詳解

 更新時(shí)間:2019年01月11日 09:49:35   作者:Waiting For You  
這篇文章主要給大家介紹了關(guān)于Spring boot中使用ElasticSearch的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

0.版本選擇

我這里選擇了5.6.x,記得如果spring-boot-starter-parent是1.x可以選擇2.x版本的elasticsearch,版本要對(duì)應(yīng),不然會(huì)有莫名其妙的問(wèn)題

1.安裝ElasticSearch

https://www.elastic.co/downloads/past-releases

windows 測(cè)試的,解壓就能用

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

1.1安裝elasticsearch-head

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

elasticsearch-head是一個(gè)網(wǎng)頁(yè)查看elasticsearch狀態(tài),操作的第三方東西

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
    }
   }
  }

8082改成你自己的端口

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

0是第一頁(yè)

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文檔生產(chǎn)工具-->
  <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文檔生產(chǎn)工具-->
 
  <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的版本號(hào)

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 類似數(shù)據(jù)庫(kù)名稱,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> {
}

這里會(huì)幫你封裝了很多了

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|一個(gè)用來(lái)測(cè)試swagger注解的控制器",tags = "HelloWorldController", description = "HelloWorldController")
public class HelloWorldController {
 
 @Autowired
 private GoodsRepository goodsRepository;
 
 @ResponseBody
 @RequestMapping(value = "/hello", method = RequestMethod.GET)
 @ApiOperation(value = "根據(jù)用戶編號(hào)獲取用戶姓名", notes = "test: 僅1和2有正確返回")
 @ApiImplicitParam(paramType="query", name = "userNumber", value = "用戶編號(hào)", 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(), "這是一個(gè)測(cè)試商品");
  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 = "得到一個(gè)商品")
 @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();
 }
 
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • java跳出for循環(huán)的三種常見方法

    java跳出for循環(huán)的三種常見方法

    這篇文章主要給大家介紹了關(guān)于java跳出for循環(huán)的三種常見方法,需要的朋友可以參考下
    2023-07-07
  • IntelliJ IDEA JRebel 安裝使用圖文教程(熱部署插件)

    IntelliJ IDEA JRebel 安裝使用圖文教程(熱部署插件)

    IDEA 全稱 IntelliJ IDEA,是java語(yǔ)言開發(fā)的集成環(huán)境,IntelliJ在業(yè)界被公認(rèn)為最好的java開發(fā)工具之一。這篇文章主要介紹了IntelliJ IDEA 熱部署插件JRebel 安裝使用圖文教程,需要的朋友可以參考下
    2018-03-03
  • Java 中Flyway的使用詳解

    Java 中Flyway的使用詳解

    這篇文章主要介紹了Java 中Flyway的使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot入門之集成JSP的示例代碼

    SpringBoot入門之集成JSP的示例代碼

    這篇文章主要介紹了SpringBoot入門之集成JSP的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Spring MVC參數(shù)校驗(yàn)詳解(關(guān)于`@RequestBody`返回`400`)

    Spring MVC參數(shù)校驗(yàn)詳解(關(guān)于`@RequestBody`返回`400`)

    這篇文章主要介紹了Spring MVC參數(shù)校驗(yàn)的相關(guān)資料,主要是針對(duì)`@RequestBody`返回`400`的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Spring如何動(dòng)態(tài)自定義logback日志目錄詳解

    Spring如何動(dòng)態(tài)自定義logback日志目錄詳解

    這篇文章主要給大家介紹了關(guān)于Spring如何動(dòng)態(tài)自定義logback日志目錄的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 解析Orika的MapperFacade 屬性賦值的使用問(wèn)題

    解析Orika的MapperFacade 屬性賦值的使用問(wèn)題

    在我們實(shí)際開發(fā)中,常常會(huì)有對(duì)象與對(duì)象之間的轉(zhuǎn)化,或者把一個(gè)對(duì)象的數(shù)據(jù)轉(zhuǎn)化到另一個(gè)數(shù)據(jù)之中,如果我們手動(dòng)的一個(gè)一個(gè)的set就會(huì)比較麻煩,代碼段看起來(lái)也會(huì)比較長(zhǎng)。而Orika的MapperFacade就是解決這個(gè)問(wèn)題的,實(shí)現(xiàn)對(duì)象屬性的復(fù)制
    2021-12-12
  • dom4j從jar包中讀取xml文件的方法

    dom4j從jar包中讀取xml文件的方法

    這篇文章主要介紹了dom4j從jar包中讀取xml文件的方法,需要的朋友可以參考下
    2014-02-02
  • Java中Retry方法的簡(jiǎn)單實(shí)現(xiàn)

    Java中Retry方法的簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要介紹了Java中Retry方法的簡(jiǎn)單實(shí)現(xiàn),Retry主要是利用Java的lambda表達(dá)式和線程接口實(shí)現(xiàn)有返回值和無(wú)返回值的重試,思考了下就寫了一個(gè)簡(jiǎn)易R(shí)etry功能分享出來(lái),需要的朋友可以參考下
    2024-01-01
  • spring boot validation參數(shù)校驗(yàn)實(shí)例分析

    spring boot validation參數(shù)校驗(yàn)實(shí)例分析

    這篇文章主要介紹了spring boot validation參數(shù)校驗(yàn),結(jié)合實(shí)例形式分析了spring boot validation進(jìn)行數(shù)據(jù)有效性驗(yàn)證的相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11

最新評(píng)論