SpringBoot整合ElasticSearch(四)
程序员文章站
2024-03-05 15:35:07
...
ElasticsearchRepository
1.注入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.application.properties添加配置
添加基于 spring.data.elasticsearch的配置。
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
nodes不同于jest使用http通信,使用nodes端口。
"cluster_name" : "elasticsearch"名字信息
{ "name" : "MTwQmnF", "cluster_name" : "elasticsearch", "cluster_uuid" : "lLW-yQMmRPW7nUBpHOqe8Q", "version" : { "number" : "5.6.12", "build_hash" : "cfe3d9f", "build_date" : "2018-09-10T20:12:43.732Z", "build_snapshot" : false, "lucene_version" : "6.6.1" }, "tagline" : "You Know, for Search" }
- 使用ElasticsearchRepository 往elasticsearch中添加数据
自定义对象Book
package com.invi.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "invi", type = "book")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private Integer id;
private String bookName;
private String author;
}
lombok插件:
注入依赖
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency>
idea安装lombok插件 不然会语法报错
- @Data :注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
- @AllArgsConstructor 全参构造方法
- @NoArgsConstructor 无参构造方法
添加BookRepository
package com.invi.repository;
import com.invi.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
//自定义查询方法
public List<Book> findByBookNameLike(String bookName);
}
继承ElasticsearchRepository
ElasticsearchRepository 中定义的方法
<S extends T> S index(S var1); //存储序列 Iterable<T> search(QueryBuilder var1);//查询 Page<T> search(QueryBuilder var1, Pageable var2); Page<T> search(SearchQuery var1); Page<T> searchSimilar(T var1, String[] var2, Pageable var3); void refresh(); Class<T> getEntityClass();
测试
package com.invi;
import com.invi.bean.Article;
import com.invi.bean.Book;
import com.invi.repository.BookRepository;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.Build;
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;
import java.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot02ElasticApplicationTests {
@Autowired
BookRepository bookRepository;
@Test
public void save() {
Book book = new Book(1, "《西游记》", "吴承恩");
bookRepository.index(book);
}
@Test
public void testSearch() {
for (Book book : bookRepository.findByBookNameLike("西游记")) {
System.out.println(book);
}
}
}
index 用来保存信息
-
需要在 Book 类上标注 序列名indexName,类型type
@Document(indexName = "invi", type = "book")
BookRepository 支持 自定义命名方法: findByBookNameLike
测试都成功!
ElasticsearchTemplate
上一篇: 344. 反转字符串
下一篇: jquery插件开发
推荐阅读
-
SpringBoot整合ElasticSearch(三)
-
SpringBoot整合ElasticSearch(四)
-
SpringBoot整合ElasticSearch
-
SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法
-
springboot整合mybatis中的问题及出现的一些问题小结
-
Springboot整合第三方中间件
-
SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
-
Springboot整合Shiro之加盐MD5加密的方法
-
SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法
-
SpringBoot整合jersey的示例代码