SpringBoot整合Elasticsearch实现全文搜索引擎
优采云 发布时间: 2021-08-22 18:21
SpringBoot整合Elasticsearch实现全文搜索引擎
Spring Boot 集成 Elasticsearch 实现全文搜索引擎案例分析
更新时间:2017-01-02 10:07:10 投稿:mrr
ElasticSearch 作为基于 Lucene 的搜索服务器,可以部署为独立服务或登录到 Web 应用程序。 SpringBoot 作为 Spring 家族的新框架,使得使用 SpringBoot 开发 Spring 应用变得非常简单。在这种情况下,我们将引入 Spring Boot 来集成 Elasticsearch 来实现全文搜索引擎
简单来说,ElasticSearch(简称ES)是一个搜索引擎,是一个针对结构化数据的分布式搜索引擎。 Elastic Search 是一个开源、分布式、实时搜索和分析引擎。 Spring Boot 提供了 Elasticsearch 的基本配置以及基于它的 Spring Data Elasticsearch 抽象。 Spring Boot 提供了一个 spring-boot-starter-data-elasticsearch'StarterPOM' 来采集依赖。
引入spring-boot-starter-data-elasticsearch依赖,在pom.xml配置文件中添加如下内容(基于上一章“Spring Boot框架”中的pom.xml文件):
org.springframework.boot
spring-boot-starter-data-elasticsearch
您可以像其他 Spring bean 一样注入自动配置的 ElasticsearchTemplate 或 Elasticsearch 客户端实例。默认情况下,实例会尝试连接本地内存服务器(Elasticsearch 项目中的一个 NodeClient),但可以通过将 spring.data.elasticsearch.clusterNodes 设置为逗号分隔的 host:port 列表来切换到远程服务器(例如,TransportClient)。
@Component
public class MyBean {
private ElasticsearchTemplate template;
@Autowired
public MyBean(ElasticsearchTemplate template) {
this.template = template;
}
// ...
}
如果您添加自己的 ElasticsearchTemplate 类型的 @Bean,它将替换默认的。
应用集成 ElasticSearch 案例
新建elasticsearch.properties配置文件,添加如下配置内容:
elasticsearch.host=localhost
elasticsearch.port=9300
ElasticSearch配置,读取elasticsearch.properties配置文件信息,具体代码如下:
@Configuration@PropertySource(value = "classpath:elasticsearch.properties")
@EnableElasticsearchRepositories(basePackages = "co.paan.repository")
public class ElasticsearchConfiguration {
@Resource
private Environment environment;
@Bean
public Client client() {
TransportClient client = new TransportClient();
TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port")));
client.addTransportAddress(address);
return client;
}
@Beanpublic ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
}
两个实体类,具体代码如下:
@Document(indexName = "post", type = "post", shards = 1, replicas = 0)
public class Post {
@Id
private String id;
private String title;
@Field(type= FieldType.Nested)
private List tags;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List getTags() {
return tags;
}
public void setTags(List tags) {
this.tags = tags;
}
}
public class Tag {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
数据源继承ElasticsearchRepository类,封装接口代码如下:
public interface PostRepository extends ElasticsearchRepository{
Page findByTagsName(String name, Pageable pageable);
}
数据服务接口和实现类,代码如下:
public interface PostService {
Post save(Post post);
Post findOne(String id);
Iterable findAll();
Page findByTagsName(String tagName, PageRequest pageRequest);
}
@Servicepublic class PostServiceImpl implements PostService{
@Autowired
private PostRepository postRepository;
@Override
public Post save(Post post) {
postRepository.save(post);
return post;
}
@Overridepublic Post findOne(String id) {
return postRepository.findOne(id);
}
@Overridepublic Iterable findAll() {
return postRepository.findAll();
}
@Overridepublic Page findByTagsName(String tagName, PageRequest pageRequest) {
return postRepository.findByTagsName(tagName, pageRequest);
}
}
测试代码如下:
@Test
public void testFindByTagsName() throws Exception {
Tag tag = new Tag();
tag.setId("1");
tag.setName("tech");
Tag tag2 = new Tag();
tag2.setId("2");
tag2.setName("elasticsearch");
Post post = new Post();
post.setId("1");
post.setTitle("Bigining with spring boot application and elasticsearch");
post.setTags(Arrays.asList(tag, tag2));
postService.save(post);
Post post2 = new Post();
post2.setId("1");
post2.setTitle("Bigining with spring boot application");
post2.setTags(Arrays.asList(tag));
postService.save(post);
Page posts = postService.findByTagsName("tech", new PageRequest(0,10));
Page posts2 = postService.findByTagsName("tech", new PageRequest(0,10));
Page posts3 = postService.findByTagsName("maz", new PageRequest(0,10));
assertThat(posts.getTotalElements(), is(1L));
assertThat(posts2.getTotalElements(), is(1L));
assertThat(posts3.getTotalElements(), is(0L));
}
总结
以上就是小编介绍的Spring Boot集成Elasticsearch实现全文搜索引擎的案例分析。我希望它会对你有所帮助。如果有什么问题,请给我留言,小编会及时回复您。非常感谢您对Script Home网站的支持!