一、环境搭建
1.Maven依赖
< parent> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-parent</ artifactId> < version> 2.3.6.RELEASE</ version> < relativePath/> </ parent> < 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>
2.配置文件的编写
elasticsearch.host= 127.0 .0.1
elasticsearch.port= 9200
logging.level.com.maoyan.es= debug
3.书写一个普通的启动类
@SpringBootApplication
public class SpringDataElasticSearchMainApplication { public static void main ( String [ ] args) { SpringApplication . run ( SpringDataElasticSearchMainApplication . class , args) ; }
}
4.书写ES的配置类
@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; }
}
二、相关准备
1.准备一个实体类(类名及索引,属性及文档字段)
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document ( indexName = "product" , 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;
}
2.书写操作文档的dao层
@Repository
public interface ProductDao extends ElasticsearchRepository < Product , Long > {
}
三、操作代码展示
1.对索引的操作
@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) ; }
}
2.对文档的操作
@RunWith ( SpringRunner . class )
@SpringBootTest
public class SpringDataESProductDaoTest { @Autowired private ProductDao productDao; @Test public void save ( ) { Product product = new Product ( ) ; product. setId ( 1L ) ; 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 ) ; } }
}
3.文档搜索
@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) ; } }
}