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

springboot集成es詳解

 更新時(shí)間:2020年11月03日 14:34:15   作者:qq_43381763  
這篇文章主要介紹了springboot集成es,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.導(dǎo)入 maven依賴

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

注意 保持版本一致 我用的是7.6.2版本的

<properties>
      <java.version>1.8</java.version>
       <elasticsearch.version>7.6.2</elasticsearch.version>
     <!--自定義版本 保持版本一致-->
    </properties>

2.編寫(xiě)config類 相當(dāng)于 xlm導(dǎo)入文檔

@Configuration
public class ESConfig {

  @Bean
  public RestHighLevelClient restHighLevelClient (){
    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
        RestClient.builder(
            new HttpHost("localhost",9100,"http")
        )
    );
    return restHighLevelClient;
  }

注意這里的端口號(hào) 一定不能搞錯(cuò)

3測(cè)試書(shū)寫(xiě) 添加 索引

@Test
  void contextLoads() throws IOException {

    //1.創(chuàng)建索引的請(qǐng)求
    CreateIndexRequest createIndexRequest = new CreateIndexRequest("mao");
    //2.執(zhí)行請(qǐng)求 獲得響應(yīng)
    CreateIndexResponse createIndexResponse = estHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    System.out.println(createIndexResponse);
  }

4.查詢索引是否存在

@Test //查詢索引是否存在
  void existIndex() throws IOException {
    GetIndexRequest getIndexRequest = new GetIndexRequest("test"); //獲得索引請(qǐng)求
    boolean exists = estHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    System.out.println(exists);
  }

5.刪除索引

@Test//刪除
  void delIndex() throws IOException {
    DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test");
    AcknowledgedResponse delete = estHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
    System.out.println(delete);
    System.out.println(delete.isAcknowledged());
  }

6.添加文檔數(shù)據(jù) 第一 要設(shè)置實(shí)體類 導(dǎo)入阿里巴巴JSON 工具類

 @Data
@Accessors(chain = true) //實(shí)體類

public class User {
  private String name;
  private String age;
}
@Test //添加文檔
  void addDocument() throws IOException {
    //創(chuàng)建對(duì)象啊
    User user = new User().setAge("13").setName("mao");
    //創(chuàng)建請(qǐng)求
    IndexRequest request = new IndexRequest("mao");
    //設(shè)置規(guī)則 PUT /test/_doc/id
    request.id("1");
    request.timeout("1s");
    //將請(qǐng)求放入josn
    request.source(JSON.toJSONString(user),XContentType.JSON);
    //客戶端發(fā)送請(qǐng)求
    IndexResponse index = estHighLevelClient.index(request, RequestOptions.DEFAULT);
    //獲取響應(yīng)結(jié)果
    System.out.println(index.toString());
    System.out.println(index.status());
 <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

7.修改文檔

@Test //Update 文檔操作
  void GengXin() throws IOException {
    UpdateRequest updateRequest = new UpdateRequest("mao","1"); //請(qǐng)求更新文檔
    updateRequest.timeout("1s"); //設(shè)置超時(shí)時(shí)間
    User user= new User().setName("張三").setAge("26");
    updateRequest.doc(JSON.toJSONString(user),XContentType.JSON); //將對(duì)象封裝丟進(jìn)去 XContentType方法 將要傳輸?shù)臄?shù)據(jù)進(jìn)行告知
    UpdateResponse update = estHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);//發(fā)送請(qǐng)求
    System.out.println(update);
  }

8.批量增加

@Test //批量丟入數(shù)據(jù)
  void TestBulkIndexRequest() throws IOException {
    BulkRequest bulkRequest = new BulkRequest(); //大批量導(dǎo)入數(shù)據(jù) 本質(zhì)是for循環(huán)
    bulkRequest.timeout("10s");
    ArrayList<User> users = new ArrayList<>();
    for(int i=0;i<10;i++){
      users.add(new User().setName("張三"+i+"號(hào)").setAge(""+i));
    }

    //批處理請(qǐng)求
    for(int i =0;i<users.size();i++){
      bulkRequest.add(
          new IndexRequest("mao")
          .id(""+(i+1))
          .source(JSON.toJSONString(users.get(i)),XContentType.JSON)
      );

    }
    BulkResponse bulk = estHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    System.out.println(bulk);
    System.out.println(bulk.hasFailures());//查詢是否失敗

  }

