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

elasticsearch 8.2.3 安裝及springboot簡單使用

 更新時(shí)間:2023年06月14日 15:15:00   作者:陽光傾灑  
這篇文章主要介紹了elasticsearch 8.2.3 安裝及springboot簡單使用,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、下載安裝

官網(wǎng)下載地址https://www.elastic.co/cn/downloads/elasticsearch

解壓
elasticsearch-8.2.3-windows-x86_64

修改配置
elasticsearch-8.2.3\config\elasticsearch.yml

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# --------------------------------- Readiness ----------------------------------
#
# Enable an unauthenticated TCP readiness endpoint on localhost
#
#readiness.port: 9399
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically      
# generated to configure Elasticsearch security features on 21-06-2022 06:14:38
#
# --------------------------------------------------------------------------------
# Enable security features
#xpack.security.enabled: true
#xpack.security.enrollment.enabled: true
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
#xpack.security.http.ssl:
#  enabled: true
#  keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
#xpack.security.transport.ssl:
  #enabled: true
  #verification_mode: certificate
  #keystore.path: certs/transport.p12
  #truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
#cluster.initial_master_nodes: ["DESKTOP-22BJ4SG"]
# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0
# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated
#transport.host: 0.0.0.0
# 集群的名字
cluster.name: es-cluster
# 節(jié)點(diǎn)名字
node.name: es_node1
# ES的監(jiān)聽地址
network.host: 0.0.0.0
#設(shè)置對外服務(wù)的http端口,默認(rèn)為9200
http.port: 9200
#設(shè)置索引數(shù)據(jù)的存儲(chǔ)路徑
path.data: D:/99-tools/elasticsearch-8.2.3-windows-x86_64/elasticsearch-8.2.3/data    
#設(shè)置日志文件的存儲(chǔ)路徑
path.logs: D:/99-tools/elasticsearch-8.2.3-windows-x86_64/elasticsearch-8.2.3/logs    
# 關(guān)閉http訪問限制
xpack.security.enabled: false
# 增加新的參數(shù),head插件可以訪問es
http.cors.enabled: true
http.cors.allow-origin: "*"
# Enable security features
#xpack.security.enabled: false
xpack.security.enrollment.enabled: true
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["es_node1"]  #注意,這個(gè)要與node.name填寫一致
#屏蔽自動(dòng)下載geopip
ingest.geoip.downloader.enabled: false

啟動(dòng)服務(wù)
雙擊elasticsearch-8.2.3\bin\elasticsearch.bat 運(yùn)行
9300:Java程序訪問的端口
9200:瀏覽器、HTTP訪問的端口

http://localhost:9200/

二、客戶端elasticsearch-head安裝

elasticsearch-head是一款專門針對于Elasticsearch的客戶端工具

下載源碼https://github.com/mobz/elasticsearch-head](https://github.com/mobz/elasticsearch-head 

加壓后,在elasticsearch-head目錄下執(zhí)行命令
需要先安裝nodejs,下載地址:http://nodejs.cn/download/

npm install
npm run start

安裝成功
http://localhost:9100/

三、springboot 集成

依賴

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>jakarta.json</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/co.elastic.clients/elasticsearch-java -->
        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.2.3</version>
        </dependency>

配置

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.host-name}")
    private String hostName;
    @Value("${elasticsearch.port}")
    private int port;
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        RestClient client = RestClient.builder(new HttpHost(hostName, port,"http")).build();
        ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }
}

index操作類

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class EsGpsIndexService {
    @Autowired
    private ElasticsearchClient client;
    public final static String INDEX_NAME = "gps";
    /**
     * 增加index
     * @throws IOException
     */
    public boolean create() throws IOException {
        CreateIndexResponse indexResponse = client.indices().create(c -> c.index(INDEX_NAME));
        System.out.println(indexResponse.toString());
        return indexResponse.acknowledged();
    }
    /**
     * 查詢Index
     * @throws IOException
     */
    public void query() throws IOException {
        GetIndexResponse getIndexResponse = client.indices().get(i -> i.index(INDEX_NAME));
        System.out.println(getIndexResponse.toString());
    }
    /**
     * 判斷index是否存在
     * @throws IOException
     */
    public boolean exists() throws IOException {
        BooleanResponse booleanResponse = client.indices().exists(e -> e.index(INDEX_NAME));
        System.out.println(booleanResponse.value());
        return booleanResponse.value();
    }
    /**
     * 刪除index
     * @throws IOException
     */
    public void delete() throws IOException {
        DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index(INDEX_NAME));
        System.out.println(deleteIndexResponse.toString());
    }
}

