Spring boot 整合 Elasticsearch

第一、安装Elasticsearch 请移步

 本文Elasticsearch版本:6.4.2

第二、项目操作

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

实体类   indexName 可以当做数据库名 加上了@Document注解之后,默认情况下这个实体中所有的属性都会被建立索引、并且分词

@Data
@Document(indexName = "commodity")
public class Commodity implements Serializable {

    @Id
    private String skuId;

    private String name;

    private String category;

    private Integer price;

    private String brand;

    private Integer stock;

}

dao层

@Repository
public interface CommodityRepository extends ElasticsearchRepository<Commodity, String> {
}

service层

public interface CommodityService {
    long count();

    Commodity save(Commodity commodity);

    void delete(Commodity commodity);

    Iterable<Commodity> getAll();

    List<Commodity> getByName(String name);

    Page<Commodity> pageQuery(Integer pageNo, Integer pageSize, String kw);
}

实现类

@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private CommodityRepository commodityRepository;


    @Override
    public long count() {
        return commodityRepository.count();
    }

    @Override
    public Commodity save(Commodity commodity) {
        return commodityRepository.save(commodity);
    }

    @Override
    public void delete(Commodity commodity) {
        commodityRepository.delete(commodity);
//        commodityRepository.deleteById(commodity.getSkuId());
    }

    @Override
    public Iterable<Commodity> getAll() {
        return commodityRepository.findAll();
    }

    @Override
    public List<Commodity> getByName(String name) {
        List<Commodity> list = new ArrayList<>();
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", name);
        Iterable<Commodity> iterable = commodityRepository.search(matchQueryBuilder);
        iterable.forEach(e->list.add(e));
        return list;
    }

    @Override
    public Page<Commodity> pageQuery(Integer pageNo, Integer pageSize, String kw) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchPhraseQuery("name", kw))
                .withPageable(PageRequest.of(pageNo, pageSize))
                .build();
        return commodityRepository.search(searchQuery);
    }
}

controller类

@RestController
@RequestMapping(value = "/es")
public class ESController {

    @Autowired
    private CommodityService commodityService;

    @RequestMapping("/testInsert")
    public void testInsert() {
        Commodity commodity = new Commodity();
        commodity.setSkuId("1501009001");
        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);

    }

    @RequestMapping(value = "/testGetByName")
    public Result testGetByName(String name) {
        List<Commodity> list = commodityService.getByName(name);
        return new Result(Result.STATE_SUCCESS,list);
    }

    @RequestMapping(value = "/testPage")
    public Result testPage() {
        Page<Commodity> page = commodityService.pageQuery(0, 10, "切片");
        System.out.println(page.getTotalPages());
        System.out.println(page.getNumber());
        System.out.println(page.getContent());
        return new Result(Result.STATE_SUCCESS,page);
    }

application.yml配置信息  cluster-name 必须和elasticsearch.yml的cluster.name 一样

spring:
  application:
    name: qxgf-elasticsearch
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300
      cluster-name: qxgf-es
      repositories:
        enabled: true

测试

原文地址:https://www.cnblogs.com/tangyin/p/10838463.html