天天看點

Elasticsearch-三-SpringDataElasticsearch-搜尋環境搭建建立Document 類建立 Repository 接口測試參考

環境搭建

本文項目基于 SpringBoot 2.1.3.RELEASE 進行建構,首先引入 Spring Data ElasticSearch 的依賴。

引入依賴 spring-boot-starter-data-elasticsearch

1
2
3
4
      
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
      

Spring Data ElasticSearch 與 ElasticSearch 有對應關系

https://github.com/spring-projects/spring-data-elasticsearch

Elasticsearch-三-SpringDataElasticsearch-搜尋環境搭建建立Document 類建立 Repository 接口測試參考
Elasticsearch-三-SpringDataElasticsearch-搜尋環境搭建建立Document 類建立 Repository 接口測試參考

根據上面的版本資訊

而本文使用的 SpringBoot 2.1.3.RELEASE 自動依賴的 Spring Data ElasticSearch 版本是 3.1.5.RELEASE,對應的 elasticsearch 版本是 6.4.3

修改application.yml

1
2
3
4
5
6
7
8
      
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
spring.data.elasticsearch.cluster-name=elasticsearch
spring.elasticsearch.jest.read-timeout=3s
spring.elasticsearch.jest.uris=http://localhost:9200
spring.elasticsearch.jest.connection-timeout=5s
spring.elasticsearch.jest.multi-threaded=true
      

建立Document 類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@AllArgsConstructor
@Document(indexName = "es_user",type = "user")
public class User {
    private Long id;
    @Field(type = FieldType.Text)
    private String name;
    private int age;
    @Field(type = FieldType.Text,analyzer = "ik_smart")
    private String description;
}
      

建立 Repository 接口

1
2
3
4
5
6
7
8
      
import com.felix.project.model.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

import java.util.List;

public interface UserRepository extends ElasticsearchCrudRepository<User,Long> {
    List<User> findByName(String name);
}
      

測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
      
import com.felix.project.model.User;
import com.felix.project.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTests {
    @Autowired(required = false)
    private UserRepository userRepository;

    /**
     * 建立索引
     */
    @Test
    public void testUserIndex() {
        for (int i = 0; i < 10; i++) {
            userRepository.save(new User((long) i, "Felix" + i, i, "Java開發工程師"));
        }
    }

    /**
     * 查詢所有
     */
    @Test
    public void findAllTest() {
        Iterable<User> list = userRepository.findAll();
        for (User user : list) {
            System.out.println(user.getId() + " " + user.getName());
        }
    }

    /**
     * 查詢所有
     */
    @Test
    public void findAllBySortTest() {
        //分頁并排序
        Iterable<User> list = userRepository.findAll(PageRequest.of(0, 15, Sort.by("id").descending()));
        for (User user : list) {
            System.out.println(user.getId() + " " + user.getName());
        }
    }

    /**
     * 删除索引
     */
    @Test
    public void deleteTest() {
        //根據id删除索引
        userRepository.deleteById(1L);
    }

    /**
     * 更新索引
     */
     @Test
    public void updateTest(){
        //Lucene沒有更新索引
        //隻需要保證id是唯一的沒有會添加索引,存在則會删除後,重新添加索引
        userRepository.save(new User(1L, "Felix" , 12,"Java開發工程師"));
    }

      /**
     * 根據name查詢
     */
     @Test
    public void findByNameTest(){
        List<User> felix = userRepository.findByName("Felix");
        System.out.println(felix);
    }
    
}
      

參考

SpringDataElasticsearch 自定義查詢參考

https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.query-methods

https://blog.csdn.net/larger5/article/details/79777319

https://www.jianshu.com/p/27e1d583aafb