当前位置: 首页 > news >正文

Elasticsearchr入门

首先在官网下载elasticsearch8.9版本,以及8.9版本的kibana。

解压,点击es8.9bin目录下的elasticsearch.bat文件启动es

如图所示即为成功。 

 启动之后打开idea,添加依赖

        <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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>8.9.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.10</version></dependency>

之后配置配置文件

@Configuration
public class ElasticSearchConfig {@Beanpublic ElasticsearchClient elasticsearchClient(){RestClient client = RestClient.builder(new HttpHost("localhost", 9200,"http")).build();ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());return new ElasticsearchClient(transport);}
}

这时候就已经可以使用了基本使用操作如下代码块


@SpringBootTest
class SpringDataJpaApplicationTests {@Autowiredprivate ElasticsearchClient client;/*创建索引*/@Testvoid test01() throws Exception {//写法比RestHighLevelClient更加简洁CreateIndexResponse indexResponse = client.indices().create(c -> c.index("user"));}//查询数据@Testpublic void queryTest() throws IOException {GetIndexResponse getIndexResponse = client.indices().get(i -> i.index("user"));System.out.println(getIndexResponse);}//判断索引是否存在@Testpublic void existsTest() throws IOException {BooleanResponse booleanResponse = client.indices().exists(e -> e.index("user"));System.out.println(booleanResponse.value());}//删除索引@Testpublic void deleteTest() throws IOException {DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index("user"));System.out.println(deleteIndexResponse.acknowledged());}//插入document@Testpublic void addDocumentTest() throws IOException {User user = new User(1, "张三","123123123");IndexResponse indexResponse = client.index(i -> i.index("user")//设置id.id("1")//传入user对象.document(user));}//更新document@Testpublic void updateDocumentTest() throws IOException {UpdateResponse<User> updateResponse = client.update(u -> u.index("user").id("1").doc(new User(1,"user2","123132131")), User.class);}//查询document@Testpublic void queryDocumentTest() throws IOException {GetResponse<User> response = client.get(g -> g.index("user").id("1"), User.class);System.out.println(response);System.out.println(response.source());}//删除document@Testpublic void deleteDocumentTest() throws IOException {DeleteResponse response = client.delete(d -> d.index("user").id("1"));System.out.println(response);}//批量插入document@Testpublic void bulkTest() throws IOException {List<User> users=new CopyOnWriteArrayList<>();users.add(new User(1,"张1","1233"));users.add(new User(2,"张2","1234"));users.add(new User(3,"张3","1235"));users.add(new User(4,"张4","1236"));users.add(new User(5,"张5","1237"));List< BulkOperation> bulkOperationCopyOnWriteArrayList =new CopyOnWriteArrayList<>();//遍历插入bulk中users.stream().forEach(u->{bulkOperationCopyOnWriteArrayList.add(BulkOperation.of(o ->o.index(i->i.document(u))));});System.out.println(bulkOperationCopyOnWriteArrayList);BulkResponse response=client.bulk(b->b.index("user").operations(bulkOperationCopyOnWriteArrayList));System.out.println(response);}//查询
/*	@Testpublic void searchTest() throws IOException {SearchResponse<User> search = client.search(s -> s.index("user")//查询name字段包含hello的document(不使用分词器精确查找).query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("hello"))))//分页查询,从第0页开始查询3个document.from(0).size(3)//按age降序排序.sort(f->f.field(o->o.field("age").order(SortOrder.Desc))),User.class);for (Hit<User> hit : search.hits().hits()) {System.out.println(hit.source());}}*/}

http://www.lryc.cn/news/110623.html

相关文章:

  • 【ARM】imx6ul移植kernel记录,恩智浦github提供的最新kernel(2023年7月31)
  • eeglab(自用)
  • Dockerfile构建Tomcat镜像(源码)
  • Frida Error: getPackageInfoNoCheck(): has more than one overload的解决方法
  • flutter开发实战-RawKeyboardListener监听键盘事件及keycode。
  • Temu、希音们全托管引争议,跨境电商应变“工贸一体化”
  • 某科技公司提前批测试岗
  • 一次redis缓存不均衡优化经验
  • npm发布包
  • Qt5.13引入QtWebApp的模块后报错: error C2440: “reinterpret_cast”: 无法从“int”转换为“quintptr”
  • 软件为什么要进行性能压力测试?
  • 阻塞队列BlockingQueue详解
  • pygame贪吃蛇游戏
  • Mac系统下使用远程桌面连接Windows系统
  • 使用 OpenCV 和深度学习对黑白图像进行着色
  • 从价值的角度看,为何 POSE 通证值得长期看好
  • pytorch的CrossEntropyLoss交叉熵损失函数默认reduction是平均值
  • OKR管理策略:为开发团队注入动力
  • C++二叉搜索树剖析
  • 升级你的GitHub终端认证方式:从密码到令牌
  • 【力扣】链表题目总结
  • Thunar配置自定义动作
  • Python 开发工具 Pycharm —— 使用技巧Lv.3
  • 51单片机(普中HC6800-EM3 V3.0)实验例程软件分析 实验三 LED流水灯
  • 深度学习与计算机相结合:直播实时美颜SDK的创新之路
  • Unity寻找子物体的方法
  • 车载软件架构 —— 车载软件安全启动关键技术解读
  • 2023-08-05——JVM Method Area(方法区)
  • 【前端知识】React 基础巩固(四十六)——自定义Hook的应用
  • Swish - Mac 触控板手势窗口管理工具[macOS]