本文使用内容    springBoot2.2.5.RELEASE版本   Elasticsearch7.6.2  linux版本的   SpringDataElasticSearch与Springboot版本对应

一、操作准备

1、导入依赖

此处版本

2.2.5.RELEASE
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2、创建application.yml文件

spring:
  elasticsearch:
    rest:
      uris: http://192.168.0.211:9200

此处的ip:端口  是自己的Elasticsearch的,如果为集群可以以逗号(,)隔开

3、创建实体类对象

最完整的springboot2.2.x.RELEASE整合springDataElasticsearch 7.6.2
 1 package cn.cqsw.elasticsearch.pojo;
 2 
 3 import org.springframework.data.annotation.Id;
 4 import org.springframework.data.elasticsearch.annotations.Document;
 5 import org.springframework.data.elasticsearch.annotations.Field;
 6 import org.springframework.data.elasticsearch.annotations.FieldType;
 7 
 8 import java.io.Serializable;
 9 
10 @Document(indexName = "item" , type = "_doc",shards = 1,replicas = 0)
11 public class Item implements Serializable {
12     @Id    //注意此处的@Id必须为springframework包下面的id   import org.springframework.data.annotation.Id;
13     Long id;
14     @Field(type = FieldType.Text,analyzer = "ik_max_word")
15     String title; //标题
16     @Field(type = FieldType.Keyword)
17     String category;// 分类
18     @Field(type = FieldType.Keyword)
19     String brand; // 品牌
20     @Field(type = FieldType.Double)
21     Double price; // 价格
22     @Field(type = FieldType.Keyword,index = false)
23     String images; // 图片地址
24 
25     public Long getId() {
26         return id;
27     }
28 
29     public void setId(Long id) {
30         this.id = id;
31     }
32 
33     public String getTitle() {
34         return title;
35     }
36 
37     public void setTitle(String title) {
38         this.title = title;
39     }
40 
41     public String getCategory() {
42         return category;
43     }
44 
45     public void setCategory(String category) {
46         this.category = category;
47     }
48 
49     public String getBrand() {
50         return brand;
51     }
52 
53     public void setBrand(String brand) {
54         this.brand = brand;
55     }
56 
57     public Double getPrice() {
58         return price;
59     }
60 
61     public void setPrice(Double price) {
62         this.price = price;
63     }
64 
65     public String getImages() {
66         return images;
67     }
68 
69     public void setImages(String images) {
70         this.images = images;
71     }
72 
73     public Item() {
74     }
75 
76     public Item(Long id, String title, String category, String brand, Double price, String images) {
77         this.id = id;
78         this.title = title;
79         this.category = category;
80         this.brand = brand;
81         this.price = price;
82         this.images = images;
83     }
84 
85 
86     @Override
87     public String toString() {
88         return "Item{" +
89                 "id=" + id +
90                 ", title='" + title + '\'' +
91                 ", category='" + category + '\'' +
92                 ", brand='" + brand + '\'' +
93                 ", price=" + price +
94                 ", images='" + images + '\'' +
95                 '}';
96     }
View Code

相关文章: