elasticsearch简单运用
创建一个springboot工程,引入依赖
<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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
版本根据自己的实际情况引入
application.yml文件配置如下:
spring:
datasource:
#driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: 123456
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
mybatis:
type-aliases-package: com.lsl.elasticsearch.pojo
启动类上加扫描的dao包路径
以上工程创建完毕
创建Sku对象
public class Sku {
private Long id;
private Long spuId;
private String title;
private String images;
private Long price;
private String indexes;
private String ownSpec;
private Boolean enable;
private Date createTime;
private Date lastUpdateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getSpuId() {
return spuId;
}
public void setSpuId(Long spuId) {
this.spuId = spuId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public String getIndexes() {
return indexes;
}
public void setIndexes(String indexes) {
this.indexes = indexes;
}
public String getOwnSpec() {
return ownSpec;
}
public void setOwnSpec(String ownSpec) {
this.ownSpec = ownSpec;
}
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
@Override
public String toString() {
return "Sku{" +
"id=" + id +
", spuId=" + spuId +
", title='" + title + '\'' +
", images='" + images + '\'' +
", price=" + price +
", indexes='" + indexes + '\'' +
", ownSpec='" + ownSpec + '\'' +
", enable=" + enable +
", createTime=" + createTime +
", lastUpdateTime=" + lastUpdateTime +
'}';
}
}
创建SkuService编写查询所有sku的方法
@Autowired
private SkuDao skuDao;
public List<Sku> queryAllSku(){
List<Sku> goodsList= skuDao.queryAllSku();
return goodsList;
}
创建SkuDao
@Select("select * from goods")
List<Sku> queryAllSku();
创建Goods对象,这个对象用来创建索引
@Document(indexName = "test",type = "goods",shards = 1,replicas = 0)
public class Goods {
@Id
private Long id;
private Long spuId;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Keyword,index = false)
private String images;
private Long price;
@Field(type = FieldType.Keyword)
private String indexes;
@Field(type = FieldType.Keyword)
private String ownSpec;
private Boolean enable;
private Date createTime;
private Date lastUpdateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getSpuId() {
return spuId;
}
public void setSpuId(Long spuId) {
this.spuId = spuId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public String getIndexes() {
return indexes;
}
public void setIndexes(String indexes) {
this.indexes = indexes;
}
public String getOwnSpec() {
return ownSpec;
}
public void setOwnSpec(String ownSpec) {
this.ownSpec = ownSpec;
}
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", spuId=" + spuId +
", title='" + title + '\'' +
", images='" + images + '\'' +
", price=" + price +
", indexes='" + indexes + '\'' +
", ownSpec='" + ownSpec + '\'' +
", enable=" + enable +
", createTime=" + createTime +
", lastUpdateTime=" + lastUpdateTime +
'}';
}
}
创建GoodsDao
public interface GoodsDao extends ElasticsearchRepository<Goods,Integer> {
}
只需继承ElasticsearchRepository类即可,第一个参数是哪一个对象,第二个参数是id的类型
在SkuService中添加buildGoods方法,将Sku对象转换成Goods对象
public Goods buildGoods(Sku sku){
Goods goods=new Goods();
goods.setId(sku.getId());
goods.setSpuId(sku.getSpuId());
goods.setTitle(sku.getTitle());
goods.setPrice(sku.getPrice());
goods.setOwnSpec(sku.getOwnSpec());
goods.setImages(sku.getImages());
goods.setEnable(sku.getEnable());
goods.setIndexes(sku.getIndexes());
goods.setCreateTime(sku.getCreateTime());
goods.setLastUpdateTime(sku.getLastUpdateTime());
return goods;
}
在测试类中编写方法将所有数据导入索引库
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private SkuService skuService;
@Autowired
private GoodsDao goodsDao;
@Test
public void contextLoads() {
elasticsearchTemplate.createIndex(Goods.class);
elasticsearchTemplate.putMapping(Goods.class);
List<Sku> skus = skuService.queryAllSku();
List<Goods> goodsList=skus.stream().map(sku -> {
try {
return skuService.buildGoods((Sku)sku);
}catch (Exception e){
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
goodsDao.saveAll(goodsList);
}
查看索引库中的数据
我们发现在索引名称为test,type为goods中已经导入了我们在数据库中的sku数据
编写查询方法
@Test
public void matchQuery(){
int page=0;
int size=5;
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withQuery(QueryBuilders.matchQuery("title","小米华为vivo"));
queryBuilder.withPageable(PageRequest.of(page,size));
queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
Page<Goods> goods = goodsDao.search(queryBuilder.build());
for (Goods good : goods) {
System.out.println(good);
}
System.out.println(goods.getTotalElements());
}
只要title中包含小米或者华为或者vivo的都会被查出来,因为我们设置了title字段分词,这里显示5条因为分页每页5条数据
各位在玩的时候可以自己编写简单的类测试,不用把类的字段写的这么多