document 操作類

 
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class EsGpsDocumentService {
    @Autowired
    private ElasticsearchClient client;
    /**
     * 插入document
     * @throws IOException
     */
    public void add(GpsInfo gps) throws IOException {
        IndexResponse indexResponse = client.index(i -> i
                .index(EsGpsIndexService.INDEX_NAME)
                //設(shè)置id
                .id(gps.getId())
                //
                .document(gps));
    }
    /**
     * 更新Document
     * @throws IOException
     */
    public void update(GpsInfo gps) throws IOException {
        UpdateResponse<GpsInfo> updateResponse = client.update(u -> u
                        .index(EsGpsIndexService.INDEX_NAME)
                        .id(gps.getId())
                        .doc(gps)
                , GpsInfo.class);
    }
    /**
     * 判斷Document是否存在
     * @throws IOException
     */
    public void exist(String id) throws IOException {
        BooleanResponse indexResponse = client.exists(e -> e.index(EsGpsIndexService.INDEX_NAME).id(id));
        System.out.println(indexResponse.value());
    }
    /**
     * 查詢Document
     * @throws IOException
     */
    public void get(String id) throws IOException {
        GetResponse<GpsInfo> getResponse = client.get(g -> g
                        .index(EsGpsIndexService.INDEX_NAME)
                        .id("1")
                , GpsInfo.class
        );
        System.out.println(getResponse.source());
    }
    /**
     * 刪除Document
     * @throws IOException
     */
    public void delete(String id) throws IOException {
        DeleteResponse deleteResponse = client.delete(d -> d
                .index(EsGpsIndexService.INDEX_NAME)
                .id("1")
        );
        System.out.println(deleteResponse.id());
    }
    /**
     * 批量插入Document
     * @throws IOException
     */
    public void bulk(List<GpsInfo> gpsList) throws IOException {
        List<BulkOperation> bulkOperationArrayList = new ArrayList<>();
        //遍歷添加到bulk中
        for(GpsInfo gps : gpsList){
            bulkOperationArrayList.add(BulkOperation.of(o->o.index(i->i.document(gps).id(gps.getId()))));
            //bulkOperationArrayList.add(BulkOperation.of(x->x.create(d->d.document(gps).id(gps.getId()).index(EsGpsIndexService.INDEX_NAME))));
        }
        BulkResponse bulkResponse = client.bulk(b -> b.index(EsGpsIndexService.INDEX_NAME).operations(bulkOperationArrayList));
    }
    /**
     * 查詢
     * @throws IOException
     * https://wenku.baidu.com/view/a8954a68862458fb770bf78a6529647d27283439.html
     */
    public void search(String vehicleId,long startTime,long endTime,int page,int pageSize) throws IOException {
        SearchResponse<GpsInfo> search = client.search(s -> s
                .index(EsGpsIndexService.INDEX_NAME)
                .query(q -> q
                        .bool(b -> b
                                .filter(m -> m.term(t -> t.field("vehicleId").value(vehicleId)))
                                .filter(f -> f.range(x-> x.field("gpsTime").gte(JsonData.of(startTime)).lte(JsonData.of(endTime))))
                         ))
                //分頁查詢,從第0頁開始查詢20個(gè)document
                .from(page)
                .size(pageSize)
                //按時(shí)間降序排序
                //.sort(f->f.field(o->o.field("gpsTime").order(SortOrder.Desc)))
                , GpsInfo.class
        );
        for (Hit<GpsInfo> hit : search.hits().hits()) {
            System.out.println(hit.source());
        }
    }
}

實(shí)體

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GpsInfo {
    private String id;
    private String vehicleId;
    private String deviceId;
    private String location;
    private Long gpsTime;
    private Long createTime;
}

測試?yán)?/p>

@RestController
@RequestMapping("/gps")
public class GpsController {
    @Autowired
    EsGpsIndexService esGpsIndexService;
    @Autowired
    EsGpsDocumentService esGpsDocumentService;
    @GetMapping("/test")
    public String test() throws IOException {
        boolean exist = esGpsIndexService.exists();
        if(!exist){
            esGpsIndexService.create();
        }
        esGpsIndexService.query();
        //
        /*GpsInfo gps = new GpsInfo();
        gps.setId("1");
        gps.setDeviceId("1111");
        gps.setVehicleId("123");
        gps.setLocation("12,33");
        gps.setCreateTime(System.currentTimeMillis());
        gps.setGpsTime(System.currentTimeMillis());
        esGpsDocumentService.add(gps);*/
        /*List<GpsInfo> gpsList =  new ArrayList<>();
        int i= 0;
        GpsInfo gps = null;
        long time = 1656041715000L;
        while (i < 10000){
            gps = new GpsInfo();
            gps.setId(String.valueOf(i*10000));
            gps.setDeviceId("1111");
            gps.setVehicleId("123456");
            gps.setLocation("12233.2232,33.2512235");
            gps.setCreateTime(time);
            gps.setGpsTime(time);
            gpsList.add(gps);
            //
            time += 1000;
            i += 1;
        }
        esGpsDocumentService.bulk(gpsList);*/
        long start = 1656041715000L;
        long end   = 1656041725000L;
        String v = "123456";
        esGpsDocumentService.search(v,start,end,0,20);
        return "elasticSearch test!!!!";
    }
}

