Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。
但是,Lucene只是一个库。想要使用它,你必须使用Java来作为开发语言并将其直接集成到你的应用中,更糟糕的是,Lucene非常复杂,你需要深入了解检索的相关知识来理解它是如何工作的。
Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API
来隐藏Lucene的复杂性,从而让全文搜索变得简单。
不过,Elasticsearch不仅仅是Lucene和全文搜索,我们还能这样去描述它:
分布式的实时文件存储,每个字段都被索引并可被搜索
分布式的实时分析搜索引擎
可以扩展到上百台服务器,处理PB级结构化或非结构化数据
安装
Java
Elasticsearch
Marvel(可选)
head 等其他插件(可选)
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文件):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
可以像其他Spring beans那样注入一个自动配置的ElasticsearchTemplate或Elasticsearch客户端实例。默认情况下,该实例将尝试连接到一个本地内存服务器(在Elasticsearch项目中的一个NodeClient),但可以通过设置spring.data.elasticsearch.clusterNodes为一个以逗号分割的host:port列表来将其切换到一个远程服务器(比如,TransportClient)。
@Componentpublic 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 { @Resourceprivate Environment environment;@Beanpublic 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 {@Idprivate String id; private String title;@Field(type= FieldType.Nested)private List<Tag> 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<Tag> getTags() { return tags;} public void setTags(List<Tag> 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<Post, String>{ Page<Post> findByTagsName(String name, Pageable pageable); }
数据服务接口及实现类,代码如下:
public interface PostService { Post save(Post post); Post findOne(String id); Iterable<Post> findAll(); Page<Post> findByTagsName(String tagName, PageRequest pageRequest);}@Servicepublic class PostServiceImpl implements PostService{ @Autowiredprivate PostRepository postRepository;@Overridepublic Post save(Post post) { postRepository.save(post); return post; } @Overridepublic Post findOne(String id) { return postRepository.findOne(id); } @Overridepublic Iterable<Post> findAll() { return postRepository.findAll(); } @Overridepublic Page<Post> findByTagsName(String tagName, PageRequest pageRequest) { return postRepository.findByTagsName(tagName, pageRequest); } }
测试代码如下:
@Testpublic 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<Post> posts = postService.findByTagsName("tech", new PageRequest(0,10)); Page<Post> posts2 = postService.findByTagsName("tech", new PageRequest(0,10)); Page<Post> posts3 = postService.findByTagsName("maz", new PageRequest(0,10)); assertThat(posts.getTotalElements(), is(1L)); assertThat(posts2.getTotalElements(), is(1L)); assertThat(posts3.getTotalElements(), is(0L)); }
本文暂时没有评论,来添加一个吧(●'◡'●)