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

springboot集成elasticsearch7的圖文方法

 更新時間:2021年05月26日 10:46:20   作者:21-夜一  
本文記錄springboot集成elasticsearch7的方法,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友參考下吧

1.創(chuàng)建項目

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
加粗樣式
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

修改依賴版本

在這里插入圖片描述

2.創(chuàng)建配置文件

在這里插入圖片描述

package com.huanmingjie.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchClientConfig {

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

}

3.測試

3.1索引操作

1.創(chuàng)建索引

在這里插入圖片描述
在這里插入圖片描述

2.判斷索引是否存在

在這里插入圖片描述
3.刪除索引
在這里插入圖片描述

索引操作代碼

package com.huanmingjie.elasticsearch;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class ElasticsearchApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    //創(chuàng)建索引 PUT zoomy_index
    @Test
    void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("zoomy_index");
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }

    //判斷索引是否存在
    @Test
    void getIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("zoomy_index");
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    //刪除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("zoomy_index");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }
}

3.2文檔操作

創(chuàng)建實體類

在這里插入圖片描述

package com.huanmingjie.elasticsearch.pojo;


import org.springframework.stereotype.Component;


@Component
public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

1.添加文檔

在這里插入圖片描述
在這里插入圖片描述

2.獲取文檔,判斷是否存在

在這里插入圖片描述

3.獲取文檔信息

在這里插入圖片描述

4.更新文檔

在這里插入圖片描述
在這里插入圖片描述

5.刪除文檔

在這里插入圖片描述
在這里插入圖片描述

3.3實戰(zhàn)操作

批量創(chuàng)建數(shù)據(jù)

在這里插入圖片描述
在這里插入圖片描述

查詢

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

package com.huanmingjie.elasticsearch;

import com.alibaba.fastjson.JSON;
import com.huanmingjie.elasticsearch.pojo.User;
import com.huanmingjie.elasticsearch.utils.ESConstant;
import net.minidev.json.JSONObject;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.naming.directory.SearchResult;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class ElasticsearchApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    //創(chuàng)建索引 PUT zoomy_index
    @Test
    void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("zoomy_index");
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }

    //判斷索引是否存在
    @Test
    void getIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("zoomy_index");
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    //刪除索引
    @Test
    void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("zoomy_index");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

    //添加文檔 PUT zoomy_index/_doc/1
    @Test
    void addDocument() throws IOException {
        User user = new User("zoomy", 21);
        IndexRequest request = new IndexRequest("zoomy_index");
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.source(JSON.toJSONString(user), XContentType.JSON);
        //客戶端發(fā)送請求,獲取響應(yīng)結(jié)果
        IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());
        //命令返回的狀態(tài)
        System.out.println(indexResponse.status());
    }


    //獲取文檔,判斷是否存在
    @Test
    void exitDocument() throws IOException {
        GetRequest request = new GetRequest("zoomy_index", "1");
        //不獲取返回的_source的上下文,效率更高
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");
        boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    //獲取文檔信息
    @Test
    void getDocument() throws IOException {
        GetRequest request = new GetRequest("zoomy_index", "1");
        GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        //打印文檔內(nèi)容
        System.out.println(getResponse.getSourceAsString());
        //返回全部內(nèi)容
        System.out.println(getResponse);

    }

    //更新文檔 POST zoomy_index/_doc/1/_update
    @Test
    void updateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("zoomy_index", "1");
        request.timeout(TimeValue.timeValueSeconds(1));
        User user = new User("zoomy", 22);
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        System.out.println(updateResponse.status());
    }


    //刪除文檔
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("zoomy_index", "1");

        DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.status());
    }

    //批量處理數(shù)據(jù)
    @Test
    void bulkRequest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(TimeValue.timeValueSeconds(10));

        ArrayList<User> userList = new ArrayList<>();
        userList.add(new User("zoomy1", 21));
        userList.add(new User("zoomy2", 22));
        userList.add(new User("zoomy3", 23));

        for (int i = 0; i < userList.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("zoomy_index")
                            .id("" + (i + 1))
                            .source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
        }
        BulkResponse bulkItemResponses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkItemResponses.hasFailures());
    }


    //批量處理數(shù)據(jù)
    @Test
    void searchRequest() throws IOException {
        SearchRequest searchRequest = new SearchRequest(ESConstant.ZOOMY_INDEX);
        //構(gòu)建搜索條件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //查詢條件QueryBuilders工具  termQuery 精確查詢 matchAllQuery匹配所有
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zoomy1");
//        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        searchSourceBuilder.query(termQueryBuilder);
        //from size有默認參數(shù)
//        searchSourceBuilder.from();
//        searchSourceBuilder.size();
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

}