9.精確查詢

 @Test//查詢
  void testSearch() throws IOException {
    SearchRequest searchRequest = new SearchRequest(ESConstant.ESConstant);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    //精確查詢條件


    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "0");
    //查詢所有
//    MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
     searchSourceBuilder.query(termQueryBuilder) ;//將規(guī)則加入
//     searchSourceBuilder.from(); //設(shè)置分頁(yè)
//     searchSourceBuilder.size();
     searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));//設(shè)置高并發(fā)下的延遲時(shí)間
     searchSourceBuilder.highlighter();
     searchRequest.source(searchSourceBuilder);//將剛剛做的請(qǐng)求體放入
    SearchResponse search = estHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//請(qǐng)求信息
    System.out.println(JSON.toJSONString(search.getHits()));//返回查詢情況 getHits 封裝返回對(duì)象

    for( SearchHit SearchHit:search.getHits().getHits() ){
      System.out.println(SearchHit.getSourceAsMap());

    }
  } 

到此這篇關(guān)于springboot集成es詳解的文章就介紹到這了,更多相關(guān)springboot集成es內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解用Spring Boot Admin來(lái)監(jiān)控我們的微服務(wù)

    詳解用Spring Boot Admin來(lái)監(jiān)控我們的微服務(wù)

    這篇文章主要介紹了用Spring Boot Admin來(lái)監(jiān)控我們的微服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java--Socket通信(客戶端服務(wù)端雙向)

    Java--Socket通信(客戶端服務(wù)端雙向)

    這篇文章主要介紹了Java--Socket通信(客戶端服務(wù)端雙向),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Java將字符串寫(xiě)入文本文件代碼示例

    Java將字符串寫(xiě)入文本文件代碼示例

    這篇文章主要介紹了Java將字符串寫(xiě)入文本文件代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 關(guān)于RedisTemplate之opsForValue的使用說(shuō)明

    關(guān)于RedisTemplate之opsForValue的使用說(shuō)明

    這篇文章主要介紹了關(guān)于RedisTemplate之opsForValue的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • springboot實(shí)現(xiàn)文件上傳步驟解析

    springboot實(shí)現(xiàn)文件上傳步驟解析

    這篇文章主要介紹了springboot實(shí)現(xiàn)文件上傳步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 一文探尋Java裝箱和拆箱的奧妙

    一文探尋Java裝箱和拆箱的奧妙

    Java中的裝箱(boxing)和拆箱(unboxing)是指將基本數(shù)據(jù)類型與其對(duì)應(yīng)的包裝類之間進(jìn)行轉(zhuǎn)換的過(guò)程。本文就來(lái)帶大家探索一下Java裝箱和拆箱的奧妙吧
    2023-04-04
  • 解決項(xiàng)目沒(méi)有build path的問(wèn)題

    解決項(xiàng)目沒(méi)有build path的問(wèn)題

    這篇文章主要介紹了解決項(xiàng)目沒(méi)有build path的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)

    ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)

    下面小編就為大家?guī)?lái)一篇ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Java排序算法之選擇排序代碼實(shí)例

    Java排序算法之選擇排序代碼實(shí)例

    這篇文章主要介紹了Java排序算法之選擇排序代碼實(shí)例,從數(shù)組的第一個(gè)元素開(kāi)始,每次遍歷數(shù)組找出一個(gè)最小值放在最左側(cè),第二次從第二個(gè)元素開(kāi)始,依次類推,直到起始元素為數(shù)組的倒數(shù)第二個(gè)元素時(shí),直接和最后一個(gè)元素比較,較小值放左邊,完成排序,需要的朋友可以參考下
    2023-11-11
  • mybatis動(dòng)態(tài)SQL?if的test寫(xiě)法及規(guī)則詳解

    mybatis動(dòng)態(tài)SQL?if的test寫(xiě)法及規(guī)則詳解

    這篇文章主要介紹了mybatis動(dòng)態(tài)SQL?if的test寫(xiě)法及規(guī)則詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評(píng)論