1,新建一个es的项目。
2,导入依赖Elasticsearch
<dependencies>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
3,配置文件
spring:
data:
elasticsearch:
cluster-name: elasticsearch #集群名字
cluster-nodes: 192.168.84.130:9300 #elasticsearch服务器ip地址
jackson:
default-property-inclusion: non_null #作用:当返回数据为null:如name:null ,此时不会显示到页面上来。
4,编写pojo
@Document(
indexName = "item",//索引名字一定要小写,否则启动会出错:“InvalidIndexNameException[Invalid index name [Item], must be lowercase”
type = "docs",
shards = 1,//默认5
replicas = 0//默认1
)
public class Item {
@Id
Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
String title;
@Field(type = FieldType.Keyword)
String category;
@Field(type = FieldType.Keyword)
String brand;
@Field(type = FieldType.Double)
Double price;
@Field(type = FieldType.Keyword,index = false)
String images;
public Item(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public