elasticsearch权威指南:https://es.xiaoleilu.com/010_Intro/00_README.html
一、启动服务
二、maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-esclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-esclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--集合工具包-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
java代码
官方文档参考:https://docs.spring.io/spring-data/elasticsearch/docs/3.2.3.RELEASE/reference/html/#reference
application.properties
#填写服务器elasticsearch.yml中的custer_name的值,默认为elasticsearch
spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes= 127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true
Commodity.java
package com.example.demoesclient;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
@Data
@Document(indexName = "commodityIndex")
public class Commodity implements Serializable {
@Id
private String id;
private String name;
}
sprignboot的start集成方式有两种方式集成分别是repository方式和elasticsearchTemplate方式
方式一
CommodityRepository.java
package com.example.demoesclient;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommodityRepository extends ElasticsearchRepository<Commodity, String> {
}
package com.example.demoesclient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchRepositoryTests {
@Autowired
CommodityRepository commodityRepository;
@Test
public void contextLoads() {
System.out.println(commodityRepository.count());
}
@Test
public void testInsert() {
Commodity commodity = new Commodity();
commodity.setId("1001");
commodity.setName("商品1");
commodityRepository.save(commodity);
commodity = new Commodity();
commodity.setId("1002");
commodity.setName("商品2");
commodityRepository.save(commodity);
}
@Test
public void testDelete() {
Commodity commodity = new Commodity();
commodityRepository.delete(commodity);
}
@Test
public void testGetAll() {
Iterable<Commodity> iterable = commodityRepository.findAll();
iterable.forEach(e->System.out.println(e.toString()));
}
@Test
public void testGetByName() {
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", "商品");
Iterable<Commodity> iterable = commodityRepository.search(matchQueryBuilder);
}
}
方式二
package com.example.demoesclient;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchTemplateTests {
@Autowired
public ElasticsearchTemplate elasticsearchTemplate;
@Test
public void testInsert() {
Commodity commodity = new Commodity();
commodity.setId("2001");
commodity.setName("商品a");
IndexQuery indexQuery = new IndexQueryBuilder().withObject(commodity).build();
elasticsearchTemplate.index(indexQuery);
}
@Test
public void testQuery() {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("name", "商品a"))
.build();
List<Commodity> list = elasticsearchTemplate.queryForList(searchQuery, Commodity.class);
}
}
思考
从elasticsearch得知在7.x版本中已经默认支持client为restHighLevelClient,而不是transportClient,并且在8.x版本中将舍弃transportClient
不过因为目前springboot版本集成的为6.x的elasticsearch版本,jar包中并没有RestHighLevelClientd的源文件,所以仍然使用的transportClient,不过从下方图片我推测由于使用了
条件注解
和官方的说明,所以我们可以手动加入RestHighLevelClient.java对应的jar包得以使用这个更推荐的客户端,不过在后续springboot持续更新的start包中应该会默认使用推荐的客户端