SpringDataElasticSearch使用

运行elasticsearch-2.4.0\bin\elasticsearch.bat文件(JAVA_HOME环境变量要提前配置好)
切换到elasticsearch 的运行命令目录,如:D:\elasticsearch-2.4.0\bin,执行如下命令:

plugin.bat install mobz/elasticsearch-head

配置集合ik分词器
下载地址https://github.com/medcl/elasticsearch-analysis-ik/tree/2.x
1)将elasticsearch-analysis-ik-2.x\target\releases路径下的7个文件复制到
elasticSearch中,复制的具体地址是 plugins/analysis-ik(没有这个文件夹,就先手动创建一个)。
2)复制文件到plugins.
3)进入target/release/config目录,将所有配置文件,复制到elasticSearch的config目录下
4)在elasticsearch.yml这个文件最后一行,添加如下一行,直接拷贝,注意:type后面的”:”是英文输入法,并且与”ik”中间有空格

index.analysis.analyzer.ik.type: "ik"

引入elasticsearch和spring data elasticsearch支持

        <!--elasticsearch依赖 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>

applicationContext.xml中要引入spring data elasticsearch名称空间,bean的注入与jpa类似

<?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/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/elasticsearch
        http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">

    <!-- 搜索DAO 扫描 -->
    <elasticsearch:repositories base-package="com.kayo.bos.index" />

    <!-- 配置Client -->
    <elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300"/>

    <!-- 配置搜索模板  -->
    <bean id="elasticsearchTemplate" 
        class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>
</beans>

编写DAO自动操作elasticsearch继承ElasticsearchRepository接口

package com.kayo.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.kayo.domain.Article;

public interface ArticleRepository extends
        ElasticsearchRepository<Article, Integer> {

    List<Article> findByTitle(String title);

    Page<Article> findByTitle(String title, Pageable pageable);

}

编写Service

package com.kayo.service;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.kayo.domain.Article;

public interface ArticleService {
    public void save(Article article);

    public void delete(Article article);

    public Article findOne(Integer id);

    public Iterable<Article> findAll();

    public Page<Article> findAll(Pageable pageable);

    public List<Article> findByTitle(String title);

    public Page<Article> findByTitle(String title, Pageable pageable);
}

Service实现类

package com.kayo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import cn.itcast.dao.ArticleRepository;
import cn.itcast.domain.Article;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleRepository articleRepository;

    public void save(Article article) {
        articleRepository.save(article);
    }

    public void delete(Article article) {
        articleRepository.delete(article);
    }

    public Article findOne(Integer id) {
        return articleRepository.findOne(id);
    }

    public Iterable<Article> findAll() {
        return articleRepository.findAll(new Sort(new Sort.Order(
                Sort.Direction.ASC, "id")));
    }

    public Page<Article> findAll(Pageable pageable) {
        return articleRepository.findAll(pageable);
    }

    public List<Article> findByTitle(String title) {
        return articleRepository.findByTitle(title);
    }

    public Page<Article> findByTitle(String title, Pageable pageable) {
        return articleRepository.findByTitle(title, pageable);
    }

}

注解定义索引映射信息
在使用spring data elasticsearch开发,需要将索引和映射信息配置实体类上面
@Document文档对象(索引信息、文档类型)
@Id文档主键 唯一标识
@Field每个文档的字段配置(类型、是否分词、是否存储、分词器)

package com.kayo.domain;

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.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "blog3", type = "article")
public class Article {
    @Id
    @Field(index = FieldIndex.not_analyzed, store = true, type = FieldType.Integer)
    private Integer id;
    @Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
    private String title;
    @Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Article [id=" + id + ", title=" + title + ", content="
                + content + "]";
    }

}

通过ElasticsearchTemplate创建索引和添加映射

    public void createIndex() {
        elasticsearchTemplate.createIndex(Article.class);
        elasticsearchTemplate.putMapping(Article.class);
    }

CURD和分页排序查询
CurdRepository提供增删改查save、delete、findAll、findOne

PagingAndSortingRepository提供分页和排序
查询标题方法

List<Article> findByTitle(String title);

分页条件查询,只需要在查询方法中,添加Pageable对象
排序条件查询,只需要在查询方法中,添加Sort对象

Page<Article> findByTitle(String title, Pageable pageable);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值