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支持

        
        <dependency>
            <groupId>org.elasticsearchgroupId>
            <artifactId>elasticsearchartifactId>
            <version>2.4.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.datagroupId>
            <artifactId>spring-data-elasticsearchartifactId>
            <version>2.0.4.RELEASEversion>
        dependency>

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


<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">

    
    <elasticsearch:repositories base-package="com.kayo.bos.index" />

    
    <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 {

    List
findByTitle(String title); Page
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
findAll(); public Page
findAll(Pageable pageable); public List
findByTitle(String title); public Page
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
findAll() { return articleRepository.findAll(new Sort(new Sort.Order( Sort.Direction.ASC, "id"))); } public Page
findAll(Pageable pageable) { return articleRepository.findAll(pageable); } public List
findByTitle(String title) { return articleRepository.findByTitle(title); } public Page
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
findByTitle(String title);

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

Page
findByTitle(String title, Pageable pageable);

你可能感兴趣的:(elasticsearch)