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

springboot集成elasticsearch7的圖文方法

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

1.創(chuàng)建項(xià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.測(cè)試

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)建實(shí)體類

在這里插入圖片描述

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實(shí)戰(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ā)送請(qǐng)求,獲取響應(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());
        //返回全部?jī)?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有默認(rèn)參數(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的詳細(xì)內(nèi)容,更多關(guān)于springboot集成elasticsearch7的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

    Springboot工具類StringUtils使用教程

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

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

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

    Java實(shí)現(xiàn)文件變化監(jiān)聽(tīng)代碼實(shí)例

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

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

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

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

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

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

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

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

    攔截器(Interceptor)是一種動(dòng)態(tài)攔截方法調(diào)用的機(jī)制,在SpringMVC中動(dòng)態(tài)攔截控制器方法的執(zhí)行。本文將詳細(xì)講講SpringMVC中攔截器的概念及入門(mén)案例,感興趣的可以嘗試一下
    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)知識(shí),文中對(duì)Fork/Join框架作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)有關(guān)知識(shí)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06

最新評(píng)論

横峰县| 龙井市| 申扎县| 利辛县| 祁阳县| 喀喇| 富平县| 玛曲县| 泗水县| 涡阳县| 喀喇沁旗| 鄂伦春自治旗| 德令哈市| 志丹县| 三原县| 基隆市| 台北市| 绥阳县| 襄樊市| 嫩江县| 靖州| 苍溪县| 阳山县| 炎陵县| 徐汇区| 通渭县| 桃江县| 泽州县| 金昌市| 登封市| 奉贤区| 梓潼县| 郎溪县| 邛崃市| 高雄县| 叶城县| 乾安县| 高要市| 买车| 周宁县| 宁蒗|