Elastic Search 安装使用

1.安装

官方下载地址:www.elastic.co/cn/download...

解压文件,然后创建一个es 用户,给用户文件权限

bash 复制代码
chown -R es /opt/elasticsearch-9.2.2

修改配置文件,添加ES_JAVA_HOME

bash 复制代码
vim /etc/profile

export ES_JAVA_HOME="/opt/elasticsearch-9.2.2/jdk"

souce /etc/profile

切换用户

复制代码
su es

启动es

bash 复制代码
./bin/elasticsearch -d

//设置密码 用户默认是:elastic

/opt/elasticsearch-9.2.2/bin/elasticsearch-reset-password -u elastic -i

浏览器可以使用插件 Multi Elasticsearch Heads,添加地址,测试连接

2.使用

2.1 创建索引

text和keyword的区别

text keyword
分词
存储 多个token 字符串
模糊搜索 支持 不支持
排序 不支持 支持
聚合 不支持 支持
less 复制代码
CreateIndexRequest request = CreateIndexRequest.of(c -> c .index("users") // 索引名称 
    .mappings(m -> m .properties("name", p -> p.text(t -> t)) //text类型 
    .properties("age", p -> p.integer(i -> i)) //数字类型 
    .properties("email", p -> p.keyword(k -> k)) // keyword类型 ) ); 
CreateIndexResponse response = client.indices().create(request);

2.2 增加

less 复制代码
IndexResponse response = client.index(i -> i .index("users") .id("1") 
    .document(Map.of( "name", "张三", "age", 18, "email", "zhangsan@xxx.com" )) );

2.3 更新

less 复制代码
UpdateResponse<Map> updateResponse = client.update(u -> u 
    .index("users") 
    .id("1") .doc(Map.of("age", 29)), Map.class );

2.4 删除

ini 复制代码
DeleteResponse response = client.delete(d -> d .index("users") .id("1") );

2.5 查询

条件查询
less 复制代码
SearchResponse<Map> search = client.search(s -> s 
    .index("users") 
    .query(q -> q 
    .match(m -> m .field("name") .query("张三") ) ), Map.class );
range 范围查询
less 复制代码
SearchResponse<Map> rangeSearch = client.search(s -> s 
    .index("users") 
    .from(0).size(10) 
    .query(q -> q 
    .range(r -> r .field("age") .gte(JsonData.of(20)) .lte(JsonData.of(30)) ) ),
    Map.class );
bool 多条件查询

must ==> and

must_not ==> 都不满足

should ==> or

filter ==> 满足条件

match ==> 全文索引

less 复制代码
SearchResponse<Map> boolSearch = client.search(s -> s 
    .index("users") 
    .query(q -> q 
    .bool(b -> b 
    .must(m -> m.match(ma -> ma.field("name").query("张三"))) 
    .filter(f -> f.range(r -> r.field("age").gte(20))) ) ), Map.class );
相关推荐
小Ti客栈6 小时前
Spring Boot 集成 Springdoc-OpenAPI 与 Knife4j实现接口文档与可视化调试
java·spring boot·后端
Ai拆代码的曹操7 小时前
Spring 事务 REQUIRES_NEW 嵌套调用:连接池翻倍的秘密
java·后端·spring
IT_陈寒9 小时前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱9 小时前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端
Wang's Blog9 小时前
Go-Zero项目开发24: 基于Bitmap实现群聊消息已读未读
开发语言·后端·golang
Conan在掘金10 小时前
鸿蒙报错速查:struct 里嵌 namespace 声明就炸,根因 + 真解法
后端
东方小月10 小时前
从零开发一个 Coding Agent(三):EventStream 事件流通道设计与实现
前端·人工智能·后端
卫子miao10 小时前
如何评价当前大语言模型的记忆机制?
后端·架构
神奇小汤圆10 小时前
为什么 AQS 成为 Java 并发的基石?
后端
星栈11 小时前
MCP 从 stdio 迁到 SSE,踩了 5 个传输层坑
人工智能·后端·架构