Spring Data Elasticsearch加Elasticsearch服务实现全文搜索

本文首发于我的博客:https://blog.wellcoding.win

Elasticsearch是一个开源的基于Lucene的搜索服务器,Elasticsearch是使用Java语言开发的,它提供了分布式多用户全文搜索引擎,提供基于RESTful 的API接口。Elasticsearch还提供了一个Java的Client,以及相应的API。
Spring Data中提供了相应的模块Spring Data ElasticsearchSpring Data Elasticsearch模块就是基于这个Client实现的。不过不知道出于什么原因,最新版的Spring Data Elasticsearch 2.1.3.RELEASE也仅仅支持到Elasticsearch 2.4版本。本文将使用Spring Data Elasticsearch 2.1.3.RELEASE

首先添加Maven依赖:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

创建一个基础接口:

package win.wellcoding.elasticsearch;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.io.Serializable;

public interface BaseSearchRepository<E, ID extends Serializable>
    extends ElasticsearchRepository<E, ID>, PagingAndSortingRepository<E, ID> {
}

spring-context.xml中添加如下配置:

    <elasticsearch:transport-client id="client" cluster-name="elasticsearch"
        cluster-nodes="127.0.0.1:9300" client-transport-sniff="false"/>
    <elasticsearch:repositories base-package="win.wellcoding.elasticsearch.repository"
        elasticsearch-template-ref="elasticsearchTemplate"/>
    <bean name="elasticsearchTemplate"
        class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client"/>
    </bean>

cluster-name是Elasticsearch集群名称;cluster-nodes是集群节点,使用的端口是9300而不是RESTful API使用的9200,用英文逗号分割;base-package是扫描包路径,会扫描这个包下的所有类。注意基础接口类不能在base-package
然后创建VO以及对应的Repository接口,接口实现不需要写,会生成默认实现:

package win.wellcoding.elasticsearch.vo;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;

@Document(indexName = "article-index", type = "article", shards = 1, replicas = 0)
public class ArticleVo implements Serializable {

    @Id
    public Long id;
    @Field(type = FieldType.String, searchAnalyzer = "ik_max_word", analyzer = "ik_max_word")
    public String title;
    @Field(type = FieldType.String, searchAnalyzer = "ik", analyzer = "ik")
    public String content;

    // **** Getter and Setter ****
}

VO类必须实现Serializable接口,使用@Document注解指定index以及type,Spring Data会在项目启动时检查对应的index和type是否存在,不存在则会创建;使用@Id指定主键字段;为要添加到Elasticsearch中的字段添加@Field注解,指定字段类型以及解析器。

package win.wellcoding.elasticsearch.repository;

import win.wellcoding.elasticsearch.vo.ArticleVo;
import win.wellcoding.elasticsearch.BaseSearchRepository;

import java.util.List;

public interface ArticleRepository extends BaseSearchRepository<ArticleVo, Long> {
}

在Service中就可以注入ArticleRepository对象,进行数据写入Elasticsearch、删除、搜索等操作了。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值