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 );
相关推荐
Geoking.21 小时前
后端Long型数据传到前端js后精度丢失的问题(前后端传输踩坑指南)
java·前端·javascript·后端
lizhongxuan21 小时前
深入 Codex 沙盒
后端
架构谨制@涛哥21 小时前
架构谨制:重新定义软件从业者的本质
后端·系统架构·软件构建
ん贤21 小时前
Go GC 非玄学,而是 CPU 和内存的权衡
开发语言·后端·golang·性能调优·gc
码事漫谈1 天前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
铁东博客1 天前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
oak隔壁找我1 天前
SpringBoot中MyBatis的Mapper的原理
后端
oak隔壁找我1 天前
Spring Boot 自动配置(Auto-configuration)的核心原理
后端
oak隔壁找我1 天前
Java的JAR包
后端
GetcharZp1 天前
告别 TCP 握手延迟!让你的 Go 服务瞬间拥抱 HTTP/3 时代
后端