springMvc集成ElasticSearch2.3.5

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目录下都可以找到
这些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();
    }
}

总结

接下来的就是调用的过程了,具体的,更多的使用各位可以去官网看相关的文档,本次就介绍到这里了。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉霍拉卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值