以上就是springboot集成elasticsearch7的詳細內(nèi)容,更多關(guān)于springboot集成elasticsearch7的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java簡單實現(xiàn)八叉樹圖像處理代碼示例

    java簡單實現(xiàn)八叉樹圖像處理代碼示例

    這篇文章主要介紹了java簡單實現(xiàn)八叉樹圖像處理代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Springboot工具類StringUtils使用教程

    Springboot工具類StringUtils使用教程

    這篇文章主要介紹了Springboot內(nèi)置的工具類之StringUtils的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • 使用SpringBoot與Thrift實現(xiàn)RPC通信的方式詳解

    使用SpringBoot與Thrift實現(xiàn)RPC通信的方式詳解

    在微服務(wù)架構(gòu)的世界里,服務(wù)間的通信機制選擇成為了關(guān)鍵決策之一,RPC因其簡潔、高效的特點備受青睞,本文將詳細探討如何利用Spring?Boot和Thrift框架構(gòu)建RPC通信,讓讀者理解其內(nèi)在原理及實現(xiàn)方式,需要的朋友可以參考下
    2023-10-10
  • Java實現(xiàn)文件變化監(jiān)聽代碼實例

    Java實現(xiàn)文件變化監(jiān)聽代碼實例

    這篇文章主要介紹了Java實現(xiàn)文件變化監(jiān)聽代碼實例,通過定時任務(wù),輪訓(xùn)查詢文件的最后修改時間,與上一次進行對比,如果發(fā)生變化,則說明文件已經(jīng)修改,進行重新加載或?qū)?yīng)的業(yè)務(wù)邏輯處理,需要的朋友可以參考下
    2024-01-01
  • 關(guān)于Minio配置文件的使用說明

    關(guān)于Minio配置文件的使用說明

    這篇文章主要介紹了關(guān)于Minio配置文件的使用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • 淺談java中的TreeMap 排序與TreeSet 排序

    淺談java中的TreeMap 排序與TreeSet 排序

    下面小編就為大家?guī)硪黄獪\談java中的TreeMap 排序與TreeSet 排序。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • Maven分模塊開發(fā)執(zhí)行指令失敗的問題

    Maven分模塊開發(fā)執(zhí)行指令失敗的問題

    Maven分模塊開發(fā),行指令失敗,modules.module[3]‘ specifies duplicate child module maven_dao @ line 29, column 1的問題,本文給大家分享解決方法,感興趣的朋友跟隨小編一起看看吧
    2020-09-09
  • SpringMVC攔截器零基礎(chǔ)掌握

    SpringMVC攔截器零基礎(chǔ)掌握

    攔截器(Interceptor)是一種動態(tài)攔截方法調(diào)用的機制,在SpringMVC中動態(tài)攔截控制器方法的執(zhí)行。本文將詳細講講SpringMVC中攔截器的概念及入門案例,感興趣的可以嘗試一下
    2023-03-03
  • SpringMVC 中文亂碼的解決方案

    SpringMVC 中文亂碼的解決方案

    這篇文章主要介紹了SpringMVC 中文亂碼的解決方案,幫助大家更好的理解和學(xué)習(xí)使用SpringMVC,感興趣的朋友可以了解下
    2021-04-04
  • Java并發(fā)編程之Fork/Join框架的理解

    Java并發(fā)編程之Fork/Join框架的理解

    今天帶大家學(xué)習(xí)Java并發(fā)編程的相關(guān)知識,文中對Fork/Join框架作了非常詳細的介紹,對正在學(xué)習(xí)有關(guān)知識的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06

最新評論

宁河县| 中阳县| 体育| 鲜城| 特克斯县| 额济纳旗| 南投市| 宝兴县| 环江| 遂宁市| 抚顺市| 廉江市| 南涧| 保靖县| 阿勒泰市| 东山县| 郧西县| 合水县| 乌兰察布市| 伊春市| 罗源县| 大丰市| 沭阳县| 兰溪市| 五常市| 甘南县| 梨树县| 教育| 喜德县| 郁南县| 固始县| 广平县| 克拉玛依市| 莱阳市| 盘山县| 迁安市| 丰原市| 五华县| 昌图县| 通江县| 象山县|