elasticsearch簡單運用
建立一個springboot工程,引入依賴java
<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文件配置以下:
node
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包路徑
以上工程建立完畢
mysql
建立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的類型spring
在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數據
sql
編寫查詢方法
@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條數據
各位在玩的時候能夠本身編寫簡單的類測試,不用把類的字段寫的這麼多
數據庫