安装elasticsearch
docker pull elasticsearch:6.8.8
启动:
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:6.8.8
安装 mobz/elasticsearch-head:5(类似navicat数据库客户端)
docker pull mobz/elasticsearch-head:5
docker run --name elasticsearch-head -d -p 9100:9100 mobz/elasticsearch-head:5
这边要分别做两个操作
浏览器访问 ip:9100
额外设置,不然连不上
ElasticSearch-head 操作时不修改配置,默认会报 406错误码
编辑vendor.js
把文件复制出来 docker cp 容器id:/usr/src/app/_site/vendor.js /usr/local/
修改
再复制进去 docker cp /usr/local/vendor.js 容器id:/usr/src/app/_site
ElasticSearch.yml文件修改
修改docker中elasticsearch的elasticsearch.yml文件
进入容器 docker exec -it 容器id /bin/bash
vi elasticsearch.yml
在下面添加两行
http.cors.enabled: true
http.cors.allow-origin: "*"
退出容器 exit
容器上面两个容器
代码篇
pom:
版本要根据官网上的要求要统一一下,不然一会会报错
我这边用的springboot2.2.6对应的spring-data-elasticsearch是3.2.6
正好对应上我之前安装的6.8.8的版本
依赖包
引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
properties文件配置
spring.data.elasticsearch.clusterName=es spring.data.elasticsearch.cluster-nodes=172.23.189.69:9300
9300对应tcp协议端口 9200对应的http协议端口
后面都是代码。我就贴出来吧
service层
package com.sharding.jdbc.es.service.impl;
import cn.hutool.core.collection.IterUtil;
import com.google.common.collect.Lists;
import com.sharding.jdbc.es.dto.CommodityPageQuery;
import com.sharding.jdbc.es.entity.Commodity;
import com.sharding.jdbc.es.mapper.CommodityRepository;
import com.sharding.jdbc.es.service.CommodityService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.MultiMatchQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @description: class
* @author: zhangguoliang
* @date: 2021/12/22 15:47
*/
@Service
public class CommodityServiceImpl implements CommodityService {
@Autowired
private CommodityRepository commodityRepository;
/**
* 新增
* @param commodity
*/
@Override
public void save(Commodity commodity) {
commodityRepository.save(commodity);
}
/**
* 查询所有
* @return
*/
@Override
public List<Commodity> getListCommodities() {
Iterable<Commodity> all = commodityRepository.findAll();
List<Commodity> commodities = Lists.newArrayList(all);
return commodities;
}
/**
* 条件删除
* @param commodity
*/
@Override
public void delete(Commodity commodity) {
commodityRepository.delete(commodity);
}
/**
* 条件查询
* @param name
* @return
*/
@Override
public List<Commodity> getCommoditiesByName(String name) {
List<Commodity> commodityList = new ArrayList<>();
// MatchQueryBuilder queryBuilder = new MatchQueryBuilder("name", name);
SearchQuery searchQuery= new NativeSearchQueryBuilder().withQuery(QueryBuilders.commonTermsQuery("name",name)).build();
Iterable<Commodity> search = commodityRepository.search(searchQuery);
search.forEach(e -> commodityList.add(e));
return commodityList;
}
/**
* 分页查询
* @param commodityQuery
* @return
*/
@Override
public Page<Commodity> pageQuery(CommodityPageQuery commodityQuery) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if(null!=commodityQuery.getName()){
boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("name",commodityQuery.getName()));
}
if(null!=commodityQuery.getBrand()){
boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("brand",commodityQuery.getBrand()));
}
SearchQuery searchQuery=new NativeSearchQueryBuilder().
withQuery(boolQueryBuilder).
withPageable(PageRequest.of(commodityQuery.getPage()-1,commodityQuery.getSize())).build();
Page<Commodity> search = commodityRepository.search(searchQuery);
return search;
}
}
Dao层
@Repository
public interface CommodityRepository extends ElasticsearchRepository<Commodity,String> {
}
写个测试类就完事了
@Autowired
private CommodityService commodityService;
@Test
public void save(){
Commodity commodity = new Commodity();
commodity.setSkuId("6R_y5X0BpkAYTaTK6LFW");
commodity.setName("原味切片面包(10片装)");
commodity.setCategory("101");
commodity.setPrice(880);
commodity.setBrand("良品铺子");
commodityService.save(commodity);
commodity = new Commodity();
commodity.setSkuId("1501009002");
commodity.setName("原味切片面包(6片装)");
commodity.setCategory("101");
commodity.setPrice(680);
commodity.setBrand("良品铺子");
commodityService.save(commodity);
commodity = new Commodity();
commodity.setSkuId("1501009004");
commodity.setName("元气吐司850g");
commodity.setCategory("101");
commodity.setPrice(120);
commodity.setBrand("百草味");
commodityService.save(commodity);
}
@Test
public void getGoodByName(){
List<Commodity> commoditiesByName = commodityService.getCommoditiesByName("原味切片面包");
for (Commodity commodity:commoditiesByName){
System.out.println(commodity);
}
}
@Test
public void delete(){
Commodity commodity = new Commodity();
commodity.setSkuId("6R_y5X0BpkAYTaTK6LFW");
commodityService.delete(commodity);
}
@Test
public void pageQuery(){
CommodityPageQuery commodityPageQuery = new CommodityPageQuery();
commodityPageQuery.setPage(1);
commodityPageQuery.setSize(10);
Page<Commodity> commodities = commodityService.pageQuery(commodityPageQuery);
System.out.println(JSONObject.toJSONString(commodities));
}