环境搭建
首先对环境进行搭建–引入依赖
核心依赖如下(其他依赖自行引入)
<!--注意:springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<!--springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--通过spring data 操作Es-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--springboot 继承test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入lombook-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
注意:低版本的springboot 不支持springboot data
开发搜索方法
创建实体类:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "shop", type = "product")
public class Product {
@Id
private String pid;
// type 指定数据类型 test支持分词 analyzer 指定分词 searchAnalyzer 指定搜索分词器
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String pname;
@Field(type = FieldType.Double)
private Double marketprice;
@Field(type = FieldType.Double)
private Double shopprice;
@Field(type = FieldType.Text)
private String image;
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String description;
@Field(type = FieldType.Integer)
private Integer ishot;
}
编写yml配置信息
spring:
data:
elasticsearch:
cluster-nodes: 172.16.251.142:9300 ##指定连接ip地址和端口号
编写简单Repository接口类
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
//根据需要自己定义方法接口
List<Product> findProductByMarketpr