编程随笔 编程随笔
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • springboot

    • MyBatis Plus使用
    • springboot2引入swagger3
    • EasyCaptcha验证码存入redis的使用
    • 常用方法
    • Elasticsearch全文搜索
      • 在 Windows 上安装 Elasticsearch 作为服务
      • 添加依赖
      • 配置连接信息
      • 创建实体类
      • 创建Repository接口
      • 使用Repository
      • 分页
      • 高亮及搜索建议
      • DSL转springboot代码
    • canal同步mysql数据到es中
    • SpringSecurity使用
    • StringUtils 工具类使用
    • HTTP各种参数发送
    • EasyExcel之Excel导入导出
    • EasyExcel具体使用
    • FreeMarker 模板引擎入门
    • FreeMarker生成文件及WEB使用
    • TrueLicense 创建及安装证书
  • 服务器相关

  • 腾讯云cos对象操作

  • 后端
  • springboot
华总
2023-08-24
0
0
目录

Elasticsearch全文搜索原创

# 在 Windows 上安装 Elasticsearch 作为服务

Elasticsearch 可以作为服务安装在后台运行,或者在启动时自动启动,无需任何用户交互。elasticsearch-service.bat这可以通过文件夹中的脚本实现,bin\该脚本允许从命令行安装、删除、管理或配置服务并可能启动和停止服务。

D:\elasticsearch-7.17.9\bin>elasticsearch-service.bat

Usage: elasticsearch-service.bat install|remove|start|stop|manager [SERVICE_ID]
1
2
3

该脚本需要一个参数(要执行的命令),后跟一个指示服务 ID 的可选参数(在安装多个 Elasticsearch 服务时很有用)。

可用的命令是:

install 将 Elasticsearch 安装为服务
remove 删除已安装的 Elasticsearch 服务(如果启动则停止该服务)
start 启动 Elasticsearch 服务(如果已安装)
stop 停止 Elasticsearch 服务(如果已启动)
manager 启动用于管理已安装服务的 GUI

# 添加依赖

要使用 Elasticsearch,需要添加 Elasticsearch 的 Java 客户端依赖。可以在项目的 pom.xml 文件中添加以下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
1
2
3
4

# 配置连接信息

spring:
  elasticsearch:
    uris: http://localhost:9200
    socket-timeout: "10s"
    username: "user"
    password: "secret"
1
2
3
4
5
6

# 创建实体类

创建一个Java类来表示您要存储在Elasticsearch中的数据。此类应该使用@Document注解进行注释,并且每个字段都应该使用相应的注解进行注释,以指定字段名称、数据类型和Elasticsearch中的数据类型。

@Document(indexName = "post")
@Data
public class PostEsDTO implements Serializable {

    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    /**
     * id
     */
    @Id
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 创建时间
     */
    @Field(index = false, store = true, type = FieldType.Date, format = {}, pattern = DATE_TIME_PATTERN)
    private Date createTime;

    private static final long serialVersionUID = 1L;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 创建Repository接口

创建一个Repository接口,继承自ElasticsearchRepository,用于在Elasticsearch中存储和检索数据。Spring Data Elasticsearch会自动根据方法名称生成查询,您也可以使用@Query注解来指定自定义查询。

public interface PostEsDao extends ElasticsearchRepository<PostEsDTO, String> {
    List<PostEsDTO> findByUserId(Long userId);
}
1
2
3

# 使用Repository

在您的代码中,可以通过注入MyEntityRepository来使用它,并使用它的方法来存储和检索数据

@RestController
public class MyController {

    @Resource
    private PostEsDao postEsDao;

    @PostMapping("/entities")
    public MyEntity create(@RequestBody PostEsDTO postEsDTO) {
        return postEsDao.save(entity);
    }

    @GetMapping("/entities/{id}")
    public MyEntity findById(@PathVariable String id) {
        return postEsDao.findById(id).orElse(null);
    }

    @GetMapping("/entities")
    public List<MyEntity> findByName(@RequestParam String name) {
        return postEsDao.findByName(name);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

以上就是使用spring-boot-starter-data-elasticsearch库在Spring Boot应用程序中与Elasticsearch进行交互的基本步骤。通过这种方式,您可以轻松地将Elasticsearch集成到您的应用程序中,并使用Spring Data的强大功能进行查询和操作。

# 分页

@PostMapping("/list")
    public Result<Page<PostEsDTO>> findAll(@RequestBody PostEsDTO postEsDTO) {
        int current = postEsDTO.getCurrent();
        int size = postEsDTO.getSize();
        return Result.success(postEsDao.findAll(PageRequest.of(current,size))); 
    }
    // findAll(PageRequest.of(current,size))为ElasticsearchRepositorynei'zhi
1
2
3
4
5
6
7

# 高亮及搜索建议

# DSL转springboot代码

#后端#springboot#Elasticsearch
上次更新: 2023/12/09 16:19:24
常用方法
canal同步mysql数据到es中

← 常用方法 canal同步mysql数据到es中→

最近更新
01
element-plus多文件手动上传 原创
11-03
02
TrueLicense 创建及安装证书 原创
10-25
03
手动修改迅捷配置 原创
09-03
04
安装 acme.sh 原创
08-29
05
zabbix部署 原创
08-20
更多文章>
Copyright © 2023-2024 liyao52033
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式