到此這篇關(guān)于elasticsearch 8.2.3 安裝及springboot簡單使用的文章就介紹到這了,更多相關(guān)elasticsearch 8.2.3 安裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?AOP底層原理及代理模式

    Spring?AOP底層原理及代理模式

    這篇文章主要為大家介紹了Spring?AOP底層原理及代理模式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • RestTemplate自定義請求失敗異常處理示例解析

    RestTemplate自定義請求失敗異常處理示例解析

    這篇文章主要為大家介紹了RestTemplate自定義請求失敗異常處理的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • @TransactionalEventListener的使用和實(shí)現(xiàn)原理分析

    @TransactionalEventListener的使用和實(shí)現(xiàn)原理分析

    這篇文章主要介紹了@TransactionalEventListener的使用和實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java中接口冪等性的五種實(shí)現(xiàn)方法

    java中接口冪等性的五種實(shí)現(xiàn)方法

    在java中保證接口冪等性,即多次調(diào)用同一接口產(chǎn)生與單次調(diào)用相同的結(jié)果,不會(huì)引發(fā)副作用,需要結(jié)合業(yè)務(wù)場景選擇合適的方案,下面就來介紹五種實(shí)現(xiàn)方法,感興趣的可以了解一下
    2025-09-09
  • Java輕松批量替換Word文檔文字內(nèi)容的操作步驟

    Java輕松批量替換Word文檔文字內(nèi)容的操作步驟

    Word 文檔常用于撰寫報(bào)告、合同、通知、技術(shù)文檔等,當(dāng)文檔內(nèi)容較多時(shí),如果需要修改或替換某些詞語、短語,甚至是格式化文本,手動(dòng)操作往往效率很低,所以本文給大家介紹了Java實(shí)現(xiàn)輕松批量替換Word文檔文字內(nèi)容,需要的朋友可以參考下
    2025-09-09
  • Java?Guava異步編程實(shí)踐

    Java?Guava異步編程實(shí)踐

    今天咱們要聊的是Guava在異步編程中的應(yīng)用,讓我們搞清楚為什么要用Guava來處理異步任務(wù),在Java的世界里,異步編程是個(gè)老話題了,但它依舊非常關(guān)鍵,它能讓咱們的應(yīng)用更高效,尤其是在處理那些耗時(shí)的I/O操作
    2023-12-12
  • OutOfMemoryError內(nèi)存不足和StackOverflowError堆棧溢出示例詳解

    OutOfMemoryError內(nèi)存不足和StackOverflowError堆棧溢出示例詳解

    這篇文章主要為大家介紹了OutOfMemoryError內(nèi)存不足和StackOverflowError堆棧溢出示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringBoot實(shí)現(xiàn)WebSocket服務(wù)并讓客戶端實(shí)時(shí)接收

    SpringBoot實(shí)現(xiàn)WebSocket服務(wù)并讓客戶端實(shí)時(shí)接收

    使用SpringBoot和WebSocket可創(chuàng)建實(shí)時(shí)消息推送服務(wù),首先添加WebSocket依賴至pom.xml,配置WebSocket端點(diǎn)和邏輯處理器,通過WebSocketHandler處理消息,使用AnnouncementController模擬消息推送,支持HTML和微信小程序客戶端接收消息,感興趣的可以了解一下
    2024-10-10
  • 大廠面試???快速排序冒泡排序算法

    大廠面試常考:快速排序冒泡排序算法

    快速排序由于排序效率在同為O(N*logN)的幾種排序方法中效率較高,因此經(jīng)常被采用,再加上快速排序思想----分治法也確實(shí)實(shí)用,因此很多軟件公司的筆試面試,包括像BAT、字節(jié)、美團(tuán)等知名IT公司都喜歡考查快速排序原理和手寫源碼
    2021-08-08
  • Android內(nèi)存泄漏實(shí)戰(zhàn)解析

    Android內(nèi)存泄漏實(shí)戰(zhàn)解析

    Java是垃圾回收語言的一種。這篇文章主要介紹了Android內(nèi)存泄漏 的相關(guān)資料,需要的朋友可以參考下
    2016-10-10

最新評論

东兴市| 凌源市| 晋城| 巴林右旗| 云梦县| 黄石市| 化州市| 东明县| 临桂县| 工布江达县| 台江县| 吐鲁番市| 祁门县| 本溪市| 锡林浩特市| 汝阳县| 临邑县| 扬中市| 绥化市| 阿合奇县| 墨竹工卡县| 昭苏县| 眉山市| 吉安县| 太湖县| 泸溪县| 宁国市| 调兵山市| 吉木萨尔县| 香格里拉县| 南漳县| 静海县| 明光市| 延安市| 高淳县| 青铜峡市| 蓬莱市| 宁晋县| 龙口市| 隆安县| 太和县|