springMvc集成ElasticSearch2.3.5
上两篇文章我们介绍了Elasticsearch以及其相关中间件的部署和基本操作,以及java-api连接Es并创建索引。现在简单介绍一下如何集成ElasticSearch。
一、配置文件
spring-elasticsearch.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch
https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:repositories base-package="com.grady.archive.elasticSearch.repository" elasticsearch-template-ref="elasticsearchTemplate"/>
<elasticsearch:transport-client id="client" cluster-nodes="172.16.18.177:9300" cluster-name="elasticsearch" />
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>
</beans>
framework-init.xml
<import resource="spring-elasticsearch.xml"/>
二、将jar包加入到项目中
这些jar包在Es的lib目录下都可以找到。
三、建包以及相关的类
dao层
public interface ArchiveContentRepository extends ElasticsearchRepository<ArchiveContentVo,String> {
//根据正文 内容查询
List<ArchiveContentVo> findArchiveContentVoByArchiveContext(String archiveContext);
List<ArchiveContentVo> findAllByArchiveContextAndArchiveStateAndBusiOrgIdAndIsDel(String archiveContext,String archiveState,String busiOrgId,String isDel);
List<ArchiveContentVo> findAllByArchiveContextOrderByBoxYear(String archiveContext);
@Override
List<ArchiveContentVo> findAll();
}
service层
@Repository("archiveContentService")
public class ArchiveContentService {
@Autowired
private ArchiveContentRepository repository;
public ArchiveContentVo createIndex(ArchiveContentVo contentVo){
ArchiveContentVo archiveContentVo = repository.save(contentVo);
return archiveContentVo;
}
public List<ArchiveContentVo> findListByContext(String archiveContext){
List<ArchiveContentVo> archiveContentVoByArchiveContext = repository.findArchiveContentVoByArchiveContext(archiveContext);
return archiveContentVoByArchiveContext;
}
public List<ArchiveContentVo> findAllByArchiveContext(String archiveContext,String archiveState,String busiOrgId,String isDel){
return repository.findAllByArchiveContextAndArchiveStateAndBusiOrgIdAndIsDel(archiveContext,archiveState,busiOrgId,isDel);
}
public List<ArchiveContentVo> findAll(){
return repository.findAll();
}
}
总结
接下来的就是调用的过程了,具体的,更多的使用各位可以去官网看相关的文档,本次就介绍到这里了。