spring-boot-starter-parent
org.springframework.boot
2.3.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-elasticsearch
spring:
elasticsearch:
rest:
uris: http://localhost:9200
@Data
@Document(indexName = "goods") //indexName 为索引库名称
public class Goods implements Serializable {
@Field(type = FieldType.Keyword) //必须指定关键字
private String id;
@Field(type = FieldType.Text)
private String goodsName;
@Field(type = FieldType.Integer)
private Integer store;
@Field(type = FieldType.Double)
private double price;
}
@Repository
public interface GoodsDao extends ElasticsearchRepository {
}
解释:ElasticsearchRepository
@Autowired
private GoodsDao goodsDao;
/**
* 添加文档
* */
@Test
public void saveTest(){
Goods goods = new Goods();
goods.setId("1");
goods.setGoodsName("华为手机");
goods.setStore(100);
goods.setPrice(5000);
goodsDao.save(goods);
System.out.println("添加成功...");
}
/**
* 根据ID查询文档
* */
@Test
public void findById(){
Goods goods = goodsDao.findById("1").get();
System.out.println(goods);
}