elasticsearch 8.2.3 安装及springboot简单使用

这篇具有很好参考价值的文章主要介绍了elasticsearch 8.2.3 安装及springboot简单使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、下载安装

  • 官网下载地址
    https://www.elastic.co/cn/downloads/elasticsearch

    elasticsearch 8.2.3 安装及springboot简单使用

     文章来源地址https://www.toymoban.com/news/detail-508646.html

  • 解压
    elasticsearch-8.2.3-windows-x86_64

    elasticsearch 8.2.3 安装及springboot简单使用

     

  • 修改配置
    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

# 节点名字
node.name: es_node1

# ES的监听地址
network.host: 0.0.0.0

#设置对外服务的http端口,默认为9200
http.port: 9200

#设置索引数据的存储路径
path.data: D:/99-tools/elasticsearch-8.2.3-windows-x86_64/elasticsearch-8.2.3/data    
#设置日志文件的存储路径
path.logs: D:/99-tools/elasticsearch-8.2.3-windows-x86_64/elasticsearch-8.2.3/logs    

# 关闭http访问限制
xpack.security.enabled: false

# 增加新的参数,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"]  #注意,这个要与node.name填写一致

#屏蔽自动下载geopip
ingest.geoip.downloader.enabled: false

  • 启动服务
    双击elasticsearch-8.2.3\bin\elasticsearch.bat 运行
    9300:Java程序访问的端口
    9200:浏览器、HTTP访问的端口
http://localhost:9200/

elasticsearch 8.2.3 安装及springboot简单使用

 

二、客户端elasticsearch-head安装

  • elasticsearch-head是一款专门针对于Elasticsearch的客户端工具

  • 下载源码
    https://github.com/mobz/elasticsearch-head](https://github.com/mobz/elasticsearch-head

  • 加压后,在elasticsearch-head目录下执行命令
    需要先安装nodejs,下载地址:http://nodejs.cn/download/

npm install
npm run start
  • 安装成功
    http://localhost:9100/

elasticsearch 8.2.3 安装及springboot简单使用

 

三、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)
                //设置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个document
                .from(page)
                .size(pageSize)
                //按时间降序排序
                //.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());
        }
    }
}
  • 实体
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;

}
  • 测试例子
@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!!!!";
    }

}

到了这里,关于elasticsearch 8.2.3 安装及springboot简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用简单迅速的方法在CentOS 上安装 Elasticsearch

    对于在CentOS 7上安装Elasticsearch,我们推荐使用官方Elasticsearch存储库中提供的rpm软件包, 教程中使用的是获取最新的7.x版本,需要Java 8或以上版本,你可以选择安装官方Oracle Java发行版或OpenJDK 8,我们本着\\\"开源精神\\\",我们将选择安装OpenJDK 8。 安装 OpenJDK 8: sudo yum install java

    2024年02月05日
    浏览(34)
  • Elasticsearch 7.6.1 安装和SpringBoot 使用

    下载地址 :Elasticesearch 7.6.1 双击bin目录下的elasticsearch.bat 原因:java 版本过低,建议使用jdk11 解决1:升级jdk11 解决2:找到config/jvm.options,编辑; 在最后一行添加: 配置后,再次启动elasticsearch,就只会提示建议使用jdk11 参考地址:AwuAwu Elasticsearch启动报错:future versions of Ela

    2023年04月09日
    浏览(33)
  • Fritzing0.9.10下载安装和简单使用

    Fritzing是个电子设计自动化软件。它支持设计师,艺术家,研究人员和爱好者参加从物理原型到进一步实际的产品。还支持用户记录其Arduino和其他电子为基础的原型,与他人分享,在教室里教电子,并建立一家生产印刷电路板的布局。 首先本博主免费提供 Fritzing0.9.10 安装包

    2023年04月09日
    浏览(34)
  • ES可视化工具--elasticsearch-head--下载、安装、使用

    原文网址:ES可视化工具--elasticsearch-head--下载、安装、使用_IT利刃鞘的博客-CSDN博客 说明         本文介绍ES可视化工具--ElasticSearch-Head的下载、安装与安装的方法。 概述         虽然UI比较古老,但它能在“数据浏览”中选择索引、类型、字段查看,个人用起来比下边“

    2024年01月18日
    浏览(64)
  • SpringBoot整合Elasticsearch(最新最全,高效安装到使用)

    为了避免使用的Elasticsearch版本和SpringBoot采用的版本不一致导致的问题,尽量使用一致的版本。下表是对应关系: 我的SpringBoot版本: 所以选择对应Elasticsearch版本为7.12.0。 Elasticsearch各版本下载 Elasticsearch7.12.0官网下载 下载上面链接的安装包 解压到任意目录 启动es /bin/elasti

    2024年02月07日
    浏览(37)
  • windows-下载adb包(无须安装)和配置adb环境及其简单使用

    阿里云盘下载地址 百度云盘下载地址 (提取码:mp3o) 1)点击win图标--点击“设置”图标--点击“系统”--点击“关于”--点击“系统信息”/“高级系统设置”--点击“环境变量”--选择\\\"系统变量\\\"里面的“path路径”后,点击【编辑】按钮--点击【新增】按钮--将adb文件的路径

    2024年02月08日
    浏览(61)
  • 安装lux:推荐一款网页视频下载工具。并简单使用。(win)

    lux是一个视频下载工具,它可以快速简单地下载各种视频网站的视频,支持多种操作系统和语言。 功能大概如下: 下载各种视频网站的视频,包括YouTube,Bilibili,Youku等(完整的支持列表见github页面) 支持多种操作系统和语言,包括Windows,macOS,Linux,Go等 支持多个输入和输

    2024年02月08日
    浏览(93)
  • Zookeeper3.5.7版本——Zookeeper可视化工具PrettyZoo的下载、安装与简单使用

    1.1、下载地址 下载地址 https://github.com/vran-dev/PrettyZoo/releases 1.2、下载步骤 点击下载地址后进入如下图页面,点击 【prettyZoo-win.msi 】下载安装包。 双击【prettyZoo-win.msi】安装包,弹出如下页面,点击【Next】。 选择安装目录位置,点击【OK】,如下图: 选择安装目录位置后,点

    2024年02月16日
    浏览(66)
  • SpringBoot 集成Elasticsearch简单八步

         Elasticsearch是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。  Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RE

    2024年02月17日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包