搜索引擎优化高级编程(SpringBoot整合Elasticsearch实现全文)
优采云 发布时间: 2021-10-06 16:13搜索引擎优化高级编程(SpringBoot整合Elasticsearch实现全文)
Elasticsearch作为基于Lucene的搜索服务器,可以作为独立服务部署或签入web应用程序。作为spring系列的一个新框架,springboot使使用springboot开发spring应用程序变得非常容易。在本例中,我们引入spring boot来集成elasticsearch以实现全文搜索引擎
简言之,弹性搜索(ES)是一种搜索引擎和分布式搜索引擎,用于结构化数据。弹性搜索是一个开源、分布式、实时的搜索和分析引擎。SpringBoot为elasticsearch和SpringDataElasticSearch提供的抽象提供了基本配置。Spring boot提供Spring boot starter数据弹性搜索“starter POM”,用于聚合依赖项
介绍了spring boot starter数据elasticsearch依赖项,并将以下内容添加到pom.xml配置文件中(基于前一章“spring boot构造框架”中的pom.xml文件):
org.springframework.bootspring-boot-starter-data-elasticsearch
您可以像其他Springbean一样注入自动配置的elasticsearchtemplate或elasticsearch客户端实例。默认情况下,实例将尝试连接到本地内存服务器(elasticsearch项目中的nodeclient),但可以通过将spring.data.elasticsearch.clusternodes设置为逗号分隔的主机:端口列表将其切换到远程服务器(如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.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实现全文搜索引擎案例分析的细节。请多关注其他相关文章