最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Elastic Search搜索引擎在SpringBoot中的實(shí)踐

 更新時(shí)間:2018年01月13日 15:00:46   作者:hansonwang99  
本篇文章主要介紹了Elastic Search搜索引擎在SpringBoot中的實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

實(shí)驗(yàn)環(huán)境

  1. ES版本:5.3.0
  2. spring bt版本:1.5.9

首先當(dāng)然需要安裝好elastic search環(huán)境,最好再安裝上可視化插件 elasticsearch-head來便于我們直觀地查看數(shù)據(jù)。

當(dāng)然這部分可以參考本人的帖子: 《centos7上elastic search安裝填坑記》

我的ES安裝在http://113.209.119.170:9200/這個(gè)地址(該地址需要配到springboot項(xiàng)目中去)

Spring工程創(chuàng)建

這部分沒有特殊要交代的,但有幾個(gè)注意點(diǎn)一定要當(dāng)心

注意在新建項(xiàng)目時(shí)記得勾選web和NoSQL中的Elasticsearch依賴,來張圖說明一下吧:

創(chuàng)建工程時(shí)勾選Nosql中的es依賴選項(xiàng)

項(xiàng)目自動(dòng)生成以后pom.xml中會(huì)自動(dòng)添加spring-boot-starter-data-elasticsearch的依賴:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

本項(xiàng)目中我們使用開源的基于restful的es java客戶端jest,所以還需要在pom.xml中添加jest依賴:

    <dependency>
      <groupId>io.searchbox</groupId>
      <artifactId>jest</artifactId>
    </dependency>

除此之外還必須添加jna的依賴:

    <dependency>
      <groupId>net.java.dev.jna</groupId>
      <artifactId>jna</artifactId>
    </dependency>

否則啟動(dòng)spring項(xiàng)目的時(shí)候會(huì)報(bào)JNA not found. native methods will be disabled.的錯(cuò)誤:

JNA not found. native methods will be disabled.

項(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)目代碼組織如下:

項(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(List<Entity> entityList);
  List<Entity> 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(List<Entity> entityList) {
    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<Entity> 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<Entity> addList = new ArrayList<Entity>();
      addList.add(newEntity);
      cityESService.saveEntity(addList);
      return "OK";
    }else {
      return "Bad input value";
    }
  }

  @RequestMapping(value="/search", method=RequestMethod.GET)
  public List<Entity> save(String name) {
    List<Entity> entityList = null;
    if(StringUtils.isNotEmpty(name)) {
      entityList = cityESService.searchEntity(name);
    }
    return entityList;
  }
}

實(shí)際實(shí)驗(yàn)

增加幾條數(shù)據(jù),可以使用postman工具,也可以直接在瀏覽器中輸入,如增加以下5條數(shù)據(jù):

http://localhost:6325/entityController/save?id=1&name=南京中山陵
http://localhost:6325/entityController/save?id=2&name=中國南京師范大學(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=中國南邊好像沒有叫帶京字的城市了

數(shù)據(jù)插入效果如下(使用可視化插件elasticsearch-head觀看):

數(shù)據(jù)插入效果

我們來做一下搜索的測試:例如我要搜索關(guān)鍵字“南京”

我們?cè)跒g覽器中輸入:

http://localhost:6325/entityController/search?name=南京

搜索結(jié)果如下:

關(guān)鍵字“南京”的搜索結(jié)果

剛才插入的5條記錄中包含關(guān)鍵字“南京”的四條記錄均被搜索出來了!

