使用Springboot整合ElasticSearch以及通过接口热更新分词和同义词

本文介绍了如何使用Springboot整合ElasticSearch,包括java配置、索引构建、数据操作、搜索以及高级特性如分片和备份。重点讲解了如何添加ik分词器和dynamic-synonym同义词插件,以及它们的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是ElasticSearch

Elasticsearch(ES) 是一个基于Lucene构建的开源、分布式、RESTful接口全文搜索引擎。ElasticSearch还是一个分布式文档数据库,其中每个字段均被索引且可被搜索,它能够扩展至数以百计的服务器存储以及处理PB级的数据。他可以在很短的时间内存储、搜索和分析大量的数据。

demo地址 : https://github.com/BKTiger/elasticsearch-demo.git

java与ES整合配置

  1. 引入依赖
    本项目使用的Springboot版本为2.0.7.RELEASE,注意必须指定elasticsearch版本,否则依赖会报错。
<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<elasticsearch.version>7.1.0</elasticsearch.version> <!-- 这里须指定ElasticSearch版本-->
</properties>

<dependency>
		<groupId>org.elasticsearch.client</groupId>
		<artifactId>elasticsearch-rest-high-level-client</artifactId>
		<version>${
   elasticsearch.version}</version>
</dependency>
  1. 引入配置
    2.1 简版配置
    @Configuration
    public class ElasticSearchRestConfig {
         
    
       @Bean
        public RestHighLevelClient restHighLevelClient(){
         
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("localhost", 9200, "http")));
            return client;
        }
    }
    
    2.2 终板配置
    @Configuration
    public class ElasticSearchRestConfig {
         
    
        @Value("${data.elasticsearch.url}")
        private String[] ipAddress;
        private static final int ADDRESS_LENGTH = 2;
        private static final String HTTP_SCHEME = "http";
    
        @Bean
        public RestClientBuilder restClientBuilder() {
         
            HttpHost[] hosts = Arrays.stream(ipAddress)
                    .map(this::makeHttpHost)
                    .filter(Objects::nonNull)
                    .toArray(HttpHost[]::new);
            return RestClient.builder(hosts);
        }
    
        @Bean
        @Primary
        public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) {
         
            //TODO 此处可以进行其它操作
            return new RestHighLevelClient(restClientBuilder);
        }
    
        private HttpHost makeHttpHost(String s) {
         
            String[] address = s.split(":");
            if (address.length == ADDRESS_LENGTH) {
         
                String ip = address[0];
                int port = Integer.parseInt(address[1]);
                return new HttpHost(ip, port, HTTP_SCHEME);
            } else {
         
                return null;
            }
        }
    }
    

索引的构建

  1. 索引的创建
    创建代码参考demo的test方法中的 com.zhangtai.demo.DemoBootApplicationTests#createIndex,该方法中引入了ik分词器和dynamic-synonym同义词插件,下文中有使用和配置的方法,需要先加入ik分词器和dynamic-synonym同义词插件后才可使用该方法创建索引,如果不使用可以不进行索引的人工创建,es会自行创建,分词器使用默认分词器,每个汉字为一个分词。
  2. 索引的删除
	// DELETE http://127.0.0.1:9200/goods
	private void deleteAll(String index){
   
		DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
		try {
   
			restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
		} catch (IOException e) {
   
			e.printStackTrace();
		}
	}
  1. 判断索引是否存在
	public void indexExist(){
   
		// goods索引是否存在
		GetIndexRequest goods = new GetIndexRequest("goods");
		try {
   
			boolean exists = restHighLevelClient.indices().exists(goods, RequestOptions.DEFAULT);
			System.out.println("goods索引存在与否:"+exists);
		} catch (IOException e) {
   
			e.printStackTrace();
		}
	}

数据操作

  1. 新增数据
    1.1 实体类

    @Data
    public class Goods {
         
    
        private String id;
    
        private String name;
    
        private BigDecimal price;
    
        private Long skuId;
    
        private Long spuId;
    
        private Long colorId;
    
        private String colorName;
    
        public Goods() {
         
        }
    
        public Goods(String id, String name, BigDecimal price, Long skuId, Long spuId, Long colorId, String colorName) {
         
            this.id = id;
            this.name = name;
            this.price = price;
            this.skuId = skuId;
            this.spuId = spuId;
            this.colorId = colorId;
            this.colorName = colorName;
        }
    
        public Goods(String name, BigDecimal price, Long skuId, Long spuId, Long colorId, String colorName) {
         
            this.name = name;
            this.price = price;
            this.skuId = skuId;
            this.spuId = spuId;
            this.colorId = colorId;
            this.colorName = colorName;
        }
    }
    

    2.2 新增测试数据

    	private List<Map<String,Object>> getDate(){
         
    		List<Map<String,Object>> result = new ArrayList<>();
    		Goods goods = new Goods("00001","袜子",new BigDecimal(21.3D),1L,1L,1L,"红色");
    		String s = JSON.toJSONString(goods);
    		Map map = JSON.parseObject(s, Map.class);
    		result.add(map);
    		goods = new Goods("00002","袜子",new BigDecimal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值