SpringDataElasticSearch使用

15242 ワード

elasticsearch-2.4を実行します.0\bin\elasticsearch.Batファイル(JAVA_HOME環境変数は事前に設定する)は、D:elasticsearch-2.4.0binなどのelasticsearchの実行コマンドディレクトリに切り替え、次のコマンドを実行します.
plugin.bat install mobz/elasticsearch-head

セットik分詞器ダウンロードアドレスの構成https://github.com/medcl/elasticsearch-analysis-ik/tree/2.x1)elasticsearch-analysis-ik-2.xtargetreleasesパスの下の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); }

サービスの作成
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); }

サービス実装クラス
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各ドキュメントのフィールド構成(タイプ、No分詞、格納するかどうか、分詞器)
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);