當(dāng)然這里用的是standard分詞方式,將每個(gè)中文都作為了一個(gè)term,凡是包含“南”、“京”關(guān)鍵字的記錄都被搜索了出來,只是評(píng)分不同而已,當(dāng)然還有其他的一些分詞方式,此時(shí)需要其他分詞插件的支持,此處暫不涉及,后文中再做探索。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實(shí)現(xiàn)反轉(zhuǎn)一個(gè)鏈表的示例代碼

    Java實(shí)現(xiàn)反轉(zhuǎn)一個(gè)鏈表的示例代碼

    本文主要介紹了Java實(shí)現(xiàn)反轉(zhuǎn)一個(gè)鏈表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java編程ThreadLocal上下傳遞源碼解析

    java編程ThreadLocal上下傳遞源碼解析

    這篇文章主要為大家介紹了java編程中ThreadLocal提供的上下傳遞方式的源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Mybatis Generator具體使用小技巧

    Mybatis Generator具體使用小技巧

    本文主要介紹了Mybatis Generator具體使用小技巧,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 快速入門Java中的Lambda表達(dá)式

    快速入門Java中的Lambda表達(dá)式

    Lambda作為函數(shù)式編程中的基礎(chǔ)部分,在其他編程語言中早就廣為使用,但在Java領(lǐng)域中發(fā)展較慢,直到j(luò)ava8,才開始支持Lambda。網(wǎng)上關(guān)于Lambda的教程很多,今天小編給大家分享一篇快速入手Lambda的教程。
    2016-08-08
  • java 容器的快速失敗(fast-fail)機(jī)制

    java 容器的快速失敗(fast-fail)機(jī)制

    Java容器的快速失敗機(jī)制是一種在迭代過程中檢測并處理集合并發(fā)修改的特性,該機(jī)制適用于ArrayList、HashMap等集合類,本文就來介紹一下java 容器的快速失敗(fast-fail)機(jī)制,感興趣的可以了解一下
    2024-11-11
  • Springboot?上傳文件或頭像(MultipartFile、transferTo)

    Springboot?上傳文件或頭像(MultipartFile、transferTo)

    本文主要介紹了Springboot?上傳文件或頭像(MultipartFile、transferTo),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Spring的@Conditional詳解

    Spring的@Conditional詳解

    這篇文章主要介紹了Spring的@Conditional詳解,給想要注入Bean增加限制條件,只有滿足限制條件才會(huì)被構(gòu)造并注入到Spring的IOC容器中,通常和@Bean注解一起使用,需要的朋友可以參考下
    2024-01-01
  • 解決mybatis批量更新(update foreach)失敗的問題

    解決mybatis批量更新(update foreach)失敗的問題

    這篇文章主要介紹了解決mybatis批量更新(update foreach)失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java后端實(shí)現(xiàn)MD5加密的方法

    Java后端實(shí)現(xiàn)MD5加密的方法

    有的時(shí)候因?yàn)闃I(yè)務(wù)的需要,我們要制作關(guān)于密碼的修改功能。而關(guān)于密碼的加密一般都是用MD5,那么這篇文章將介紹如何在Java的后端實(shí)現(xiàn)MD5加密,有需要的可以參考借鑒。
    2016-08-08
  • Spring?Security權(quán)限管理實(shí)現(xiàn)接口動(dòng)態(tài)權(quán)限控制

    Spring?Security權(quán)限管理實(shí)現(xiàn)接口動(dòng)態(tài)權(quán)限控制

    這篇文章主要為大家介紹了Spring?Security權(quán)限管理實(shí)現(xiàn)接口動(dòng)態(tài)權(quán)限控制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06

最新評(píng)論

三都| 卢龙县| 定州市| 松桃| 修水县| 双江| 尚义县| 开江县| 大兴区| 瑞金市| 湖州市| 宜州市| 民丰县| 栾城县| 岚皋县| 湄潭县| 隆尧县| 左云县| 梅河口市| 田阳县| 广宗县| 周至县| 新竹县| 拉孜县| 大新县| 广州市| 淄博市| 阳高县| 余姚市| 巴东县| 沛县| 蒲江县| 青浦区| 深水埗区| 航空| 广宁县| 禄劝| 兴城市| 历史| 长武县| 尼勒克县|