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 {
    @Bean
    public 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 {

	@Autowired
	private ElasticsearchClient client;

	/*创建索引*/
	@Test
	void test01() throws Exception {

		//写法比RestHighLevelClient更加简洁
		CreateIndexResponse indexResponse = client.indices().create(c -> c.index("user"));

	}


	//查询数据
	@Test
	public void queryTest() throws IOException {
		GetIndexResponse getIndexResponse = client.indices().get(i -> i.index("user"));
		System.out.println(getIndexResponse);

	}

	//判断索引是否存在
	@Test
	public void existsTest() throws IOException {
		BooleanResponse booleanResponse = client.indices().exists(e -> e.index("user"));
		System.out.println(booleanResponse.value());
	}


	//删除索引
	@Test
	public void deleteTest() throws IOException {
		DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index("user"));
		System.out.println(deleteIndexResponse.acknowledged());
	}


	//插入document
	@Test
	public 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
	@Test
	public void updateDocumentTest() throws IOException {
		UpdateResponse<User> updateResponse = client.update(u -> u
						.index("user")
						.id("1")
						.doc(new User(1,"user2","123132131"))
				, User.class);
	}

	//查询document
	@Test
	public 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
	@Test
	public void deleteDocumentTest() throws IOException {
		DeleteResponse response = client.delete(d -> d
				.index("user")
				.id("1")
		);
		System.out.println(response);
	}


	//批量插入document
	@Test
	public 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);
	}


	//查询
/*	@Test
	public 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());
		}
	}*/

}
相关推荐
Cosolar18 分钟前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
yiangL_2 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
geovindu4 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
Elasticsearch5 小时前
安全负责人如何看待人工智能在安全运营中心(SOC)的应用
elasticsearch
skywalk81635 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾5 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
Elasticsearch6 小时前
Elastic 可观测性 9.6 新功能:APM 中的人工智能原生应用调查与大语言模型追踪、人工智能站点可靠性工程师,以及将告警作为数据
elasticsearch
听情歌落俗6 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力7 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
新*8 小时前
ES 7.17 三节点 Docker 集群的自动备份:从 NFS 被拦住到用 ACL 根治权限
运维·elasticsearch·docker·slm·快照备份