美文网首页JAVA_Spring
SpringBoot 5 集成Elasticsearch

SpringBoot 5 集成Elasticsearch

作者: kason_zhang | 来源:发表于2018-11-03 12:26 被阅读2次

此处学习下在SpringBoot中使用Elasticsearch的方式

先看下ES上的页面数据:


ES

此处我们对face_fixedperson 这个索引进行操作.

(1) 编写pom文件, 此处的es版本我们使用的是2.3.5

<!-- elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.3.5</version>
            <!-- <version>6.0.0</version> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <!-- <version>2.1.3</version>   -->
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <!-- <version>3.0.9</version> -->
        </dependency>

(2) 编写索引对应的实体类, Person

package com.kason.kasonpro.entity;

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

import java.io.Serializable;

/**
 * Created by zhangkai12 on 2018/6/26.
 */
@Document(indexName = "face_fixedperson", type = "Fixedperson",shards = 3,replicas = 0)
public class Person implements Serializable {
    @Id
    private String id;
    @Field
    private String locate;
    @Field
    private String degree;
    @Field
    private String name;
    @Field
    private int age;

    public String getLocate() {
        return locate;
    }

    public void setLocate(String locate) {
        this.locate = locate;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", locate='" + locate + '\'' +
                ", degree='" + degree + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

(3) 编写Repository类

package com.kason.kasonpro.repository;

import com.kason.kasonpro.entity.Person;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by zhangkai12 on 2018/6/26.
 */
public interface PersonRepository extends CrudRepository<Person, String> {


    public List<Person> getPersonByDegree(String degree);



}

(4) 编写Service接口以及Service实现

package com.kason.kasonpro.service;

import com.kason.kasonpro.entity.Person;

import java.util.List;

/**
 * Created by zhangkai12 on 2018/6/26.
 */
public interface PersonService {

    public List<Person> getPersonByDegree(String degree);
}
package com.kason.kasonpro.service.impl;

import com.kason.kasonpro.entity.Person;
import com.kason.kasonpro.repository.PersonRepository;
import com.kason.kasonpro.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangkai12 on 2018/6/26.
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;

    @Override
    public List<Person> getPersonByDegree(String degree) {
        return personRepository.getPersonByDegree("研究生");
    }
}

(5) 编写测试类:

package com.kason.kasonpro.service.impl;

import com.kason.kasonpro.KasonProApplicationTests;
import com.kason.kasonpro.entity.Person;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by zhangkai12 on 2018/6/26.
 */
public class PersonServiceImplTest extends KasonProApplicationTests{

    @Autowired
    private PersonServiceImpl personService;
    @Test
    public void testPersonGet() {

        List<Person> p;
        p = personService.getPersonByDegree("研究生");
        System.out.println(p.size());

        p.forEach(res -> {
            System.out.println(res);
        });
    }

}

测试结果:


结果

相关文章

网友评论

    本文标题:SpringBoot 5 集成Elasticsearch

    本文链接:https://www.haomeiwen.com/subject/hpqmyftx.html