120.【ElastiSearch】(八)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 120.【ElastiSearch】

(十)、集成SpringBoot

官网: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.6/index.html

1.ElasticSearch依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.6.2</version>
</dependency>

2.初始化ElasticSearch

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")));
client.close();

3.分析这个类的方法

(1).创建一个空项目

(2).在空项目中创建SpringBOOT模块

选择NoSQL的 es依赖

(3).配置JDK版本

因为我们是新建的空项目所以要用 JDK8 版本

4.配置基本的项目

问题: 一定要保证我们的导入的依赖和我们的E下载ES版本一致

我们本地的版本是 7.6.2.

(1).解决版本不一致问题

如果我们在构想项目的时候自己选用默认的版本我们就需要 自定义版本依赖。否则不需要自定义版本的依赖了。

<!--    自己定义es版本依赖,保证和本地一致    -->
        <elasticsearch.version>7.6.2</elasticsearch.version>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jsxs</groupId>
    <artifactId>Jsxs-es-api-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Jsxs-es-api-01</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <!--    自己定义es版本依赖,保证和本地一致    -->
        <elasticsearch.version>7.6.2</elasticsearch.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.7.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

(2).配置配置类文件
package com.jsxs.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;
/**
 * @Author Jsxs
 * @Date 2023/6/27 11:21
 * @PackageName:com.jsxs.config
 * @ClassName: ElasticSearchClientConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
public class ElasticSearchClientConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}


相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
索引
120.【ElastiSearch】(十)
120.【ElastiSearch】
59 1
|
自然语言处理 索引
120.【ElastiSearch】(七)
120.【ElastiSearch】
68 0
|
存储 监控 关系型数据库
120.【ElastiSearch】(三)
120.【ElastiSearch】
97 0
|
自然语言处理 算法 数据库
120.【ElastiSearch】(四)
120.【ElastiSearch】
55 0
|
SQL JavaScript 数据可视化
120.【ElastiSearch】(二)
120.【ElastiSearch】
56 0
|
JSON Java 数据格式
120.【ElastiSearch】(六)
120.【ElastiSearch】
59 0
|
搜索推荐 Java 大数据
120.【ElastiSearch】(一)
120.【ElastiSearch】
67 0
|
5月前
|
存储 自然语言处理 搜索推荐
【技术解析 | 实践】Havenask分析器
本次分享内容为Havenask的分析器,本次课程主要分为3部分内容(分析器介绍、解释分析器主要配置、实战演示),希望本次通过分享帮助大家更好了解和使用Havenask。
52248 3
【技术解析 | 实践】Havenask分析器
|
4月前
|
网络安全 数据库 Python
常用百宝箱——日志处理
常用百宝箱——日志处理
|
API 索引
120.【ElastiSearch】(九)
120.【ElastiSearch】
50 0