1.9300:TCP
spring-data-elasticsearch:transport-api.jar
;springboot 版本不同, transport-api.jar 不同
,不能适配 es 版本 7.x 已经不建议使用,8 以后就要废弃 2.9200:HTTP
JestClient:非官方,更新慢 RestTemplate:模拟发 HTTP 请求,ES 很多操作需要自己封装,麻烦 HttpClient:同上 Elasticsearch-Rest-Client
:官方 RestClient,封装了 ES 操作,API 层次分明,上手简单 3.下面分别来介绍两种方式 :
a.Spring Data 框架介绍:
1.Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持 map-reduce 框架
和云计算数
据服务。 Spring Data
可以极大的简化 JPA(Elasticsearch„)
的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。除了 CRUD 外,还包括如分页、排序等一些常用的功能。 2.Spring Data 的官网 :
b.Spring Data Elasticsearch 介绍
1.Spring Data Elasticsearch 基于 spring data API 简化 Elasticsearch 操作,将原始操作Elasticsearch 的客户端 API 进行封装 。Spring Data 为 Elasticsearch 项目提供集成搜索引擎。Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻松地编写一个存储索引库数据访问层。 2.官方网站 :
c.Spring Data Elasticsearch 版本对比
d.框架集成测试:
1.创建maven项目: 2.修改pom文件,添加依赖:
<?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 http://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.3.6.RELEASE</ version>
< relativePath/>
</ parent>
< groupId> com.atguigu</ groupId>
< artifactId> es</ artifactId>
< version> 1.0</ version>
< properties>
< maven.compiler.source> 8</ maven.compiler.source>
< maven.compiler.target> 8</ maven.compiler.target>
</ properties>
< dependencies>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-data-elasticsearch</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-devtools</ artifactId>
< scope> runtime</ scope>
< optional> true</ optional>
</ 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-test</ artifactId>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
</ dependency>
</ dependencies>
</ project>
elasticsearch.host=127.0.0.1
elasticsearch.port=9200
logging.level.com.atguigu.es=debug
package com. atguigu. es ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@SpringBootApplication
public class SpringDataElasticSearchMainApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( SpringDataElasticSearchMainApplication . class , args) ;
}
}
package com. atguigu. es ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import lombok. ToString ;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Product {
private Long id;
private String title;
private String category;
private Double price;
private String images;
}
6.配置类:
ElasticsearchRestTemplate
是 spring-data-elasticsearch 项目中的一个类,和其他 spring 项目中的 template类似。在新版的 spring-data-elasticsearch 中,ElasticsearchRestTemplate 代替了原来的 ElasticsearchTemplate
。原因是 ElasticsearchTemplate 基于 TransportClient
,TransportClient 即将在 8.x 以后的版本中移除。所以,我们推荐使用 ElasticsearchRestTemplate
。 ElasticsearchRestTemplate
基 于 RestHighLevelClient
客户端的。需要自定义配置类,继承AbstractElasticsearchConfiguration,并实现 elasticsearchClient()抽象方法,创建 RestHighLevelClient 对象
。
package com. atguigu. es ;
import lombok. Data ;
import org. apache. http. HttpHost ;
import org. elasticsearch. client. RestClient ;
import org. elasticsearch. client. RestClientBuilder ;
import org. elasticsearch. client. RestHighLevelClient ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. context. annotation. Configuration ;
import
org. springframework. data. elasticsearch. config. AbstractElasticsearchConfiguration ;
@ConfigurationProperties ( prefix = "elasticsearch" )
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
private String host ;
private Integer port ;
@Override
public RestHighLevelClient elasticsearchClient ( ) {
RestClientBuilder builder = RestClient . builder ( new HttpHost ( host, port) ) ;
RestHighLevelClient restHighLevelClient = new
RestHighLevelClient ( builder) ;
return restHighLevelClient;
}
}
package com. atguigu. es ;
import
org. springframework. data. elasticsearch. repository. ElasticsearchRepository ;
import org. springframework. stereotype. Repository ;
@Repository
public interface ProductDao extends ElasticsearchRepository < Product , Long > {
}
package com. atguigu. es ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import lombok. ToString ;
import org. springframework. data. annotation. Id ;
import org. springframework. data. elasticsearch. annotations. Document ;
import org. springframework. data. elasticsearch. annotations. Field ;
import org. springframework. data. elasticsearch. annotations. FieldType ;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document ( indexName = "shopping" , shards = 3 , replicas = 1 )
public class Product {
@Id
private Long id;
@Field ( type = FieldType. Text , analyzer = "ik_max_word" )
private String title;
@Field ( type = FieldType. Keyword )
private String category;
@Field ( type = FieldType. Double )
private Double price;
@Field ( type = FieldType. Keyword , index = false )
private String images;
}
package com. atguigu. es ;
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. ElasticsearchRestTemplate ;
import org. springframework. test. context. junit4. SpringRunner ;
@RunWith ( SpringRunner . class )
@SpringBootTest
public class SpringDataESIndexTest {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void createIndex ( ) {
System . out. println ( "创建索引" ) ;
}
@Test
public void deleteIndex ( ) {
boolean flg = elasticsearchRestTemplate. deleteIndex ( Product . class ) ;
System . out. println ( "删除索引 = " + flg) ;
}
}
package com. atguigu. es ;
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. domain. Page ;
import org. springframework. data. domain. PageRequest ;
import org. springframework. data. domain. Sort ;
import org. springframework. test. context. junit4. SpringRunner ;
import java. util. ArrayList ;
import java. util. List ;
@RunWith ( SpringRunner . class )
@SpringBootTest
public class SpringDataESProductDaoTest {
@Autowired
private ProductDao productDao;
@Test
public void save ( ) {
Product product = new Product ( ) ;
product. setId ( 2L ) ;
product. setTitle ( "华为手机" ) ;
product. setCategory ( "手机" ) ;
product. setPrice ( 2999.0 ) ;
product. setImages ( "http://www.atguigu/hw.jpg" ) ;
productDao. save ( product) ;
}
@Test
public void update ( ) {
Product product = new Product ( ) ;
product. setId ( 1L ) ;
product. setTitle ( "小米 2 手机" ) ;
product. setCategory ( "手机" ) ;
product. setPrice ( 9999.0 ) ;
product. setImages ( "http://www.atguigu/xm.jpg" ) ;
productDao. save ( product) ;
}
@Test
public void findById ( ) {
Product product = productDao. findById ( 1L ) . get ( ) ;
System . out. println ( product) ;
}
@Test
public void findAll ( ) {
Iterable < Product > products = productDao. findAll ( ) ;
for ( Product product : products) {
System . out. println ( product) ;
}
}
@Test
public void delete ( ) {
Product product = new Product ( ) ;
product. setId ( 1L ) ;
productDao. delete ( product) ;
}
@Test
public void saveAll ( ) {
List < Product > productList = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
Product product = new Product ( ) ;
product. setId ( Long . valueOf ( i) ) ;
product. setTitle ( "[" + i+ "]小米手机" ) ;
product. setCategory ( "手机" ) ;
product. setPrice ( 1999.0 + i) ;
product. setImages ( "http://www.atguigu/xm.jpg" ) ;
productList. add ( product) ;
}
productDao. saveAll ( productList) ;
}
@Test
public void findByPageable ( ) {
Sort sort = Sort . by ( Sort. Direction . DESC , "id" ) ;
int currentPage= 0 ;
int pageSize = 5 ;
PageRequest pageRequest = PageRequest . of ( currentPage, pageSize, sort) ;
Page < Product > productPage = productDao. findAll ( pageRequest) ;
for ( Product Product : productPage. getContent ( ) ) {
System . out. println ( Product ) ;
}
}
}
package com. atguigu. es ;
import org. elasticsearch. index. query. QueryBuilders ;
import org. elasticsearch. index. query. TermQueryBuilder ;
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. domain. PageRequest ;
import org. springframework. test. context. junit4. SpringRunner ;
@RunWith ( SpringRunner . class )
@SpringBootTest
public class SpringDataESSearchTest {
@Autowired
private ProductDao productDao;
@Test
public void termQuery ( ) {
TermQueryBuilder termQueryBuilder = QueryBuilders . termQuery ( "title" , "
小米") ;
Iterable < Product > products = productDao. search ( termQueryBuilder) ;
for ( Product product : products) {
System . out. println ( product) ;
}
}
@Test
public void termQueryByPage ( ) {
int currentPage= 0 ;
int pageSize = 5 ;
PageRequest pageRequest = PageRequest . of ( currentPage, pageSize) ;
TermQueryBuilder termQueryBuilder = QueryBuilders . termQuery ( "title" , "
小米") ;
Iterable < Product > products =
productDao. search ( termQueryBuilder, pageRequest) ;
for ( Product product : products) {
System . out. println ( product) ;
}
}
}
b.案例2:
1.选用:Elasticsearch-Rest-Client操作 2.选择Elasticsearch-Rest-Client(elasticsearch-rest-high-level-client)
操作es 3.官方文档 :
b1.技术结构:
1.暂时先忽略MQ的过程: