环境:springboot2.3.10.RELEASE + ElasticSearch7.8.0
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency>
spring: elasticsearch: rest: uris: - http://localhost:9201---logging: level: com.pack: debug org.springframework.data.elasticsearch.core: debug
@Document(createIndex = true,详相关 indexName = "products", shards = 3, replicas = 1)public class Product { @Id private Long id ; @Field(analyzer = "ik_max_word", type = FieldType.Text) 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 ; @Override public String toString() { return "Product [id=" + id + ", title=" + title + ", category=" + category + ", price=" + price + ", images=" + images + "]"; }}
这里我们只需要继承ElasticsearchRepository即可,是解及不是和data-jpa一样一样的的。
public interface ProductRepository extends ElasticsearchRepository<Product,使用 Long> { }
继承ElasticsearchRepository后 我们也可以像data-jpa一样使用findBy*等语法来写相关查询方法。
图片
Repository中也支持@Query注解的方式自定义查询字符串。
public interface ProductRepository extends ElasticsearchRepository<Product,详相关 Long> { List<Product> findByTitle(String title) ; @Query("{ \"fuzzy\": { \"title\": \"?0\"}}") Page<Product> findByTitle(String sex,Pageable pageable); // 自定义查询 @Query("{ \"match\": { \"category\": \"?0\"}}") Page<Product> findByCategory(String category,Pageable pageable); // 高亮设置 @Highlight(fields = { @HighlightField(name = "title"), @HighlightField(name = "category")}) List<SearchHit<Product>> findByTitleOrCategory(String title, String category,Pageable pageable) ;}
除了使用Repository方式,我们还可以使用ElasticsearchRestTemplate的解及方式请求服务。如下测试
@Resourceprivate ProductRepository productRepository ;@Resourceprivate ElasticsearchRestTemplate elasticTemplate ; @Testpublic void testCreate() { Product product = new Product() ; product.setId(3L) ; product.setCategory("配件") ; product.setPrice(299.5d) ; product.setImages("http://www.pack.com/memory.jpg") ; product.setTitle("很牛逼的使用内存条") ; productRepository.save(product) ;} @Testpublic void testQuery() { Product product = productRepository.findById(1L).orElse(null) ; System.out.println(product) ;} @Testpublic void testFindAll() { Pageable pageable = PageRequest.of(1, 2) ; Page<Product> page = productRepository.findAll(pageable) ; System.out.println(page.getTotalPages() + "\n" + page.getContent()) ;} @Testpublic void testTermSearch() { for (Product p : productRepository.findByTitle("Java从入门到精通")) { System.out.println(p) ; }} @Testpublic void testFindByTitle() { Pageable pageable = PageRequest.of(0, 2) ; Page<Product> page = productRepository.findByTitle("Java", pageable) ; System.out.println(page.getTotalPages() + "\n" + page.getContent()) ;} @Testpublic void testFindByCategory() { Pageable pageable = PageRequest.of(0, 2) ; Page<Product> page = productRepository.findByCategory("书籍", pageable) ; System.out.println(page.getTotalPages() + "\n" + page.getContent()) ;} @Testpublic void testCriteriaQuery() { Criteria criteria = new Criteria("price").greaterThan(50).lessThan(80); Query query = new CriteriaQuery(criteria); SearchHits<Product> hits = elasticTemplate.search(query, Product.class, IndexCoordinates.of("products")) ; for (SearchHit<Product> hit : hits) { System.out.println(hit) ; }} @Testpublic void testStringQuery() { Query query = new StringQuery("{ \"match\": { \"category\": { \"query\": \"配件\" } } } "); SearchHits<Product> hits = elasticTemplate.search(query, Product.class); for (SearchHit<Product> hit : hits) { System.out.println(hit) ; }}@Testpublic void testStringQueryFuzzy() { Query query = new StringQuery("{ \"fuzzy\":{ \"title\":{ \"value\":\"Java\"}} }"); HighlightQuery highlightQuery = null ; HighlightBuilder highBuilder = new HighlightBuilder().preTags("<font color='red'>").postTags("</font>").field("title") ; highlightQuery = new HighlightQuery(highBuilder) ; query.setHighlightQuery(highlightQuery) ; SearchHits<Product> hits = elasticTemplate.search(query, Product.class); for (SearchHit<Product> hit : hits) { System.out.println(hit + "\n" + hit.getHighlightField("title")) ; }}
在启动服务时会自动地为我们创建索引。
我们可以安装Chrome插件 ElasticSearch Head非常方便地查看es的整合状态及索引信息。
图片
这里我是详相关搭建的集群。
解及图片
(责任编辑:休闲)
21999元!雷神上架新款黑武士台式机 搭载RTX 4080
九兴控股(01836.HK)发布公告:授出1969.5万份购股权